Skip to content

Instantly share code, notes, and snippets.

@shimesaba-type0
Last active August 4, 2019 17:03
Show Gist options
  • Save shimesaba-type0/09669df18707aa4b60c1ef786bc2a728 to your computer and use it in GitHub Desktop.
Save shimesaba-type0/09669df18707aa4b60c1ef786bc2a728 to your computer and use it in GitHub Desktop.
Bash Prompt 作った。必要になったら、git status 系を入れる。

Bash Prompt setting

for macOS

完成イメージ

image

.bashrc

####################
# prompt setting
####################
PROMPT_COMMAND=""
C() {
  case $1 in
    black)    echo -e -n "\033[1;30m";;
    red)      echo -e -n "\033[1;31m";;
    green)    echo -e -n "\033[1;32m";;
    yellow)   echo -e -n "\033[1;33m";;
    blue)     echo -e -n "\033[1;34m";;
    magenta)  echo -e -n "\033[1;35m";;
    cyan)     echo -e -n "\033[1;36m";;
    white)    echo -e -n "\033[1;37m";;
    white-black)    echo -e -n "\033[1;30;40m";;
    blue-white)     echo -e -n "\033[1;34;47m";;
    *) echo -e -n "\033[0m";;
  esac
}

# usename@hostname
ps1_login() {
  echo -n "${USER}@"
  echo -n -e "$(hostname -s)"
}

# git status
branch() {
  local branch
  branch="$(git branch 2>/dev/null | grep '^\*' | sed -e "s/^* //")"
  if [[ "${branch}" =~ ^tmp ]]; then
    C magenta
  elif [[ "${branch}" =~ ^develop$ ]]; then
    C red
  elif [[ "${branch}" =~ ^issue ]]; then
    C cyan
  elif [[ "${branch}" = "(detached from hde/master)" ]]; then
    C yellow
  else
    C white
  fi
  echo -n "${branch}"
  C reset
}

git_hash() {
  C green
  git log --pretty=format:'%h' -n 1
  C reset
}

commit_date() {
  git log --pretty=format:'%cd' --date=format:'%Y-%m-%d %H:%M:%S' -n 1
}


git_ps() {
  echo ''
  if ! git status --ignore-submodules &>/dev/null; then
    echo -n "$(ps1_login)"
    return
  fi
  echo -n "$(ps1_login)"
  echo -n " [$(branch):$(git_hash):$(commit_date)]"
}

# PROMPT_COMMAND 環境変数は、プロンプトが呼ばれるたびに実行される
PROMPT_COMMAND='git_ps'
# PS1 のフォーマットを定義する。
PS1="\n\D{%F %T} \w\n\[$(C cyan)\]\$\[$(C reset)\] "
# PS1 環境変数は、プロンプトの書式を定義する。
export PS1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment