-
Notifications
You must be signed in to change notification settings - Fork 20
/
fish_command_timer.fish
294 lines (268 loc) · 9.98 KB
/
fish_command_timer.fish
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Copyright (C) 2016-2020 Chuan Ji <chuan@jichu4n.com> #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# A fish shell script for printing execution time for each command.
#
# For the most up-to-date version, as well as further information and
# installation instructions, please visit the GitHub project page at
# https://github.com/jichu4n/fish-command-timer
#
# Requires fish 2.2 or above.
# SETTINGS
# ========
#
# Whether to enable the command timer by default.
#
# To temporarily disable the command timer, type the following
# in a session:
# set fish_command_timer_enabled 0
# To re-enable:
# set fish_command_timer_enabled 1
if not set -q fish_command_timer_enabled
set fish_command_timer_enabled 1
end
# Whether to display the exit status of the previous command.
if not set -q fish_command_timer_status_enabled
set fish_command_timer_status_enabled 0
end
# The color of the output.
#
# This should be a color string accepted by fish's set_color command, as
# described here:
#
# http://fishshell.com/docs/current/commands.html#set_color
#
# If empty, disable colored output. Set it to empty if your terminal does not
# support colors.
if not set -q fish_command_timer_color
set fish_command_timer_color blue
end
# Similarly, the color to use for displaying success and failure exit statuses.
if not set -q fish_command_timer_success_color
set fish_command_timer_success_color green
end
if not set -q fish_command_timer_fail_color
set fish_command_timer_fail_color $fish_color_status
end
# The display format of the current time.
#
# This is a strftime format string (see http://strftime.org/). To tweak the
# display format of the current time, change the following line to your desired
# pattern.
#
# If empty, disables printing of current time.
if not set -q fish_command_timer_time_format
set fish_command_timer_time_format '%b %d %I:%M%p'
end
# Whether to print command timings up to millisecond precision.
#
# If set to 0, will print up to seconds precision.
if not set -q fish_command_timer_millis
set fish_command_timer_millis 1
end
# Whether to export the duration string as a shell variable.
#
# When set, this will export the duration string as an environment variable
# called $CMD_DURATION_STR.
if not set -q fish_command_timer_export_cmd_duration_str
set fish_command_timer_export_cmd_duration_str 1
end
if begin
set -q fish_command_timer_export_cmd_duration_str; and \
[ "$fish_command_timer_export_cmd_duration_str" -ne 0 ]
end
set CMD_DURATION_STR
end
# The minimum command duration that should trigger printing of command timings,
# in milliseconds.
#
# When set to a non-zero value, commands that finished within the specified
# number of milliseconds will not trigger printing of command timings.
if not set -q fish_command_timer_min_cmd_duration
set fish_command_timer_min_cmd_duration 0
end
# IMPLEMENTATION
# ==============
# fish_command_timer_get_ts:
#
# Command to print out the current time in seconds.
#
# fish_command_timer_print_time:
#
# Command to print out a timestamp using fish_command_timer_time_format. The
# timestamp should be in seconds. This is required because the "date" command in
# Linux and OS X use different arguments to specify the timestamp to print.
if date --date='@0' '+%s' > /dev/null 2> /dev/null
# Linux.
function fish_command_timer_print_time
date --date="@$argv[1]" +"$fish_command_timer_time_format"
end
else if date -r 0 '+%s' > /dev/null 2> /dev/null
# macOS / BSD.
function fish_command_timer_print_time
date -r "$argv[1]" +"$fish_command_timer_time_format"
end
else
echo 'No compatible date commands found, not enabling fish command timer'
set fish_command_timer_enabled 0
end
# fish_command_timer_strlen:
#
# Command to print out the length of a string. This is required because the expr
# command behaves differently on Linux and OS X. On fish 2.3+, we will use the
# "string" builtin.
if type string > /dev/null 2> /dev/null
function fish_command_timer_strlen
string length "$argv[1]"
end
else if expr length + "1" > /dev/null 2> /dev/null
function fish_command_timer_strlen
expr length + "$argv[1]"
end
else if type wc > /dev/null 2> /dev/null; and type tr > /dev/null 2> /dev/null
function fish_command_timer_strlen
echo -n "$argv[1]" | wc -c | tr -d ' '
end
else
echo 'No compatible string, expr, or wc commands found, not enabling fish command timer'
set fish_command_timer_enabled 0
end
# Computes whether the postexec hooks should compute command duration.
function fish_command_timer_should_compute
begin
set -q fish_command_timer_enabled; and \
[ "$fish_command_timer_enabled" -ne 0 ]
end; or \
begin
set -q fish_command_timer_export_cmd_duration_str; and \
[ "$fish_command_timer_export_cmd_duration_str" -ne 0 ]
end
end
# Computes the command duration string (e.g. "3m5s016").
function fish_command_timer_compute_cmd_duration_str
set -l SEC 1000
set -l MIN 60000
set -l HOUR 3600000
set -l DAY 86400000
set -l num_days (math -s0 "$CMD_DURATION / $DAY")
set -l num_hours (math -s0 "$CMD_DURATION % $DAY / $HOUR")
set -l num_mins (math -s0 "$CMD_DURATION % $HOUR / $MIN")
set -l num_secs (math -s0 "$CMD_DURATION % $MIN / $SEC")
set -l num_millis (math -s0 "$CMD_DURATION % $SEC")
set -l cmd_duration_str ""
if [ $num_days -gt 0 ]
set cmd_duration_str {$cmd_duration_str}{$num_days}"d "
end
if [ $num_hours -gt 0 ]
set cmd_duration_str {$cmd_duration_str}{$num_hours}"h "
end
if [ $num_mins -gt 0 ]
set cmd_duration_str {$cmd_duration_str}{$num_mins}"m "
end
set -l num_millis_pretty ''
if begin
set -q fish_command_timer_millis; and \
[ "$fish_command_timer_millis" -ne 0 ]
end
set num_millis_pretty (printf '%03d' $num_millis)
end
set cmd_duration_str {$cmd_duration_str}{$num_secs}s{$num_millis_pretty}
echo $cmd_duration_str
end
# The fish_postexec event is fired after executing a command line.
function fish_command_timer_postexec -e fish_postexec
set -l last_status $status
set -l command_end_time (date '+%s')
if not fish_command_timer_should_compute
return
end
set -l cmd_duration_str (fish_command_timer_compute_cmd_duration_str)
if begin
set -q fish_command_timer_export_cmd_duration_str; and \
[ "$fish_command_timer_export_cmd_duration_str" -ne 0 ]
end
set CMD_DURATION_STR "$cmd_duration_str"
end
if not begin
set -q fish_command_timer_enabled; and \
[ "$fish_command_timer_enabled" -ne 0 ]
end
return
end
if set -q fish_command_timer_min_cmd_duration; and \
[ "$fish_command_timer_min_cmd_duration" -gt "$CMD_DURATION" ]; and begin
[ "$last_status" -eq 0 ]; or \
not set -q fish_command_timer_status_enabled; or \
[ "$fish_command_timer_status_enabled" -eq 0 ]
end
return
end
# Compute timing string (e.g. [ 1s016 | Oct 01 11:11PM ])
set -l timing_str
set -l now_str (fish_command_timer_print_time $command_end_time)
if [ -n "$now_str" ]
set timing_str "[ $cmd_duration_str | $now_str ]"
else
set timing_str "[ $cmd_duration_str ]"
end
set -l timing_str_length (fish_command_timer_strlen "$timing_str")
# Compute timing string with color.
set -l timing_str_colored
if begin
set -q fish_command_timer_color; and \
[ -n "$fish_command_timer_color" ]
end
set timing_str_colored (set_color $fish_command_timer_color)"$timing_str"(set_color normal)
else
set timing_str_colored "$timing_str"
end
# Compute status string (e.g. [ SIGINT ])
set -l status_str ""
if begin
set -q fish_command_timer_status_enabled; and \
[ "$fish_command_timer_status_enabled" -ne 0 ]
end
set -l signal (fish_status_to_signal $last_status)
set status_str "[ $signal ]"
end
set -l status_str_length (fish_command_timer_strlen "$status_str")
# Compute status string with color.
set -l status_str_colored
if begin
[ $last_status -eq 0 ]; and \
set -q fish_command_timer_success_color; and \
[ -n "$fish_command_timer_success_color" ]
end
set status_str_colored (set_color $fish_command_timer_success_color)"$status_str"(set_color normal)
else if begin
[ $last_status -ne 0 ]; and \
set -q fish_command_timer_fail_color; and \
[ -n "$fish_command_timer_fail_color" ]
end
set status_str_colored (set_color --bold $fish_command_timer_fail_color)"$status_str"(set_color normal)
else
set status_str_colored "$status_str"
end
# Combine status string and timing string.
set -l output_length (math $timing_str_length + $status_str_length + 1)
# Move to the end of the line. This will NOT wrap to the next line.
echo -ne "\033["{$COLUMNS}"C"
# Move back output_length columns.
echo -ne "\033["{$output_length}"D"
# Finally, print output.
echo -e "$status_str_colored $timing_str_colored"
end