LinuxMacWSL

zshのプロンプトをカッコよくしてGitのブランチを表示させる

zshのプロンプトがデフォルトのままじゃ見にくいのでカスタマイズします.
ついでにGitのブランチも表示できるようにします.
動作が重くならないように,プラグインなどは使用しません.
ターミナルのフォントはパワーライン用のフォントに設定してください.
パワーライン用のフォントじゃないと正しく表示されません.
インストール方法はhttps://tomiylab.com/2020/03/font-powerline/を見てください.

プロンプト

下の画像のようにプロンプトを変更します.
1行目はmacOS Catalinaで自動的に表示されるものです.
2行目のhello.は初代MacintoshのKeynoteでのデモの
“Hello, I’m Macintosh. It sure is great to get out of that bag. Unaccustomed as I am to public speaking, I’d like to share with you a maxim I thought of the first time I met an IBM mainframe: NEVER TRUST A COMPUTER YOU CAN’T LIFT.”
からです.
プロンプトを2行にしてpathが長くなっても見やすいようにしています.
また,コマンドの実行後に自動的に改行することで,見やすくしています.

.zshrc

以下を~/.zshrcに追加してください.

export CLICOLOR=1

echo hello.

autoload -Uz compinit && compinit  # Gitの補完を有効化

function left-prompt {
  name_t='179m%}'      # user name text clolr
  name_b='000m%}'    # user name background color
  path_t='255m%}'     # path text clolr
  path_b='031m%}'   # path background color
  arrow='087m%}'   # arrow color
  text_color='%{\e[38;5;'    # set text color
  back_color='%{\e[30;48;5;' # set background color
  reset='%{\e[0m%}'   # reset
  sharp='\uE0B0'      # triangle
  
  user="${back_color}${name_b}${text_color}${name_t}"
  dir="${back_color}${path_b}${text_color}${path_t}"
  echo "${user}%n%#@%m${back_color}${path_b}${text_color}${name_b}${sharp} ${dir}%~${reset}${text_color}${path_b}${sharp}${reset}\n${text_color}${arrow}→ ${reset}"
}

PROMPT=`left-prompt` 

# コマンドの実行ごとに改行
function precmd() {
    # Print a newline before the prompt, unless it's the
    # first prompt in the process.
    if [ -z "$NEW_LINE_BEFORE_PROMPT" ]; then
        NEW_LINE_BEFORE_PROMPT=1
    elif [ "$NEW_LINE_BEFORE_PROMPT" -eq 1 ]; then
        echo ""
    fi
}

解説

プロンプト

1行目はプロンプトとは関係ありませんがlsを実行した時にファイル名を色分けする設定です.
3行目でターミナルの起動直後にhello.と表示しています.
”.zshrc”はzshを起動した直後に読み込まれるのでここにコマンドを書いておくとターミナルの起動直後に一度だけコマンドを実行できます.
echoは文字列を表示するコマンドです.
7から23行目で関数を宣言しています.
8から12行目は色の指定です.
13,14行目は色の指定用の変数です.
15行目は色設定をリセットする変数です.
16行目は三角形を表示する変数です.
18,19行目はユーザー名とディレクトリの表示設定をしています.
色は’○○○m%}’の○○○の数字を変えると変更できます.
数字と色の対応は以下のコマンドで調べることができます.

for c in {000..255}; do echo -n "\e[38;5;${c}m $c" ; [ $(($c%16)) -eq 15 ] && echo;done;echo

色を指定するには本来は以下の1行目のように設定しますが,色の変更を行いやすいように
2から4行目のように書きました.

