blob: 7d46f70f26675725388fb9f678b68cc647c752f7 (
plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
bindkey -e # emacs-style keybindings
setopt autocd
# last line without newline terminator
# is swallowed if this is absent
setopt prompt_sp
autoload -U zmv
setopt prompt_subst prompt_percent
autoload colors && colors
# wrap in %{%} to prevent zsh from counting the content towards
# the text length, leading to over-compensation for zero-width characters
zcol() {
echo -n "%{$@%}"
}
timestamp() {
echo $((`date "+%s + %N / 1e9"`))
}
elapsed() {
echo $(($(timestamp) - $command_start_time))
}
humanize_duration() {
# ORS= disables the trailing newline
command awk -v ORS= '
function hmTime(time, stamp) {
split("h:m:s:ms", units, ":")
for (i = 2; i >= -1; i--) {
if (t = int( i < 0 ? time % 1000 : time / (60 ^ i * 1000) % 60 )) {
stamp = stamp t units[sqrt((i - 2) ^ 2) + 1] " "
}
}
if (stamp ~ /^ *$/) {
return "0ms"
}
return substr(stamp, 1, length(stamp) - 1)
}
{ print hmTime($0) }
'
}
duration=0
command_start_time=$(timestamp)
# called just before line is evaluated
preexec() {
command_start_time=$(timestamp)
}
# called just before prompt is updated which
# is usually after the program ran, but also
# on ^C or Enter on an empty line
precmd() {
duration=$(elapsed)
command_start_time=$(timestamp)
}
git_prompt() {
local git_branch=$(git branch 2>/dev/null | grep \* | sed 's/* //')
# is in git repository if branch != ""
if [[ -n "$git_branch" ]]; then
# if no uncommitted changes
if git diff-index --quiet HEAD --; then
zcol "$fg[green]"
else
zcol "$fg[red]"
fi
echo -n "$git_branch "
fi
}
left_prompt() {
local last_status=$?
case $USER in
"$PRIMARY_USER" | "root") ;;
*) echo -n "$USER "
esac
if test -n "$SSH_TTY" \
|| [ -f /.dockerenv ]; then
zcol "$fg[cyan]"
echo -n "@$(hostname | tr -d '\n') "
fi
if test -n "$IN_NIX_SHELL"; then
zcol "$fg[cyan]"
echo -n "nix "
fi
if test -n "$IN_BWRAP"; then
zcol "$fg[cyan]"
echo -n "bwrap "
fi
if test -n "$DOCKER_HOST"; then
zcol "$fg[cyan]"
echo -n "$(basename "$DOCKER_HOST") "
fi
zcol "$fg[green]"
{ pwd; echo -n " " } | tr -d '\n' | sed -e "s:^$HOME:~:"
git_prompt
case $USER in
root) zcol "$fg[red]"; echo -n "# " ;;
*) zcol "$fg[blue]"; echo -n "> " ;;
esac
if [ "$last_status" -ne 0 ]; then
zcol "$fg_bold[red]"
echo -n "$last_status "
fi
zcol "$reset_color"
}
right_prompt() {
command_start_time=$(timestamp)
if (($duration > 0.99)); then
zcol "$fg_bold[cyan]"
humanize_duration <<<"$(( $duration * 1000 ))"
fi
zcol "$reset_color"
}
PROMPT='$(left_prompt)'
RPROMPT='$(right_prompt)'
bindkey "${terminfo[khome]}" beginning-of-line
bindkey "${terminfo[kend]}" end-of-line
bindkey '\eOA' history-substring-search-up # or ^[OA
bindkey '\eOB' history-substring-search-down # or ^[OB
export ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE=fg=cyan
|