LoginSignup
1
2

More than 5 years have passed since last update.

ホストごとに色が変わるプロンプト

Posted at

作業しているホストごとにプロンプトの色が変わったら、作業ミスも無くなりそうで便利ですよね。

そんな方は↓を bashrc にペタッと、どうぞ。

CentOS系で確認しているので、Ubuntu系とかで動かなかったらすみません。

##### For git completion and prompt ###########################################

GIT_PROMPT="$(find /usr/share/doc/ -type f -name git-prompt.sh 2> /dev/null | head -1)"
if [ -n "$GIT_PROMPT" ]; then
  source "$GIT_PROMPT"
fi

GIT_COMPLETION="$(find /usr/share/doc/ -type f -name git-completion.bash 2> /dev/null | head -1)"
if [ -n "$GIT_COMPLETION" ]; then
  source "$GIT_COMPLETION"
fi

# Unstaged (*) and staged (+) changes will be shown next to the branch name
GIT_PS1_SHOWDIRTYSTATE=true

# A "<" indicates you are behind, ">" indicates you are ahead,
# "<>" indicates you have diverged and "=" indicates that there is no difference.
GIT_PS1_SHOWUPSTREAM=auto

# If there're untracked files, then a '%' will be shown next to the branch name.
GIT_PS1_SHOWUNTRACKEDFILES=true

# If something is stashed, then a '$' will be shown next to the branch name.
GIT_PS1_SHOWSTASHSTATE=true

##### Set prompt ##############################################################

# Defined Colors
RED="\033[38;2;255;84;88m"
GREEN="\033[38;2;98;209;150m"
#YELLOW="\033[38;2;255;179;120m"
#BLUE="\033[38;2;101;178;255m"
#MAGENTA="\033[38;2;144;108;255m"
#CYAN="\033[38;2;99;242;241m"
CLEAR_COLOR="\033[0m"

# Hostname hash
HMD5="$(hostname | md5sum)"
PROMPT_RGB="$(printf "%d;%d;%d" 0x${HMD5:0:2} 0x${HMD5:2:2} 0x${HMD5:4:2})"
PROMPT_COLOR="\033[38;2;${PROMPT_RGB}m"
GIT_PROMPT_RGB="$(printf "%d;%d;%d" 0x${HMD5:6:2} 0x${HMD5:8:2} 0x${HMD5:10:2})"
GIT_PROMPT_COLOR="\033[38;2;${GIT_PROMPT_RGB}m"

# Set prompt
case ${UID} in
0)
  PS1="\[${RED}\]\${?##0}\[${PROMPT_COLOR}\][\u@\h \W]\[${GIT_PROMPT_COLOR}\]\$(__git_ps1)\[${RED}\]\\$\[${CLEAR_COLOR}\] "
  ;;
*)
  PS1="\[${RED}\]\${?##0}\[${PROMPT_COLOR}\][\u@\h \W]\[${GIT_PROMPT_COLOR}\]\$(__git_ps1)\[${GREEN}\]\\$\[${CLEAR_COLOR}\] "
  ;;
esac

前半部分は、gitを使っている方がいたら、ブランチ名とかがプロンプトに表示されるというところで、gitを使っていない方は無視してもらえれば良いかと思いますー。

色を変える、みそは後半のこのあたりです。

# Hostname hash
HMD5="$(hostname | md5sum)"
PROMPT_RGB="$(printf "%d;%d;%d" 0x${HMD5:0:2} 0x${HMD5:2:2} 0x${HMD5:4:2})"
PROMPT_COLOR="\033[38;2;${PROMPT_RGB}m"

これだと、完全にランダムな感じの色になるので、せめて青系には、したいという方がいたら、

PROMPT_RGB="$(printf "%d;%d;255" 0x${HMD5:0:2} 0x${HMD5:2:2})"

こんな感じで、青だけ 255 固定とかにしたらよいかもです。

追伸:

Defined Colorsのところは私の趣味なので、 RED じゃねぇぞごるぁー、とかはなしでおねがいします。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2