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