-
Notifications
You must be signed in to change notification settings - Fork 0
/
utillib.lzui
236 lines (209 loc) · 6.33 KB
/
utillib.lzui
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#
# Library file (*.lzui)
#
# Utility functions
#
ZUI[utillib_sourced]="1"
# FUNCTION: -zui_util_map_bools {{{
# Maps boolean values of expressions given in $1
# (string separated by ';') to parameters given
# via names in $2 (separated by ';'). For true,
# $3 is assigned to corresponding parameter, $4
# for false.
#
# If $1 contains [[ or ((, it is evaluated. If
# it is a positive number or zero, then it is
# treated as direct bool value. Otherwise it's
# treated as parameter name, and boolean value
# of the parameter (it has to be positive number
# or zero) is examined.
#
# -zui_util_map_bools "1;[[ a = b ]];ZUI[text_select]" \
# "color1;color2;color3" $red $white
#
function -zui_util_map_bools() {
local __exp="$1" __pm="$2" __tre="$3" __fse="$4"
local -a __exps __pms
__exps=( "${(@s:;:)__exp}" )
__pms=( "${(@s:;:)__pm}" )
integer __index __size=${#__pms}
for (( __index=1; __index <= __size; ++ __index )); do
__exp="${__exps[__index]}"
__pm="${__pms[__index]}"
[[ -z "${__exp##[[:space:]]##}" || -z "${__pm##[[:space:]]##}" ]] && continue
if [[ "$__exp" = <-> ]] then
(( $__exp )) && : ${(P)__pm::=$__tre} || : ${(P)__pm::=$__fse}
elif [[ "$__exp" != *\[\[* && "$__exp" != *\(\(* ]]; then
[[ "${(P)__exp}" != <-> || "${(P)__exp}" = 0## ]] && : ${(P)__pm::=$__fse} || : ${(P)__pm::=$__tre}
else
eval "$__exp" && : ${(P)__pm::=$__tre} || : ${(P)__pm::=$__fse}
fi
done
} # }}}
# FUNCTION: -zui_util_strip_codes {{{
# Strips formatting codes from text in
# $1, saves result into parameter REPLY
#
# $1 - text to strip codes from
function -zui_util_strip_codes() {
REPLY="${1//[$'\03'-$'\07'$'\013'-$'\014'$'\016'-$'\031'$'\037']/}"
} # }}}
# FUNCTION: -zui_util_get_segment {{{
# Return n-th (z) segment of given text
# $1 - text
# $2 - segment (default is 1)
# $3 - destination variable name (default is "REPLY")
#
# Can use e.g. 'reply[1]' for $3
function -zui_util_get_segment() {
local -a segs
segs=( "${(z@)1}" )
local varname="${3:-REPLY}"
local index="${2:-1}"
: ${(P)varname::=${segs[index]}}
} # }}}
# FUNCTION: -zui_util_get_time {{{
#
# Returns time %H:%M, via datetime or `date` as fallback
#
function -zui_util_get_time() {
local ts
ts="$EPOCHSECONDS"
[[ -z "$ts" || "$ts" = "0" ]] && REPLY="$(date '+%H:%M')" || strftime -s REPLY '%H:%M' "$ts"
} # }}}
# FUNCTION: -zui_util_get_datetime {{{
# Returns date and time. Uses datetime zsh module
# or date command as fallback.
#
# $REPLY - date and time string "Ymd_H.M.S"
#
function -zui_util_get_datetime() {
local ts
ts="$EPOCHSECONDS"
[[ -z "$ts" || "$ts" = "0" ]] && REPLY="$(date '+%Y%m%d_%H.%M.%S')" || strftime -s REPLY '%Y%m%d_%H.%M.%S' "$ts"
} # }}}
# FUNCTION: -zui_util_get_timestamp {{{
# Returns timestamp, via datetime or `date` as fallback
#
function -zui_util_get_timestamp() {
REPLY="$EPOCHSECONDS"
[[ -z "$REPLY" ]] && REPLY="$(date +%s)"
} # }}}
# FUNCTION: -zui_util_has_default_color {{{
# Returns true if the "default" color
# can be used with current Zsh/zcurses
function -zui_util_has_default_color() {
(( ${+zcurses_colors} )) || return 2
[[ -z "${zcurses_colors[(r)default]}" ]] && return 1
autoload is-at-least
is-at-least 2>/dev/null 5.3 && return 0
return 1
} # }}}
# FUNCTION: -zui_util_resolve_path {{{
# Resolves absolute path from current working directory and file path
#
# $1 - current working directory
#
# $2 - file path
#
# $reply[1] - dirname
#
# $reply[2] - basename
#
function -zui_util_resolve_path() {
local dirpath="$1" filepath="$2"
local dirpath2="${dirpath/#\~/$HOME}"
# :a behaves weird, prepends paths, which are not CWD
[ "${dirpath2[1]}" = "/" ] && dirpath2="${dirpath2:a}"
local filepath2="${filepath/#\~/$HOME}"
[ "${filepath2[1]}" = "/" ] && filepath2="${filepath2:a}"
reply=()
if [ "${filepath2[1]}" = "/" ]; then
reply[1]="${filepath2:h}"
reply[2]="${filepath2:t}"
else
local p="$dirpath2/$filepath2"
[ "${p[1]}" = "/" ] && p="${p:a}"
reply[1]="${p:h}"
reply[2]="${p:t}"
fi
} # }}}
# FUNCTION: -zui_util_to_cmd_line {{{
# Puts given text on command line - regardless
# if Zle is active or not
#
# $1 - text to put on command line
#
function -zui_util_to_cmd_line() {
if zle; then
zle .kill-buffer
BUFFER="$1"
zle .redisplay
zle .beginning-of-line
else
print -zr "$1"
fi
} # }}}
# FUNCTION: -zui_util_circular_next {{{
# Returns next file to write to in circular buffer set
# of file names <base>.1 <base>.2 ... <base>.<size>
#
# The buffer is ordered according to modification time.
#
# $1 - base of file names in circular buffer
# $2 - maximum number of files in circular buffer
#
function -zui_util_circular_next() {
setopt localoptions extendedglob
# Input data
local base="$1" count="$2"
# Circular buffers' directory
local circpath="$ZUI_CONFIG_DIR/var/circular_buffers"
[[ ! -d "$circpath" ]] && command mkdir -p "$circpath"
local -a circular_buffer
circular_buffer=( "$circpath"/"$base".[[:digit:]]##(OmN) )
if [[ "$count" -gt "${#circular_buffer}" ]]; then
integer next_index=$(( ${#circular_buffer} + 1 ))
REPLY="$circpath/${base}.${next_index}"
else
REPLY="${circular_buffer[1]}"
fi
return 0
} # }}}
# FUNCTION: -zui_util_circular_paths {{{
#
# Returns absolute file paths of given circular buffer.
# They are ordered from most recent to least recent.
#
# No count is obtained, so all files are returned, even
# actually disabled by buffer limit.
#
# $1 - name of the circular buffer
#
function -zui_util_circular_paths() {
setopt localoptions extendedglob
# Input data
local base="$1"
# Output array
reply=( )
# Circular buffers' directory
local circpath="$ZUI_CONFIG_DIR/var/circular_buffers"
[[ ! -d "$circpath" ]] && return 1
reply=( "$circpath"/"$base".[[:digit:]]##(omN) )
} # }}}
# FUNCTION: -zui_util_to_cmd_line {{{
# Puts given text on command line – regardless if Zle is active or not
#
# $1 - text to put on command line
#
function -zui_util_to_cmd_line() {
if zle; then
zle .kill-buffer
BUFFER="$1"
zle .redisplay
zle .beginning-of-line
else
print -zr "$1"
fi
} # }}}
# vim:ft=zsh