简体   繁体   English

有条件地为ZSH提示颜色

[英]Conditionally Color ZSH Prompt

I am trying to color my prompt based on the return code of the previous command. 我正在尝试根据上一条命令的返回代码为提示符着色。 I have the following: 我有以下几点:

PROMPT='%{$fg_bold[cyan]%}%~$(git_prompt_info) %($?.%{fg_bold[cyan]%}.%{$fg_bold[red]%})»%{$reset_color%} '

Specifically I'm having trouble with the %($?.%{fg_bold[cyan]%}.%{$fg_bold[red]%}) portion. 具体来说,我在%($?.%{fg_bold[cyan]%}.%{$fg_bold[red]%})部分遇到了麻烦。

Unfortunately my prompt ends up looking like this: 不幸的是,我的提示最终看起来像这样:

在此处输入图片说明

What am I doing wrong? 我究竟做错了什么?

EDIT: 编辑:

After fixing my typo I now have: 修正错字后,我现在有:

PROMPT='%{$fg_bold[cyan]%}%~$(git_prompt_info) %($?.%{$fg_bold[cyan]%}.%{$fg_bold[red]%})»%{$reset_color%} '

My prompt now looks like this: 我的提示现在看起来像这样:

在此处输入图片说明

You forgot the $ for the parameter expansion, and you don't precede the ? 您忘记了参数扩展的$ ,并且没有?之前? with a dollar; 一美元 it's a flag for the conditional, not a parameter reference. 它是条件标记,而不是参数引用。

%(?.%{$fg_bold[cyan]%}.%{$fg_bold[red]%})
  ^   ^

An alternative would be to send the return code (when different than 0) to kill -l so that you can display the name of the error code. 一种替代方法是发送返回代码(当不同于0时)以kill -l以便您可以显示错误代码的名称。

This is what I use (for the prompt at the right side) 这就是我使用的(针对右侧的提示)

setopt PROMPT_SUBST
RPS1='%(?..%{$fg_bold[red]%}[%? $(kill -l $?)]%{${reset_color}%} )%T' # time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM