-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpronto.zsh
133 lines (116 loc) · 3.36 KB
/
pronto.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
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
# pronto zsh theme
#
# Copyright (C) 2021-2023 Jason Thatcher.
# Licensed under the MIT license.
# SPDX-License-Identifier: MIT
#
[[ -o interactive ]] || return
zmodload zsh/datetime || { print "can't load zsh/datetime"; return }
autoload -Uz add-zsh-hook || { print "can't load add-zsh-hook"; return }
(( ${+pronto_features} )) || typeset -A pronto_features=( time 1 git 1 git_ab 1 rprompt 1 )
pronto_init() {
local lp=('%(?..%??)%n@%m:')
local rp=()
if (( $pronto_features[rprompt] )) {
rp+=('%1v')
} else {
pronto_features[time]=0
lp+=('%1v')
}
lp+=('%0~%#%(1j.%j&.) ')
PROMPT=${(j..)lp}
RPROMPT=${(j..)rp}
}
pronto_preexec() {
pronto_timestamp=$EPOCHREALTIME
}
pronto_format_duration() {
local s=()
local r="$(printf '%.0f' $1)"
(($r >= 31557600)) && { s+="$(($r / 31557600))y"; r=$(($r % 31557600)) }
(($r >= 604800 )) && { s+="$(($r / 604800 ))w"; r=$(($r % 604800 )) }
(($r >= 86400 )) && { s+="$(($r / 86400 ))d"; r=$(($r % 86400 )) }
(($r >= 3600 )) && { s+="$(($r / 3600 ))h"; r=$(($r % 3600 )) }
(($r >= 60 )) && { s+="$(($r / 60 ))m"; r=$(($r % 60 )) }
print -n "${(j::)s}$(printf '%.3f' $(($1 % 60)))s"
}
pronto_precmd () {
local all=()
local delta_string=""
if (( $pronto_features[time] )) {
pronto_elapsed=0
if (( pronto_timestamp > 0 )) {
pronto_elapsed=$(( EPOCHREALTIME - pronto_timestamp ))
delta_string=$(pronto_format_duration $pronto_elapsed)
pronto_last_timestamp=$pronto_timestamp
}
pronto_timestamp=0
}
if (( $pronto_features[git] )) {
local -A gs
if (( $pronto_features[git_ab] )) {
command git status --porcelain=v2 --branch --untracked-files=no 2>/dev/null | {
local IFS=''; while read -A; do
if [[ $reply[1] =~ '^# branch\.(oid|head|ab) (.*)$' ]] {
gs+=($match)
}
done
}
} else {
# fast path
local repo
() {
local r=(${(s./.)PWD:P})
while :; do
repo=/${(j./.)r}
[[ -e ${repo}/.git ]] && return 0
(( $#r )) || return 1
r=(${r[1,-2]})
done
}
(( $? )) || {
gs[oid]=$(command git -C ${repo} rev-parse HEAD 2>/dev/null)
if (( $? )) {
gs[oid]='(initial)'
}
gs[head]=$(command git -C ${repo} symbolic-ref HEAD 2>/dev/null)
if (( $? )) {
gs[head]='(detached)'
} else {
gs[head]=${gs[head]#refs/heads/}
}
}
}
if [[ -n $gs ]] {
local ab_string=()
if [[ $gs[ab] =~ '^[+]([0-9]+) [-]([0-9]+)$' ]] {
local ahead=$match[1]
local behind=$match[2]
[[ $ahead != 0 ]] && ab_string+=${ahead}↑
[[ $behind != 0 ]] && ab_string+=${behind}↓
}
local oid=${gs[oid]}
if (( $#oid > 9 )) {
oid=${oid:0:7}
}
local git_string=($gs[head] ${oid} ${(j::)ab_string})
(( $pronto_features[rprompt] )) || git_string=(${(Oa)git_string})
all+=${(j.·.)git_string}
}
}
if (( $pronto_features[time] )) {
if [[ -n $delta_string ]] {
all+=$delta_string
}
all+=$(print -Pn '%D{%f %b %L:%M:%S%p}')
}
if (( $pronto_features[rprompt] )) {
psvar=("${(j: | :)all}")
} else {
(( $#all )) && all+=''
psvar=("${(j.:.)all}")
}
}
add-zsh-hook precmd pronto_precmd
add-zsh-hook preexec pronto_preexec
pronto_init