/bin/log

Log my thoughts

Home About GitHub

02 Sep 2011
PS1 Setting

PS1='\n\e[0;36m┌─[\e[1;35m\u\e[1;34m @ \e[1;35m\h\e[0;36m]─[\e[1;34m\A\e[0;36m]─[\e[1;34m\w\e[0;36m]─[\e[0;31m\!\e[0;36m] \e[1;34m$(parse_git_branch)\n\e[0;36m└─[\e[1;37m\$\e[0;36m] \e[0m'

Add more info: // see the following for \? [\e[1;34m\?\e[0;36m]─

Add git branch info:

function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}

## Parses out the branch name from .git/HEAD:
function find_git_branch {
    local dir=.
        local head
    until [ "$dir" -ef / ]; do
        if [ -f "$dir/.git/HEAD" ]; then
            head=$(< "$dir/.git/HEAD")
            if [[ $head = ref:\ refs/heads/* ]]; then
                git_branch="${head#*/*/}"
            elif [[ $head != '' ]]; then
                git_branch="(detached)"
            else
                git_branch="(unknow)"
            fi
        echo $git_branch
            return
        fi
        dir="../$dir"
    done
    git_branch=''
}
  • \a : an ASCII bell character (07)
  • \d : the date in “Weekday Month Date” format (e.g., “Tue May 26”)
  • \D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
  • \e : an ASCII escape character (033)
  • \h : the hostname up to the first ‘.’
  • \H : the hostname
  • \j : the number of jobs currently managed by the shell
  • \l : the basename of the shell’s terminal device name
  • \n : newline
  • \r : carriage return
  • \s : the name of the shell, the basename of $0 (the portion following the final slash)
  • \t : the current time in 24-hour HH:MM:SS format
  • \T : the current time in 12-hour HH:MM:SS format
  • \@ : the current time in 12-hour am/pm format
  • \A : the current time in 24-hour HH:MM format
  • \u : the username of the current user
  • \v : the version of bash (e.g., 2.00)
  • \V : the release of bash, version + patch level (e.g., 2.00.0)
  • \w : the current working directory, with $HOME abbreviated with a tilde
  • \W : the basename of the current working directory, with $HOME abbreviated with a tilde
  • ! : the history number of this command
  • # : the command number of this command
  • \$ : if the effective UID is 0, a #, otherwise a $
  • \nnn : the character corresponding to the octal number nnn
  • \ : a backslash
  • [ : begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  • ] : end a sequence of non-printing characters

\[\e[F;Bm\] 其中F为字体颜色,编号30~37;B为背景色,编号40~47。

前景 背景 颜色
30 40 黑色
31 41 红色
32 42 绿色
33 43 黄色
34 44 蓝色
35 45 紫红色
36 46 青蓝色
37 47 白色
代码 意义
0 OFF
1 高亮显示
4 下划线
5 闪烁
7 反白显示
8 不可见

可通过\e[0m关闭颜色输出;特别的,当B为1时,将显示加亮加粗的文字。

References: http://www.phpvim.net/os/mac/git-branch-in-bash-prompt.html http://www.idb-stock.net/idb/2011/07/12/213.html http://www.thegeekstuff.com/2008/09/bash-shell-take-control-of-ps1-ps2-ps3-ps4-and-prompt_command/


Carpe diem,
JZR at 10:43

Home About GitHub