何をしたいか
- タイトルの通り、ぱっと見解りやすく色付けたい。
- 割とネタ。
理由と動機
- 間違ってrootで入ってたあああああとか
- 間違って本番機入ってたあああああうおおおrm -rfとかしちまったあああああとかなくすため。
設定
- ログインするサーバリストやユーザリストに応じていちいちコード変更しなくてもいいようにしてみた。
- bashとzshそれぞれのシェルプロンプト設定を紹介。
- .zshrcの設定
#color mapping
#_color_map=(${_color_map[*]} 0) # black
_color_map=(${_color_map[*]} 1) # red
_color_map=(${_color_map[*]} 2) # green
_color_map=(${_color_map[*]} 3) # yellow
_color_map=(${_color_map[*]} 4) # blue
_color_map=(${_color_map[*]} 5) # magenta
_color_map=(${_color_map[*]} 6) # cyan
_color_map=(${_color_map[*]} 7) # white
# attribute mapping
_attr_map=(${_attr_map[*]} 0) # normal
_attr_map=(${_attr_map[*]} 1) # bold
_attr_map=(${_attr_map[*]} 4) # under_score
#_attr_map=(${_attr_map[*]} 7) # reverse
# Coloring hostname and username
_cl_host_index=$(/usr/bin/python -c 'print hash("'$HOSTNAME'") % '${#_color_map[*]}'')
_cl_user_index=$(/usr/bin/python -c 'print hash("'$USER'") % '${#_color_map[*]}'')
_attr_host_index=$(/usr/bin/python -c 'print hash("'$HOSTNAME'") % '${#_attr_map[*]}'')
_attr_user_index=$(/usr/bin/python -c 'print hash("'$USER'") % '${#_attr_map[*]}'')
_colored_host=$(echo -e "%{\e[${_attr_map[${_attr_host_index}]};3${_color_map[${_cl_host_index}]}m%}%m%{\e[m%}")
_colored_user=$(echo -e "%{\e[${_attr_map[${_attr_user_index}]};3${_color_map[${_cl_user_index}]}m%}%n%{\e[m%}")
# prompt setting
PROMPT="[${_colored_user}@${_colored_host} %~]%# "
- 最後のprompt settingの所はお好みに応じて。
- bashの場合は、_colored_host以下を次の通りに。適当に.bashrcにでも追加。
_colored_host=$(echo -e "\e[${_attr_map[${_attr_host_index}]};3${_color_map[${_cl_host_index}]}m\h\e[m")
_colored_user=$(echo -e "\e[${_attr_map[${_attr_user_index}]};3${_color_map[${_cl_user_index}]}m\u\e[m")
# prompt setting
PS1="[${_colored_user}@${_colored_host} \w]\$ "
- 同じくPS1の設定は好みで。
説明
- ホスト名とユーザ名のハッシュコード値を使って、文字色と文字効果を組み合わせて特徴付けさせてます。
- 運が悪いと同じになりますがまぁ滅多にない程度の緩さ。
カスタマイズ
_color_mapと_attr_mapの定義部分で、
好みじゃない文字色や文字効果をコメントアウトすればそいつは出てこなくなります。
注意
- ピュアシェルじゃない。トテモカッコワルイ。
- 今やどこにでも入ってるであろうpythonを使ってる。ない環境だったらスミマセン。