gallois.zsh-theme 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Depends on the git plugin for work_in_progress()
  2. (( $+functions[work_in_progress] )) || work_in_progress() {}
  3. ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[cyan]%}[%{$fg[green]%}"
  4. ZSH_THEME_GIT_PROMPT_SUFFIX="%{$fg[cyan]%}]"
  5. ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}"
  6. ZSH_THEME_GIT_PROMPT_CLEAN=""
  7. # Customized git status, oh-my-zsh currently does not allow render dirty status before branch
  8. git_custom_status() {
  9. local branch=$(git_current_branch)
  10. [[ -n "$branch" ]] || return 0
  11. print "%{${fg_bold[yellow]}%}$(work_in_progress)%{$reset_color%}\
  12. ${ZSH_THEME_GIT_PROMPT_PREFIX}$(parse_git_dirty)${branch}\
  13. ${ZSH_THEME_GIT_PROMPT_SUFFIX}"
  14. }
  15. autoload -U colors && colors
  16. #export VCS_PROMPT=hg_prompt_info
  17. export VCS_PROMPT=git_custom_status
  18. base_prompt="%{$fg[cyan]%}[%~% ]%(?.%{$fg[green]%}.%{$fg[red]%})%B$%b "
  19. custom_prompt=""
  20. last_run_time=""
  21. last_vcs_info=""
  22. function pipestatus_parse {
  23. PIPESTATUS="$pipestatus"
  24. ERROR=0
  25. for i in "${(z)PIPESTATUS}"; do
  26. if [[ "$i" -ne 0 ]]; then
  27. ERROR=1
  28. fi
  29. done
  30. if [[ "$ERROR" -ne 0 ]]; then
  31. print "[%{$fg[red]%}$PIPESTATUS%{$fg[cyan]%}]"
  32. fi
  33. }
  34. # Combine it all into a final right-side prompt
  35. PROMPT='%{$fg[cyan]%}[%~% ]%(?.%{$fg[green]%}.%{$fg[red]%})%B$%b '
  36. function preexec() {
  37. last_run_time=$(perl -MTime::HiRes=time -e 'printf "%.9f\n", time')
  38. }
  39. function duration() {
  40. local duration
  41. local now=$(perl -MTime::HiRes=time -e 'printf "%.9f\n", time')
  42. local last=$1
  43. local last_split=("${(@s/./)last}")
  44. local now_split=("${(@s/./)now}")
  45. local T=$((now_split[1] - last_split[1]))
  46. local D=$((T/60/60/24))
  47. local H=$((T/60/60%24))
  48. local M=$((T/60%60))
  49. local S=$((T%60))
  50. local s=$(((now_split[2] - last_split[2]) / 1000000000.))
  51. local m=$(((now_split[2] - last_split[2]) / 1000000.))
  52. (( $D > 0 )) && duration+="${D}d"
  53. (( $H > 0 )) && duration+="${H}h"
  54. (( $M > 0 )) && duration+="${M}m"
  55. if [[ $S -le 0 ]]; then
  56. printf "%ims" "$m"
  57. else
  58. if ! [[ -z $duration ]] && printf "%s" "$duration"
  59. local sec_milli=$((S + s))
  60. printf "%.3fs" "$sec_milli"
  61. fi
  62. }
  63. function precmd() {
  64. RETVAL=$(pipestatus_parse)
  65. local info=""
  66. if [ ! -z "$last_run_time" ]; then
  67. local elapsed=$(duration $last_run_time)
  68. last_run_time=$(print $last_run_time | tr -d ".")
  69. if [ $(( $(perl -MTime::HiRes=time -e 'printf "%.9f\n", time' | tr -d ".") - $last_run_time )) -gt $(( 120 * 1000 * 1000 * 1000 )) ]; then
  70. local elapsed_color="%{$fg[magenta]%}"
  71. elif [ $(( $(perl -MTime::HiRes=time -e 'printf "%.9f\n", time' | tr -d ".") - $last_run_time )) -gt $(( 60 * 1000 * 1000 * 1000 )) ]; then
  72. local elapsed_color="%{$fg[red]%}"
  73. elif [ $(( $(perl -MTime::HiRes=time -e 'printf "%.9f\n", time' | tr -d ".") - $last_run_time )) -gt $(( 10 * 1000 * 1000 * 1000 )) ]; then
  74. local elapsed_color="%{$fg[yellow]%}"
  75. else
  76. local elapsed_color="%{$fg[green]%}"
  77. fi
  78. info=$(printf "%s%s%s%s%s" "%{$fg[cyan]%}[" "$elapsed_color" "$elapsed" "%{$fg[cyan]%}]" "$RETVAL")
  79. unset last_run_time
  80. fi
  81. if [ -z "$info" -a ! -z "$last_vcs_info" ]; then
  82. custom_prompt="$last_vcs_info$base_prompt"
  83. return;
  84. fi
  85. if (( ${+VCS_PROMPT} )); then
  86. last_vcs_info=$($VCS_PROMPT)
  87. if [ ! -z "$last_vcs_info" ]; then
  88. [ -z "$info" ] && info=$last_vcs_info || info="$info$last_vcs_info"
  89. fi
  90. fi
  91. [ -z "$info" ] && custom_prompt="$base_prompt" || custom_prompt="$info$base_prompt"
  92. }
  93. function hg_prompt_info() {
  94. unset output info parts branch_parts branch
  95. local output=""
  96. if ! output="$(hg status 2> /dev/null)"; then
  97. return
  98. fi
  99. local info=$(hg log -l1 --template '{author}:{node|short}:{remotenames}:{phabdiff}')
  100. local parts=(${(@s/:/)info})
  101. local branch_parts=(${(@s,/,)parts[3]})
  102. local branch=${branch_parts[-1]}
  103. [ ! -z "${parts[3]}" ] && [[ "${parts[1]}" =~ "$USER@" ]] && branch=${parts[3]}
  104. [ -z "${parts[3]}" ] && branch=${parts[2]}
  105. if [[ ! -z "$output" ]]; then
  106. local color="%{$fg[red]%}"
  107. elif [[ "${branch}" == "master" || "${branch}" == "warm" ]]; then
  108. local color="%{$fg[yellow]%}"
  109. else
  110. local color="%{$fg[green]%}"
  111. fi
  112. print "%{$fg[cyan]%}[${color}${branch}%{$fg[cyan]%}]"
  113. }
  114. setopt PROMPT_SUBST
  115. PROMPT='$custom_prompt'