-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash-functions
113 lines (103 loc) · 3.1 KB
/
.bash-functions
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
auth() {
gcloud auth login &&
gcloud auth application-default login
}
clear_path() {
# Ensure that $1 is not in PATH
PATH=$(echo "$PATH" | sed \
-e "s@:$1:@:@g" \
-e "s@^$1:@@" \
-e "s@:$1\$@@" \
)
}
prepend_path() { for x; do clear_path "$x"; PATH="$x${PATH:+:}$PATH"; done; }
after_cmd() {
# Run after a command, and before a prompt is displayed
local _status=$? ps="${PIPESTATUS[@]}"
report_cmd_status "$HISTFILE" "$ps" >> "$HOME"/.bash-history
window-title
type -t local_post_cmd_hook > /dev/null && local_post_cmd_hook
return $_status
}
make_hist_file() {
{
printf '#%s Shell %d' "$(date +%s)" "$$"
if test "$PPID" -gt 0 2> /dev/null; then
printf ', child of %s,' "$(ps -o pid=,comm= $PPID)"
fi
printf ' begins in %s' "$PWD"
printf '\n'
} | tr -s ' ' >> "$1"
export HISTFILE=$1
}
debug_trap() {
# Runs before a command in an interactive shell
# warning: if you execute a loop, this will run for every iteration
# TODO: figure out how to suppress that behavior
if test "$IFS" != $' \t\n'; then
echo "WARNING: IFS contains unexpected characters"
fi
history -a || warn history -a failed
test -f "$HISTFILE" || warn HISTFILE does not exist
# Emit a warning if the history file is not updating
# When a shell starts up, the debug trap is being called multiple times
# before any commands have been executed. In that case, fc emits no output
# and we do not want to emit a warning about the HISTFILE in that situation.
if ! {
local last # last is the first word of the most recent command
last=$(fc -l -1 2> /dev/null | awk '{print $2; exit}')
# Check the last command to ensure that the HISTFILE is updating.
# Last command may be multi-line, hence the tac.
test -z "$last" || tac "$HISTFILE" |
awk '/^#/ && a++ > 2 {exit}
$1 == "rh" || $1 == last {b++} END{ exit !b }' last="$last";
} 2> /dev/null
then
: warn HISTFILE is not updating
fi
case ${BASH_COMMAND%% } in
:\
|after_cmd\
|debug_trap\
|less\
|ls\
|cd\
)
# Do not set window title for these commands
;;
*)
window-title "${BASH_COMMAND}"
;;
esac
} >&2
#1698377918 (YYYY-MM-DDTHH:MM:SS+0 pid:1121 /Users/wrp/3rd-party/tmux) ok
report_cmd_status() {
# Clean up the most recent command in HISTFILE and append it to global .bash-history.
# Note that the FAILED string is used in scripts/search-bash-history
test -f "$1" && tac "$1" | STATUS="$2" perl -MPOSIX -pe '
if( /^#[0-9]{10}$/ ) { # abort after adding the timestamp.
$ts = substr($_, 1, 10);
s@([0-9]{10})@sprintf "%s (%s pid:%d %s:%s) %s",
$ts,
POSIX::strftime("%FT%T%z", localtime $ts),
'"$$"',
"'"$(hostname)"'",
"'"${PWD}"'",
$ENV{STATUS} =~ "1" ? "FAILED" . " $ENV{STATUS}" : "ok",
@ge;
print; # Since about to skip auto print with -p
exit 0; # Exit so we only process most recent command
}
' | tac
}
die() { test -n "$*" && printf 'FATAL: %s\n' "$*" >&2; exit 1; }
warn() { test -z "$NOWARN" && printf 'WARNING: %s\n' "$*" >&2; }
window-title() {
test -t 1 || return;
while test "${1#-}" != "$1"; do shift; done;
local t
if type deactivate 2> /dev/null >&2; then
t=1
fi
deactivate_is_defined=$t set-title "$@";
}