-
Notifications
You must be signed in to change notification settings - Fork 5
/
callback_functions.sh
68 lines (62 loc) · 2.22 KB
/
callback_functions.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
#!/bin/bash
#
# Useful functions to add to the COMMAND_FINISHED_CALLBACKS array to add
# functionality to your prompt. E.g.:
#
# COMMAND_FINISHED_CALLBACKS+=("notify_desktop")
#
# Callback arguments:
# 1: command
# 2: exit code
# 3: runtime seconds (int)
# 4: formatted runtime (human-readable string)
# Triggers a desktop notification (Ubuntu-only for now) when long-running
# commands finish.
notify_desktop() {
# TODO don't invoke notify-send directly, in order to support other platforms
if [[ -z "$DISPLAY" ]] || ! command -v notify-send >/dev/null; then return; fi
if (( $3 < DISPLAY_COMMAND_FINISHED_DIALOG )); then return; fi
# Don't report certain (e.g. interactive) commands
# TODO make this extensible
if [[ "$1" =~ (less|ssh|vi|vim)\ .* ]]; then return; fi
local icon='stock_dialog-info'
local msg='Finished'
if (( $2 > 127 && $2 < 192 )); then # signals, 1-64
icon='stock_dialog-warning'
msg='Terminated'
elif (( $2 != 0 )); then
icon='stock_dialog-error'
msg='Failed'
fi
notify-send -i "$icon" "${msg} after ${4}: ${1}"
}
# Enables a blink(1) device with the color of the exit-code of long-running
# commands. The disable_blink1 function should also be added to ENV_INFO to
# turn the device off again.
notify_blink1() {
if ! command -v blink1-tool >/dev/null; then return; fi
# Turn off the blink(1) unconditionally, clearing previous commands
blink1-tool --quiet --off
if (( $3 < DISPLAY_COMMAND_FINISHED_DIALOG )); then return; fi
# Don't report certain (e.g. interactive) commands
if [[ "$1" =~ (less|ssh|vi|vim)\ .* ]]; then return; fi
local color=--green
if (( $2 > 127 && $2 < 192 )); then # signals, 1-64
color=--magenta
elif (( $2 != 0 )); then
color=--red
fi
blink1-tool --quiet "$color"
}
# Invokes bc::warm::[func] on any functions added to TITLE_INFO or ENV_INFO that
# are cached with bash-cache. Adding this to your COMMAND_FINISHED_CALLBACKS *may*
# improve prompt responsiveness by concurrently warming cached functions, at the
# cost of additional background processes.
prompt::warm_bc() {
local func
for func in "${TITLE_INFO[@]}" "${ENV_INFO[@]}"; do
if declare -F "bc::warm::${func}" &> /dev/null; then
"bc::warm::${func}"
fi
done
}