-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc
104 lines (91 loc) · 2.77 KB
/
.bashrc
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
#!/usr/bin/env bash
# shellcheck disable=SC1090 # Ignore "Can't follow non-constant source."
#
# Header
#
# Begin overall benchmarking...
now() { perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000)'; }
START="$(now)"
# Load config
CONF=~/.config/bash_profile.env
writeconf() {
mkdir -p "$(dirname "$CONF")"
{
echo "SILENT=${SILENT:-false}"
echo "BENCH=${BENCH:-false}"
echo "DEBUG=${DEBUG:-false}"
echo "TRACE=${TRACE:-false}"
} > "$CONF"
}
readconf() {
[[ -f "$CONF" ]] || writeconf
source "$CONF"
}
readconf
# Utilities
lower() { tr '[:upper:]' '[:lower:]'; }
os() { [[ "$OS" == "$1" ]]; }
OS="$(uname | lower)"
has() { command -v "$1" > /dev/null 2>&1; }
dur() { perl -e 'printf("%.3f\n", ($ARGV[1] - $ARGV[0])/1000)' "$1" "$2"; }
since() { end="$(now)"; dur "$1" "$end"; }
bench() { local start; B=""
if $BENCH && $DEBUG; then start="$(now)"; fi
"$@"; code=$?
if $BENCH && $DEBUG; then B=" ($(since "$start"))"; fi
return "$code"
}
reload() { source ~/.bash_profile; }
CTX="$(basename "${BASH_SOURCE[0]}")"
dbg() { $DEBUG || return 0; log "DEBUG: $CTX: $*"; }
bnc() { $BENCH || return 0; log "BENCH: $CTX: $*"; }
trace() { $TRACE || return 0; log "TRACE: $CTX: $*"; }
log() { printf "%s\n" "$*" >&2; }
msg() { $SILENT && return 0; MSGS+=("$*"); }; MSGS=()
run() { trace "run: \$ $*"
CTX="$CTX: run: $1" bench "$@" && { dbg "run: $1: succeeded$B"; return 0; }
code=$?; dbg "run: $1: failed$B"; return $code;
}
benchmark() { clear; local B="$BENCH"; bench_on; reload 2>&1 | ts -i "%.S"; BENCH="$B"; writeconf; }
bench_on() { BENCH=true; writeconf; }
bench_off() { BENCH=false; writeconf; }
debug_on() { DEBUG=true; writeconf; }
debug_off() { DEBUG=false; TRACE=false; writeconf; }
include() { local code start fn_name file="$1" filename
# STOP is a special exit code meaning stop sourcing further files.
STOP=13
filename="$(basename "$file")"
trace "include: $file"
$BENCH && start="$(now)"
source "$file" || return $?
trace "include: $file: sourced"
fn_name="$(sed -E 's/[[:digit:]]{2}\-(.*)\.bash/\1/' <<< "$filename")"
$BENCH && bnc "$filename $(since "$start")"
run $fn_name && return 0
(( code == STOP )) && MSG=dbg || MSG=log
$MSG "$file failed with exit code $code, skipping remaining files."
return "$code"
}
pathadd() {
for P in "$@"; do
test -d "$P" || { dbg "pathadd: skipping nonexistent path $P to PATH"; continue; }
#[[ ":$PATH:" == *":$P:"* ]] && { dbg "pathadd: skipping duplicate path $P to PATH"; continue; }
PATH="$P:$PATH" && dbg "pathadd: adding $P to PATH"
done
}
path() {
echo "$PATH" | tr ':' '\n' | nl
}
#
# Main
#
for f in ~/.bashrc.d/*.bash; do
include "$f" || { msg "Errors encountered, see above."; break; }
done
#
# Footer
#
$SILENT || {
msg "Loaded .bash_profile in $(since "$START")s"
for M in "${MSGS[@]}"; do log "$M"; done
}