Permalink
Browse files

loading new prompt

  • Loading branch information...
1 parent d4fbce1 commit df6673b4d8a732916a607a384ab17b5ce8e1f2c1 @spf13 committed Apr 29, 2011
Showing with 102 additions and 41 deletions.
  1. +102 −41 environment.conf
View
@@ -5,52 +5,113 @@ export RSYNC_RSH="ssh"
export PYTHONSTARTUP="$HOME/.pythonrc.py"
export GREP_OPTIONS='--color=auto'
export GREP_COLOR='1;32'
-export CLICOLOR='yes'
-# Returns 0 (success) if the pwd is tracked, otherwise 1 (failure).
-git_pwd_is_tracked() {
- [ $(git log -1 --pretty=oneline . 2> /dev/null | wc -l) -eq "1" ]
-}
+__gps1() {
+ local _tab_title
+ local _pretty_pwd
+ local _project
+ local _ref
+ local _stat_flags
+ local _base_dir
+ local _sub_dir
+ local _stat_color
-# Emits the current time in 24-hr notation.
-show_time() {
- echo "${COLOR_GRAY}$(date +%H:%M)${COLOR_NONE}"
-}
+ # ansi terminal colors
+ if [ "$TERM" != "dumb" ]; then
+ local bold=$(tput bold)
+ local normal=$(tput sgr0)
-# Stores the exit status of the last command for use by show_exit_status function.
-if [[ ! $PROMPT_COMMAND =~ store_exit_status ]]; then
- export PROMPT_COMMAND="store_exit_status && ${PROMPT_COMMAND:-:}"
-fi
-store_exit_status() {
- LAST_EXIT_STATUS=$?
-}
+ local red=$(tput setaf 1)
+ local green=$(tput setaf 2)
+ local yellow=$(tput setaf 3)
+ local blue=$(tput setaf 4)
+ local magenta=$(tput setaf 5)
+ local cyan=$(tput setaf 6)
+ local white=$(tput setaf 7)
-# Emits exit status of last command if error condition returned.
-show_exit_status() {
- if [ "x${LAST_EXIT_STATUS}" != "x0" ]; then
- echo " [${COLOR_RED}${LAST_EXIT_STATUS}${COLOR_NONE}]"
- fi
-}
+ local RED=${bold}${red}
+ local GREEN=${bold}${green}
+ local YELLOW=${bold}${yellow}
+ local BLUE=${bold}${blue}
+ local MAGENTA=${bold}${magenta}
+ local CYAN=${bold}${cyan}
+ local WHITE=${bold}${white}
+ fi
-# Emits the current git branch and marker if there are outstanding changes.
-show_git_branch_and_status() {
- if git_pwd_is_tracked; then
- local branch_prompt
- branch_prompt=$(__git_ps1 " (${COLOR_YELLOW}%s${COLOR_NONE})")
- if [[ -n "$branch_prompt" ]]; then
- echo "$branch_prompt $(show_git_status)"
- fi
- fi
-}
+ __prompt_command() {
+ local last_command
-# Removes display of git status from prompt, useful for large repositories.
-hide_git_status() {
- show_git_branch_and_status() { exit; }
-}
+ function parse_git_dirty {
+ git diff --no-ext-diff --quiet --exit-code > /dev/null 2>&1 || echo "*"
+ }
+ function parse_git_staged {
+ git diff --cached --no-ext-diff --quiet --exit-code > /dev/null 2>&1 || echo "~"
+ }
+ function parse_git_ahead {
+ git branch -v 2> /dev/null | grep -Eo '^\* [^ ]* *[^ ]* *\[[^]]*\]' | grep -Eo '\[[^]]*\]$' | awk 'ORS="";/ahead/ {print "+"} /behind/ {print "-"}'
+ }
+ function parse_git_stashed {
+ git rev-parse --verify refs/stash >/dev/null 2>&1 && echo "$"
+ }
+ function parse_git_branch {
+ # __git_ps1 "%s"
+ git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"
+ }
-# Emits a red ✗ if current repository is 'dirty'.
-show_git_status() {
- [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "${COLOR_RED}変${COLOR_NONE}"
-}
+ last_command=$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g")
+
+ if [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
+
+ # find the parent dir and project name
+ _base_dir=$(git rev-parse --show-toplevel 2>/dev/null)
+ _project=$(basename "${_base_dir}")
+ _base_dir="${_base_dir%$_project}"
+ _base_dir="${_base_dir/$HOME/~}"
+
+ # find the path relative to project root
+ _sub_dir=$(git rev-parse --show-prefix)
+ _sub_dir="${_sub_dir%/}"
+ [[ -n $_sub_dir ]] && _sub_dir="/$_sub_dir"
-export PS1="\n"'$(show_time) $(prompt_color)'"\u@\h${COLOR_NONE}: ${COLOR_CYAN}\w${COLOR_NONE}"'$(show_exit_status)$(show_git_branch_and_status)'"\n> "
+ # get the current branch
+ _ref=$(parse_git_branch)
+ [[ "$_ref" = '(no branch)' ]] && _ref="$(git rev-parse --short HEAD)…"
+
+ # save these for later
+ local _st_dirty="$(parse_git_dirty)"
+ local _st_staged="$(parse_git_staged)"
+ local _st_ahead="$(parse_git_ahead)"
+ local _st_stashed="$(parse_git_stashed)"
+
+ _stat_flags="${_st_dirty}${_st_staged}${_st_ahead}${_st_stashed}"
+ [[ -n "$_stat_flags" ]] && _stat_flags=" $_stat_flags"
+
+ if [ -n "$_st_dirty" ] || [ -n "$_st_staged" ]; then
+ _stat_color="${RED}"
+ elif [ -n "$_st_stashed" ]; then
+ _stat_color="${YELLOW}"
+ else
+ _stat_color="${green}"
+ fi
+ PS1="\[${TAB_TITLE}\]${PS0/\\W/$GPS1}"
+
+ _tab_title="$_project:[$last_command]"
+ else
+ PS1="\[${TAB_TITLE}\]${PS0}"
+ _tab_title="[$last_command]"
+ fi
+
+ _pretty_pwd="${PWD/$HOME/~}"
+ }
+
+ # Store the old one for later
+ [ -z "$PS0" ] && PS0=$PS1
+
+ # Add some awesomeness here.
+ PROMPT_COMMAND=__prompt_command
+
+# TAB_TITLE='\e]2;\h::${_pretty_pwd}\a\e]1;${_tab_title}\a'
+ TAB_TITLE=''
+ GPS1='\[${blue}\]${_base_dir}\[${WHITE}\]${_project}\[${normal}\][\[${_stat_color}\]${_ref}${_stat_flags}\[${normal}\]]\[${blue}\]${_sub_dir}\[${normal}\]'
+}
+__gps1

0 comments on commit df6673b

Please sign in to comment.