BASH:用户输入的文本颜色不同 [英] BASH: different colour for text entered by user

查看:327
本文介绍了BASH:用户输入的文本颜色不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经整理了我的BASH提示。



目前看起来像这样:



img src =https://i.stack.imgur.com/ob0Mm.pngalt =enter image description here>



这里是代码:

 #注意:OSX使用.bashprofile http://superuser.com/questions/244964/mac-os-x-bashrc- not-working 

#http://mywiki.wooledge.org/BashFAQ/037
bold = $(tput bold || tput md)
black = $(tput setaf 0)
red = $(tput setaf 1)
green = $(tput setaf 2)
blue = $(tput setaf 4)
white = | tput AF 7)
RESET = $(tput sgr0)

#https://github.com/sickill/stderred
export DYLD_INSERT_LIBRARIES =/ usr / lib / libstderred .dylib $ {DYLD_INSERT_LIBRARIES:+:$ DYLD_INSERT_LIBRARIES}
export STDERRED_ESC_CODE =$ bold $ red

pre_prompt()
{
如果[$? = 0];那么
echo$ green✔;
else
echo$ red✘;
fi

printf$ RESET\\\

printf$ bold
printf%s @%s〜%s:\\\
$ USER$ HOSTNAME$ PWD
printf$ RESET
}

提示前执行exec b $ b export PROMPT_COMMAND = pre_prompt

#\ [... \] - > http://mywiki.wooledge.org/BashFAQ/053
export PS1 =\ [$ bold $ blue\]⤐\ [$ RESET $ bold\]

export PS2 = - 2->
export PS3 = - 3->
export PS4 = - 4->



请注意,我使用一个超小的代码,由sickill使STDERR打印为红色。



我还想改进的唯一的事情是用户输入的文字。



有什么办法吗?

解决方案

$ RESET 后添加 $ bold 加上您想用来突出显示命令行条目的任何颜色代码。


I've been tidying up my BASH prompt.

Currently it looks like this:

Here is the code:

# NOTE: OSX uses .bashprofile  http://superuser.com/questions/244964/mac-os-x-bashrc-not-working

# http://mywiki.wooledge.org/BashFAQ/037
bold=$( tput bold || tput md )
black=$( tput setaf 0 )
red=$( tput setaf 1 )
green=$( tput setaf 2 )
blue=$( tput setaf 4 )
white=$( tput setaf 7 || tput AF 7 )
RESET=$( tput sgr0 )

# https://github.com/sickill/stderred
export DYLD_INSERT_LIBRARIES="/usr/lib/libstderred.dylib${DYLD_INSERT_LIBRARIES:+:$DYLD_INSERT_LIBRARIES}"
export STDERRED_ESC_CODE="$bold$red"

pre_prompt () 
{  
    if [ $? = 0 ]; then 
        echo "$green ✔";
    else 
        echo "$red ✘"; 
    fi

    printf "$RESET\n"
    printf "$bold"
    printf "%s@%s ~ %s:\n" "$USER" "$HOSTNAME" "$PWD"
    printf "$RESET"
}

# execs before prompt
export PROMPT_COMMAND=pre_prompt

# \[ ... \] --> http://mywiki.wooledge.org/BashFAQ/053
export PS1="\[$bold$blue\] ⤐  \[$RESET$bold\]"

export PS2="-2-> "
export PS3="-3-> "
export PS4="-4-> "

Note I'm using a super little piece of code by sickill to have STDERR get printed in red.

The only thing that I would still like to improve is to hilight text entered by the user.

Is there any way to do this?

解决方案

After your $RESET, add $bold plus whatever color code you want to use to highlight the commandline entry.

这篇关于BASH:用户输入的文本颜色不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