fnwiya's quine

自分自身を出力するブログ

zshプロンプトの設定

zshのプロンプトのスタイルようやく落ち着きました。
ディレクトリを右に持ってくるか左に持ってくるか悩むところですがいったん右にしてます。

gitのブランチがみれるのは地味に便利ですね。

########################################
# prompt
########################################
# Copyright 2011-2015 Kouhei Sutou <kou@clear-code.com>
# https://github.com/clear-code/zsh.d/blob/master/zshrc
# looks like bellow
# [name@hostname]                                                     [/your/dir]
# %                                                                  [git:branch]
prompt_bar_left="[%n@%m] "
prompt_bar_right=" [%d]"
prompt_left="%# " # 一般ユーザなら%/rootユーザなら#
autoload -Uz vcs_info
zstyle ':vcs_info:*' formats '[%s:%b]'

count_prompt_characters(){
    print -n -P -- "$1" | sed -e $'s/\e\[[0-9;]*m//g' | wc -m | sed -e 's/ //g'
}

update_prompt(){
    local bar_left_length=$(count_prompt_characters "$prompt_bar_left")
    local bar_rest_length=$[COLUMNS - bar_left_length]
    local bar_left="$prompt_bar_left"
    local bar_right_without_path="${prompt_bar_right:s/%d//}"
    local bar_right_without_path_length=$(count_prompt_characters "$bar_right_without_path")
    local max_path_length=$[bar_rest_length - bar_right_without_path_length]
    bar_right=${prompt_bar_right:s/%d/%(C,%${max_path_length}<...<%d%<<,)/}
    local separator="${(l:${bar_rest_length}:: :)}"
    bar_right="%${bar_rest_length}<<${separator}${bar_right}%<<"

    PROMPT="%F{green}${bar_left}${bar_right}%f"$'\n'"${prompt_left}"

    LANG=C vcs_info >&/dev/null
    if [ -n "$vcs_info_msg_0_" ]; then
        RPROMPT="%F{green}${vcs_info_msg_0_}%f"
    fi
}

precmd_functions=($precmd_functions update_prompt)