美文网首页
在Ubuntu终端中仿MacOS的zsh主题的简单实现

在Ubuntu终端中仿MacOS的zsh主题的简单实现

作者: Gavinxixi臻 | 来源:发表于2018-12-18 19:14 被阅读0次

2020.01 注:
都2020年了,还不知道oh-my-zsh么,赶紧关掉这篇文章去用吧
https://ohmyz.sh/

一直羡慕macos的终端有漂亮的zsh主题,最近自己也捣鼓捣鼓,在Ubuntu上做个山寨版的~
原理是利用shell输出带样式的字符,以及修改提示符的PS1实现。

山寨的效果图 详细说明
  • 显示当前所在的branch,若不是git的版本库,则显示$ 结束
  • 直观地显示当前仓库状态 [干净] [新建] [变更] [暂存] [冲突],并使用不同样式标识
  • 显示HEAD的短SHA和首行信息,不用老是git log来去翻查HEAD信息了

代码就不详述了,在~/.bashrc上找对应的段落按自己的修改就行。

  • 修改force_color_prompt=yes
  • 根据自己的git版本选择中文还是英文的提示函数
  • 去掉原来自带的$结尾符号
  • 记得在git ignore文件中加入 .IS_GIT_DIR_GAVIN 文件,不然可能会出现错误的新建状态

20181220更新:优化git样式化PS1

  • 使用换行符规避xterm覆盖输入字符问题(本质上是xterm对PS1中echo输出的非显示字符的计算错误)
  • 使用存储字符串的方式减少每次执行git命令的次数
  • 增加对HEAD信息的输出
  • 优化显示效果的样式和status的提示
  • 英文版的没有修改,请自行修改
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi
function get_git_head_now {
    if [ `cat .IS_GIT_DIR_GAVIN` != 1 ]; then
        rm -rf .IS_GIT_DIR_GAVIN
        echo -e "$ " && return 1
    fi
    rm -rf .IS_GIT_DIR_GAVIN
    STR_head=`git log HEAD -n 1 --oneline 2>&1`
    #echo "$STR_head" | grep -q "fatal: Not a git repository" && return 1                #不是git仓库
    echo -e "\033[45;35m[\033[37;1m${STR_head%% *}\033[0;45;35m]\033[49m▶\033[1;33m ${STR_head#*:}\033[0m"
    return 0
}

function get_git_branch_now  {
    #echo -e '\033[34m test \033[0m$ \n'
    if [ `cat .IS_GIT_DIR_GAVIN` != 1 ]; then
        return 1
    fi
    #git branch 2>&1 | grep -q "fatal: Not a git repository" && echo -e "$ " && return 1
    git branch --no-color 2> /dev/null | awk -F '(|)| ' '/\*/{printf $(NF-1)" "$NF" " }' | awk -F '[ ]+' '{printf "\033[44;34m[\033[37;1m"$(NF-1)"\033[0;44;34m]\033[49m◣\033[0m" }'
    return 0
}
# git color 中文 
function get_git_status_now {
    STR_status=`git status 2>&1`
    echo "0" > .IS_GIT_DIR_GAVIN
    echo "$STR_status" | grep -q "fatal: " && return 1                #不是git仓库
    echo "1" > .IS_GIT_DIR_GAVIN
    echo "$STR_status" | grep -q "未合并的路径" && echo -e '\033[41;37m[冲突]\033[0m' &&  return 0                #合并冲突状态
    echo "$STR_status" | grep -qE "尚未暂存以备提交的变更|Changed but not updated" && echo -e '\033[31m[变更]\033[0m' &&  return 0   #未暂存状态
    echo "$STR_status" | grep -q "要提交的变更" && echo -e '\033[33m[暂存]\033[0m' &&  return 0  #未提交状态
    echo "$STR_status" | grep -q "未跟踪的文件" && echo -e '\033[35m[新建]\033[0m' &&  return 0  #有新建文件或目录
    echo -e '\033[32m[干净]\033[0m'
    return 0
}
# git color English 
# function get_git_status_now {
#   git status 2> /dev/null | grep -q "Unmerged paths" && echo -e '\033[41;37m*\033[0m' &&  return 0                #合并冲突状态
#   git status 2> /dev/null | grep -qE "Changes not staged for commit|Changed but not updated" && echo -e '\033[31m*\033[0m' &&  return 0   #未暂存状态
#   git status 2> /dev/null | grep -q "Changes to be committed" && echo -e '\033[33m*\033[0m' &&  return 0  #未提交状态
# }

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]'
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w'
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# for git color display
#PS1="$PS1\$ "
PS1="$PS1\$(get_git_status_now)\$(get_git_branch_now)\$(get_git_head_now)\n> "
# 加一个换行符规避终端不换行问题

相关文章

网友评论

      本文标题:在Ubuntu终端中仿MacOS的zsh主题的简单实现

      本文链接:https://www.haomeiwen.com/subject/nzzlkqtx.html