开发者

How do i make the colour of the $ bit in a terminal change colour every line?

开发者 https://www.devze.com 2023-02-21 17:53 出处:网络
currently i have this: function xtitle2()# Adds some text in the terminal frame. { export var1=`echo $HOSTNAME | perl -pe \'s/^([a-zA-Z0-9]+)\\.(.*)$/\\1/g\'`

currently i have this:

function xtitle2()      # Adds some text in the terminal frame.
{
export var1=`echo $HOSTNAME | perl -pe 's/^([a-zA-Z0-9]+)\.(.*)$/\1/g'`
export var2=`pwd`
echo -n -e "\033]0;$var1 : $var2\007"

a=$(( $a + 1 ))
if (( $a > 36 ))
then
    a=30
fi

}开发者_如何转开发


PROMPT_COMMAND="xtitle2"

PS1="\e[0;${a}m$ \e[m"

but it only changes the colour when i do

$. ~/.profile

but i want it to change the colour every time any command is entered...

how do i do this?

EIDT:

ended up going with this:

function xtitle2()      # Adds some text in the terminal frame.
{
export var1=`echo $HOSTNAME | perl -pe 's/^([a-zA-Z0-9]+)\.(.*)$/\1/g'`
export var2=`pwd`
echo -n -e "\033]0;$var1 : $var2\007"

if [ -z $a ]
then
    a=29
fi

a=$(( $a + 1 ))
if (( $a > 36 ))
then
    a=30
fi
PS1="\[\033[${a}m\]$\[\e[0m\]"
}


export PROMPT_COMMAND="xtitle2"


Include "$(xtitle2)" in your PS1 setting

Of course you need to refactor xtitle2 a bit; the good news is that you won't have to abuse PROMPT_COMMAND for this purpose anymore. Also, all the vars except a could be local.

You will want to use $(($HISTCMD % 30)) instead of the jumble with variable a


Instead of double quotes in PS1="\e[0;${a}m$ \e[m" use single quotes, like this:

PS1='\e[0;${a}m$ \e[m'

... so that ${a} will be evaluated each time.


Basically PROMPT_COMMAND is the Bash feature you are probably looking for.

From man bash(1):

PROMPT_COMMAND

If set, the value is executed as a command prior to issuing each primary prompt.

So:

function rotate_prompt_colour() {
    ROTATE_COLOUR=$(( (ROTATE_COLOUR + 1) % 7))
    PS1="\h : \w \[\e[$(( 30 + ROTATE_COLOUR ))m\]\$\[\e[0m\] "
}

export PROMPT_COMMAND=rotate_prompt_colour

Note that PS1 has some useful escape sequences that can save you some hassle. Also note the \[...\] around ANSI sequences to avoid some readline weirdness.

0

精彩评论

暂无评论...
验证码 换一张
取 消