-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-functions
142 lines (131 loc) · 3.35 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
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
# vim: syntax=sh tabstop=4 shiftwidth=4
function abort {
if [ ! -z "$1" ]; then
echo $@ >&2
fi
exit 1
}
function die {
abort "$@"
}
function waitforpid {
for pid in "$@"; do
while kill -0 "$pid" 2>/dev/null; do
sleep 0.5
done
done
}
function color {
if [ ! -z "$1" ] && [ -f "$1" ]; then
color < $1
else
printf '\x1b[41m\x1b[37m%q\x1b[0m' $(cat)
fi
}
function failcolor {
if [ "$1" = '-h' -o "$1" = '--help' ]; then
echo "Usage: ${FUNCNAME[0]} STRING"
echo "Print STDIN using \`color' if output does not match STRING"
return 1
fi
inputstring=$(cat)
if [ "$inputstring" != "$1" ]; then
color <<< "$inputstring"
else
printf '%q' "$inputstring"
fi
}
function findfunction {
if [ "$(type -t "$1")" != "function" ]; then
type "$1"
return 0
fi
shopt -s extdebug
declare -F "$1"
type "$1"
shopt -u extdebug
}
#enable bash completion for this
complete -c wcat
function wcat {
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ -z "$1" ]; then
echo "Usage: $FUNCNAME [-l|-v] COMMAND [VIMOPTS]"
echo "Print contents of file matching COMMAND"
echo
echo " -l Use less instead of cat"
echo ' -v Use vim instead of cat; append VIMOPTS to vim invocation'
echo " -h This help"
return 0
elif [ "$1" = "-l" ]; then
cmdname=less
shift
elif [ "$1" = "-v" ]; then
cmdname=vim
shift
else
cmdname=cat
fi
if [ "$(type -t "$1")" != "file" ]; then
type "$1"
return 1
elif ! which "$1" &> /dev/null; then
echo "'$1' not found, exiting"
return 1
else
command="$1"
shift
"$cmdname" "$(which "$command")" $@
fi
}
# need to write waitforfile at some point
function cheat {
if [ "$#" -eq 2 ] && [[ ! $2 = --* ]]; then
command cheat "$1" | egrep --color=auto "$2"
else
command cheat "$@"
fi
}
if hash busctl 2> /dev/null; then
function shutdown {
if [ "$1" = "--help" ]; then
/sbin/shutdown --help
echo " -w Display time of a pending shutdown (helpers addition)"
elif [ "$1" = "-w" ]; then
awk -f - <<'ENDAWK'
BEGIN {
"busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager ScheduledShutdown" | getline result
split(result,results)
results[3]/=1000000
if (results[3] != 0)
print "Shutdown scheduled for " strftime("%a %Y-%m-%d %T %Z",results[3],0) ", use 'shutdown -c' to cancel."
}
ENDAWK
else
/sbin/shutdown $@
fi
}
fi
if hash mkvinfo 2> /dev/null; then
function mkvinf () {
local args=( "$@" )
if [ "$1" = "-h" -o "$1" = "--help" ]; then
printf 'Usage: mkvinf MKVFILE... [ELEMENT]\n'
printf 'Print information about ELEMENT in MKVFILES, or all elements if ELEMENT is not provided.'
printf '\n\nCommon elements:\n\taudio\n\tresolution\n\tsubtitles\n\tvideo\n'
return 1
fi
if [ -n "$2" ] && [ ! -f "${args[-1]}" ]; then
local fieldname="${args[-1]}"
unset 'args[-1]'
fi
for i in "${args[@]}"; do
if [ -n "$fieldname" ] && [ "$fieldname" != "resolution" ]; then
mkvmerge --identify -F json "$1" | jq -C '.tracks | .[] | select(.type=="'"$fieldname"'")'
elif [ "$fieldname" = "resolution" ]; then
mkvmerge --identify -F json "$1" | jq -C '.tracks | .[] | select(.type=="video") | .properties | .display_dimensions'
else
mkvmerge --identify -F json "$1" | jq -C '.'
fi
done
}
fi