%{\e[38;5;179m%}  # text colorを179に設定
name_t='179m%}' 
text_color='%{\e[38;5;'
${text_color}${name_t}  # text colorを179に設定

自動改行

28から36行目はprecmdというコマンドが実行されるごとに実行される関数を宣言しています.
ここでコマンドの実行後に改行する設定をしています.
“.zshrc”を読み込んだ直後は改行しないようにしています.

Gitのブランチを表示

下の画像のようにGitで管理されているディレクトリに移動した時にブランチ名と状態をターミナルの右側に表示するようにします.

.zshrc

# git ブランチ名を色付きで表示させるメソッド
function rprompt-git-current-branch {
  local branch_name st branch_status
  
  branch='\ue0a0'
  color='%{\e[38;5;' #  文字色を設定
  green='114m%}'
  red='001m%}'
  yellow='227m%}'
  blue='033m%}'
  reset='%{\e[0m%}'   # reset
  
  if [ ! -e  ".git" ]; then
    # git 管理されていないディレクトリは何も返さない
    return
  fi
  branch_name=`git rev-parse --abbrev-ref HEAD 2> /dev/null`
  st=`git status 2> /dev/null`
  if [[ -n `echo "$st" | grep "^nothing to"` ]]; then
    # 全て commit されてクリーンな状態
    branch_status="${color}${green}${branch}"
  elif [[ -n `echo "$st" | grep "^Untracked files"` ]]; then
    # git 管理されていないファイルがある状態
    branch_status="${color}${red}${branch}?"
  elif [[ -n `echo "$st" | grep "^Changes not staged for commit"` ]]; then
    # git add されていないファイルがある状態
    branch_status="${color}${red}${branch}+"
  elif [[ -n `echo "$st" | grep "^Changes to be committed"` ]]; then
    # git commit されていないファイルがある状態
    branch_status="${color}${yellow}${branch}!"
  elif [[ -n `echo "$st" | grep "^rebase in progress"` ]]; then
    # コンフリクトが起こった状態
    echo "${color}${red}${branch}!(no branch)${reset}"
    return
  else
    # 上記以外の状態の場合
    branch_status="${color}${blue}${branch}"
  fi
  # ブランチ名を色付きで表示する
  echo "${branch_status}$branch_name${reset}"
}
 
# プロンプトが表示されるたびにプロンプト文字列を評価、置換する
setopt prompt_subst
 
# プロンプトの右側にメソッドの結果を表示させる
RPROMPT='`rprompt-git-current-branch`'

解説

ブランチ名の色と記号は以下のようになっています.

表示がうまいくかなかったら

UbuntuとWindowsのWSLで試したところ以下のように,pathの後についている三角形が途切れる,
ブランチマークとテキストが重なってしまう,という現象がありました.

Ubuntu

WSL

以下のようにスペースを入れると解消できます.

# pathの後の三角形の途切れを修正
# 2つ目の${sharpe}の後にスペースを入れる
echo"${user}%n%#@%m${back_color}${path_b}${text_color}${name_b}${sharp} ${dir}%~${reset}${text_color}${path_b}${sharp} ${reset}\n${text_color}${arrow}→ ${reset}"
# ブランチマーク
# ${branch}の後にスーペースを入れる
if [ ! -e  ".git" ]; then
    # git 管理されていないディレクトリは何も返さない
    return
  fi
  branch_name=`git rev-parse --abbrev-ref HEAD 2> /dev/null`
  st=`git status 2> /dev/null`
  if [[ -n `echo "$st" | grep "^nothing to"` ]]; then
    # 全て commit されてクリーンな状態
    branch_status="${color}${green}${branch} "
  elif [[ -n `echo "$st" | grep "^Untracked files"` ]]; then
    # git 管理されていないファイルがある状態
    branch_status="${color}${red}${branch} ?"
  elif [[ -n `echo "$st" | grep "^Changes not staged for commit"` ]]; then
    # git add されていないファイルがある状態
    branch_status="${color}${red}${branch} +"
  elif [[ -n `echo "$st" | grep "^Changes to be committed"` ]]; then
    # git commit されていないファイルがある状態
    branch_status="${color}${yellow}${branch} !"
  elif [[ -n `echo "$st" | grep "^rebase in progress"` ]]; then
    # コンフリクトが起こった状態
    echo "${color}${red}${branch} !(no branch)${reset}"
    return
  else
    # 上記以外の状態の場合
    branch_status="${color}${blue}${branch}"
  fi

コメント

タイトルとURLをコピーしました