Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
821 views
in Technique[技术] by (71.8m points)

command prompt - PS1 line with Git current branch and colors

Here is my current PS1:

export PS1='[u@h W$(__git_ps1 " (%s)")]$ '

How can I display the current branch in a different color?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is, part by part (and no Ruby):

function color_my_prompt {
    local __user_and_host="[33[01;32m]u@h"
    local __cur_location="[33[01;34m]w"
    local __git_branch_color="[33[31m]"
    #local __git_branch="`ruby -e "print (%x{git branch 2> /dev/null}.grep(/^*/).first || '').gsub(/^* (.+)$/, '(1) ')"`"
    local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\* (.+)$/(\\1) /`'
    local __prompt_tail="[33[35m]$"
    local __last_color="[33[00m]"
    export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt

Looks like this (with my own terminal palette):

Colored prompt

Also, see this and this article.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...