-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-command-timer.sh
222 lines (195 loc) · 7.59 KB
/
bash-command-timer.sh
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
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# #
# Copyright (C) 2014-2020 Chuan Ji <[email protected]> #
# #
# 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. #
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Added a GUI notification for long commands taken from:
# https://gitlab.com/victor-engmark/tilde/-/commit/1fc8b9f6ab0586499c7921b1452c43c394cd1e39
# A simple Bash script for printing timing information for each command line
# executed.
#
# 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/bash-command-timer
# SETTINGS
# ========
#
# Whether to enable the command timer by default.
#
# To temporarily disable the printing of timing information, type the following
# in a session:
# BCT_ENABLE=0
# To re-enable:
# BCT_ENABLE=1
BCT_ENABLE=1
# The color of the output for successful and failed commands respectively.
#
# They should be a color string usable in a VT100 escape sequence (see
# http://en.wikipedia.org/wiki/ANSI_escape_code#Colors), without the
# escape sequence prefix and suffix. For example, bold red would be '1;31'.
#
# The color of the output is set in the function BCTPreCommand
#
# If empty, disable colored output. Set them to empty if your terminal does not
# support VT100 escape sequences.
BCT_SUCCESS_COLOR='32'
BCT_ERROR_COLOR='91'
# 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.
BCT_TIME_FORMAT='%b %d %I:%M%p'
# Whether to print command timings up to millisecond precision.
#
# If set to 0, will print up to seconds precision.
BCT_MILLIS=1
# Wheter to wrap to the next line if the output string would overlap with
# characters of last command's output
BCT_WRAP=0
# IMPLEMENTATION
# ==============
# BCTTime:
#
# Command to print out the current time in nanoseconds. This is required
# because the "date" command in OS X and BSD do not support the %N sequence.
#
# BCTPrintTime:
#
# Command to print out a timestamp using BCT_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 +'%N' | grep -qv 'N'; then
BCTTime="date '+%s%N'"
function BCTPrintTime() {
date --date="@$1" +"$BCT_TIME_FORMAT"
}
elif hash gdate 2>/dev/null && gdate +'%N' | grep -qv 'N'; then
BCTTime="gdate '+%s%N'"
function BCTPrintTime() {
gdate --date="@$1" +"$BCT_TIME_FORMAT"
}
elif hash perl 2>/dev/null; then
BCTTime="perl -MTime::HiRes -e 'printf(\"%d\",Time::HiRes::time()*1000000000)'"
function BCTPrintTime() {
date -r "$1" +"$BCT_TIME_FORMAT"
}
else
echo 'No compatible date, gdate or perl commands found, aborting'
exit 1
fi
# BCTPreCommand is trapped to the DEBUG trap, directly or through bash-preexec.
# The debug trap is invoked before the execution of each command typed by the
# user (once for every command in a composite command) and again before the
# execution of PROMPT_COMMAND after the user's command finishes. Thus, to be
# able to preserve the timestamp before the execution of the first command, we
# set the BCT_AT_PROMPT flag in PROMPT_COMMAND, only set the start time if the
# flag is set and clear it after the first execution.
BCT_AT_PROMPT=1
function BCTPreCommand() {
local EXIT="$?"
if [ $EXIT == 0 ]
then
# colour for exit without error
BCT_COLOR=$BCT_SUCCESS_COLOR
else
# colour for exit with error
BCT_COLOR=$BCT_ERROR_COLOR
fi
if [ -z "$BCT_AT_PROMPT" ]; then
return
fi
unset BCT_AT_PROMPT
BCT_COMMAND_START_TIME=$(eval $BCTTime)
BCT_COMMAND_LINE=$(history 1 | sed "s/^[ ]*[0-9]*[ ]*//g")
}
# Bash will automatically set COLUMNS to the current terminal width.
export COLUMNS
# Flag to prevent printing out the time upon first login.
BCT_FIRST_PROMPT=1
# This is executed before printing out the prompt.
function BCTPostCommand() {
BCT_AT_PROMPT=1
if [ -n "$BCT_FIRST_PROMPT" ]; then
unset BCT_FIRST_PROMPT
return
fi
if [ -z "$BCT_ENABLE" ] || [ $BCT_ENABLE -ne 1 ]; then
return
fi
# BCTTime prints out time in nanoseconds.
local MSEC=1000000
local SEC=$(($MSEC * 1000))
local MIN=$((60 * $SEC))
local HOUR=$((60 * $MIN))
local DAY=$((24 * $HOUR))
local command_start_time=$BCT_COMMAND_START_TIME
local command_end_time=$(eval $BCTTime)
local command_time=$(($command_end_time - $command_start_time))
local num_days=$(($command_time / $DAY))
local num_hours=$(($command_time % $DAY / $HOUR))
local num_mins=$(($command_time % $HOUR / $MIN))
local num_secs=$(($command_time % $MIN / $SEC))
local num_msecs=$(($command_time % $SEC / $MSEC))
local time_str=""
if [ $num_days -gt 0 ]; then
time_str="${time_str}${num_days}d "
fi
if [ $num_hours -gt 0 ]; then
time_str="${time_str}${num_hours}h "
fi
if [ $num_mins -gt 0 ]; then
time_str="${time_str}${num_mins}m "
fi
local num_msecs_pretty=''
if [ -n "$BCT_MILLIS" ] && [ $BCT_MILLIS -eq 1 ]; then
local num_msecs_pretty=$(printf '%03d' $num_msecs)
fi
time_str="${time_str}${num_secs}s${num_msecs_pretty}"
now_str=$(BCTPrintTime $(($command_end_time / $SEC)))
if [ -n "$now_str" ]; then
local output_str="[ $time_str | $now_str ]"
else
local output_str="[ $time_str ]"
fi
if [ -n "$BCT_COLOR" ]; then
local output_str_colored="\033[${BCT_COLOR}m${output_str}\033[0m"
else
local output_str_colored="${output_str}"
fi
# Trick to make sure the output wraps to the next line if there is not
# enough room for the string (only when BCT_WRAP == 1)
if [ -n "$BCT_WRAP" ] && [ $BCT_WRAP -eq 1 ]; then
# we'll print as many spaces as characters exist in output_str, plus 2
local wrap_space_prefix="${output_str//?/ } "
else
local wrap_space_prefix=""
fi
# Move to the end of the line. This will NOT wrap to the next line
# unless you have BCT_WRAP == 1
echo -ne "$wrap_space_prefix\033[${COLUMNS}C"
# Move back (length of output_str) columns.
echo -ne "\033[${#output_str}D"
# Finally, print output.
echo -e "${output_str_colored}"
# Send a notification if the command took long time (> 10 minutes)
local tot_secs=$(($command_time / $SEC))
local notify_path=$(which notify-send)
if [ $tot_secs -gt 600 ] && [ -x "$notify_path" ]; then
notify-send "Finished in: $time_str" "$BCT_COMMAND_LINE"
fi
}