-
Notifications
You must be signed in to change notification settings - Fork 1
/
prompt.zsh
85 lines (69 loc) · 2.15 KB
/
prompt.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Pure
# by Sindre Sorhus
# https://github.com/sindresorhus/pure
# MIT License
# Change this to your own username
DEFAULT_USERNAME='igel'
# Threshold (sec) for showing cmd exec time
CMD_MAX_EXEC_TIME=5
# For my own and others sanity
# git:
# %b => current branch
# %a => current action (rebase/merge)
# prompt:
# %F => color dict
# %f => reset color
# %K => background color
# %k => reset background
# %B => Bold
# %b => no bold - resets background color :'(
# %~ => current path
# %* => time
# %n => username
# %m => shortname host
# %(?..) => prompt conditional - %(condition.true.false)
autoload -Uz vcs_info
autoload -Uz add-zsh-hook
zstyle ':vcs_info:*' enable git # You can add hg too if needed: `git hg`
zstyle ':vcs_info:git*' formats ' %F{9}%f%b'
zstyle ':vcs_info:git*' actionformats ' %B%F{red}%a %%b%K{0}%F{9}%f%b'
# enable prompt substitution
setopt PROMPT_SUBST
# Only show username if not default
[ $USER != $DEFAULT_USERNAME ] && local username='%n@%m '
# Fastest possible way to check if repo is dirty
git_dirty() {
# check if we're in a git repo
command git rev-parse --is-inside-work-tree &>/dev/null || return
# check if it's dirty
command git diff --quiet --ignore-submodules HEAD &>/dev/null; [ $? -eq 1 ] && echo '%F{9}±%F{white}'
}
# Displays the exec time of the last command if set threshold was exceeded
cmd_exec_time() {
local stop=`date +%s`
local start=${cmd_timestamp:-$stop}
let local elapsed=$stop-$start
[ $elapsed -gt $CMD_MAX_EXEC_TIME ] && print_time ${elapsed}
}
print_time() {
((h=${1}/3600))
((m=(${1}%3600)/60))
((s=${1}%60))
printf "took %02d:%02d:%02d\n" $h $m $s
}
record_start_time() {
cmd_timestamp=`date +%s`
}
add-zsh-hook preexec record_start_time
print_prompt() {
vcs_info
# Add `%*` to display the time
print -P '%F{white}%K{10}%~%K{0}%F{10}%F{white}$vcs_info_msg_0_`git_dirty` %k%F{0}%f$username%f %F{yellow}`cmd_exec_time`%f %*'
# Reset value since `preexec` isn't always triggered
unset cmd_timestamp
}
add-zsh-hook precmd print_prompt
# Prompt turns red if the previous command didn't exit with 0
PROMPT='%(?.%F{green}.%F{red})❯%f '
# Can be disabled:
# PROMPT='%F{magenta}❯%f '