问题描述
想让bash默认就能显示出conda当前环境,git分支信息和fish里面的路径折叠。不想用zsh因为慢,不想用fish因为怕兼容性出问题。
修改前终端显示是这样的:
因为我用tmux,所以主机名和用户名对我没啥意义,而且这么长的路径个人觉得很占空间,毕竟要路径都直接pwd
。我最想要的是conda环境信息和git分支信息。
修改之后终端变成了这样:
终端仅仅显示conda信息,分支信息,路径只显示首字母。分支绿色代表工作区干净,红色表示不干净,黄色表示本地分支领先远程分支。若是HEAD处于游离态,那么不显示分支名,直接显示HEAD指向的快照
解决方案
先把以下文件内容保存成一个脚本,比如叫ps.sh
:
#!/bin/bash
COLOR_RED="\033[0;31m"
COLOR_PINK="\033[38;5;211m"
COLOR_YELLOW="\033[0;33m"
COLOR_GREEN="\033[0;32m"
COLOR_OCHRE="\033[38;5;95m"
COLOR_BLUE="\033[0;34m"
COLOR_WHITE="\033[0;37m"
COLOR_RESET="\033[0m"
# 系统语言是英文用这个
#function git_color() {
# local git_status="$(git status 2> /dev/null)"
#
# if [[ ! $git_status =~ "working tree clean" ]]; then
# echo -e $COLOR_RED
# elif [[ $git_status =~ "Your branch is ahead of" ]]; then
# echo -e $COLOR_YELLOW
# elif [[ $git_status =~ "nothing to commit" ]]; then
# echo -e $COLOR_GREEN
# else
# echo -e $COLOR_OCHRE
# fi
#}
#function git_branch() {
# local git_status="$(git status 2> /dev/null)"
# local on_branch="On branch ([^${IFS}]*)"
# local on_commit="HEAD detached at ([^${IFS}]*)"
#
# if [[ $git_status =~ $on_branch ]]; then
# local branch=${BASH_REMATCH[1]}
# echo "($branch)"
# elif [[ $git_status =~ $on_commit ]]; then
# local commit=${BASH_REMATCH[1]}
# echo "($commit)"
# fi
#}
function git_branch() {
local git_status="$(git status 2> /dev/null)"
local on_branch="位于分支 ([^${IFS}]*)"
local on_commit="HEAD 目前位于 ([^${IFS}]*)"
local on_detach="头指针分离于 ([^${IFS}]*)"
if [[ $git_status =~ $on_branch ]]; then
local branch=${BASH_REMATCH[1]}
echo "($branch)"
elif [[ $git_status =~ $on_commit ]]; then
local commit=${BASH_REMATCH[1]}
echo "($commit)"
elif [[ $git_status =~ $on_detach ]]; then
local commit=${BASH_REMATCH[1]}
echo "($commit)"
fi
}
function git_color() {
local git_status="$(git status 2> /dev/null)"
if [[ ! $git_status =~ "干净的工作区" ]]; then
echo -e $COLOR_RED
elif [[ $git_status =~ "您的分支领先" ]]; then
echo -e $COLOR_YELLOW
elif [[ $git_status =~ "无文件要提交" ]]; then
echo -e $COLOR_GREEN
else
echo -e $COLOR_OCHRE
fi
}
# >>> 修改终端显示 <<<-----------------------------------------------------------------------------------------------
# 缩短路径显示
function shortwd() {
num_dirs=3
pwd_symbol="..."
newPWD="${PWD/#$HOME/~}"
if [ $(echo -n $newPWD | awk -F '/' '{print NF}') -gt $num_dirs ]; then
newPWD=$(echo -n $newPWD | awk -F '/' '{print $1 "/.../" $(NF-1) "/" $(NF)}')
fi
echo -n $newPWD
}
function _fish_collapsed_pwd() {
local pwd="$1"
local home="$HOME"
local size=${#home}
[[ $# == 0 ]] && pwd="$PWD"
[[ -z "$pwd" ]] && return
if [[ "$pwd" == "/" ]]; then
echo "/"
return
elif [[ "$pwd" == "$home" ]]; then
echo "~"
return
fi
[[ "$pwd" == "$home/"* ]] && pwd="~${pwd:$size}"
if [[ -n "$BASH_VERSION" ]]; then
local IFS="/"
local elements=($pwd)
local length=${#elements[@]}
for ((i=0;i<length-1;i++)); do
local elem=${elements[$i]}
if [[ ${#elem} -gt 1 ]]; then
elements[$i]=${elem:0:1}
fi
done
else
local elements=("${(s:/:)pwd}")
local length=${#elements}
for i in {1..$((length-1))}; do
local elem=${elements[$i]}
if [[ ${#elem} > 1 ]]; then
elements[$i]=${elem[1]}
fi
done
fi
local IFS="/"
echo "${elements[*]}"
}
#export current_conda_env=$CONDA_PROMPT_MODIFIER
# >>> 修改终端显示 <<<-------------------------------------------------------------------------------------------------
function changeps(){
PS1="\[$COLOR_PINK\]\$(_fish_collapsed_pwd)"
PS1+="\[\$(git_color)\]"
PS1+=" \$(git_branch)"
PS1+="\[$COLOR_BLUE\]\$\[$COLOR_RESET\] "
export PS1
conda activate base
}
然后在.bashrc
文件里加上
source ps.sh路径
然后source .bashrc
现在你想要更改之后的终端样子,执行changeps
就行,想恢复就source .bashrc
ok
参考
- https://coderwall.com/p/pn8f0g/show-your-git-status-and-branch-in-color-at-the-command-prompt