-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path.funcs
348 lines (297 loc) · 14.3 KB
/
.funcs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#!/usr/bin/env bash
duplicity_backup() {
(( $UID )) && { echo 'you need to be root to run this script'; return 1; }
[[ "$#" -lt 2 ]] && \
echo -e "Usage: Call with 'duplicity_backup --exclude-globbing-filelist [excludefilelist (text file)] [src] [dst]'\n" \
" Note: use 'duplicity_backup -v8 ' and as [dst] -> gdocs://[email protected]/backup'\n" \
" for GDrive backup (requires python2-gdata package)" && \
return 1;
echo "Saving installed packages to $HOME/(aur)pkglist.txt"
pacman -Qqen >! $HOME/pkglist.txt
pacman -Qqem >! $HOME/aurpkglist.txt
/usr/lib/systemd/scripts/pakbak
duplicity "$@"
}
# Quick function that:
# Generates an ssh key
# Saves it in the proper subfolder
# Adds the key agent to the ssh-ident config
# Adds the extra config to ssh's config file
# Copies the pub key to the remote server
ssh-keygen-setup() {
[[ "$#" -lt 4 ]] && \
echo "Usage: call with 'ssh-keygen-setup aliasname hostname user port'" && \
return 1;
local aliasname=$1
local hostname=$2
local user=$3
local port=$4
local keyname="id_rsa_$aliasname"
local keydir=$HOME/.ssh/identities/$aliasname/
ssh-keygen-rsa $keyname
mkdir -p $keydir
mv $keyname $keydir
mv $keyname".pub" $keydir
# prepend the new identity entry to ssh-ident config when you find the "]" character
sed -i "/\]/i \ \ \ \ \ \ \ \ (r\"$aliasname\",\"$aliasname\")," $HOME/.ssh/ssh-ident-config
printf '%s\n %s\n %s\n %s\n %s\n %s\n' "Host $aliasname" "HostName $hostname" "User $user" "Port $port" "IdentityFile $keydir$keyname" "IdentitiesOnly yes" >> $HOME/.ssh/config
chmod 0700 $keydir
chmod 0600 $keydir/*
ssh-copy-id -p $port -i $keydir$keyname".pub" $user@$hostname
}
# better colors for man pages
man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
man "$@"
}
path_prepend() {
if test -d "$1"; then
PATH="$1:$PATH"
fi
}
path_append() {
if test -d "$1"; then
PATH="$PATH:$1"
fi
}
mkcd() {
mkdir -p "$1"
builtin cd "$1"
}
# right align text and echo it; helper function for _truncated_ls
_right_align() {
local PADDING=$(($COLUMNS - $(builtin echo "$1" | wc --chars)))
if [[ $PADDING -gt 0 ]]; then
for i in {1..$PADDING}; do
builtin echo -n " "
done
fi
builtin echo "$1"
}
# a pretty ls truncated to at most N lines; helper function for cd, popd, pushd
_truncated_ls() {
local LS_LINES=25 # use no more than N lines for ls output
local RESERVED_LINES=1 # reserve N lines of the term, for short windows
# eg. if a window is only 8 lines high, we want to avoid filling up the
# whole screen, so instead only 3 lines would be consumed.
# if using all N lines makes us go over the reserved number of lines
if [[ $(($LINES - $RESERVED_LINES)) -lt $LS_LINES ]]; then
local LS_LINES=$(($LINES - $RESERVED_LINES))
fi
# compute and store the result of ls
local RAW_LS_OUT="$(ls -lha -r -lhaF --group-directories-first \
--color=always \
--width=$COLUMNS)"
local RAW_LS_LINES=$(builtin echo -E "$RAW_LS_OUT" | wc -l)
if [[ $RAW_LS_LINES -gt $LS_LINES ]]; then
builtin echo -E "$RAW_LS_OUT" | tail -n $(($LS_LINES - 1))
_right_align "... $(($RAW_LS_LINES - $LS_LINES + 1)) lines hidden"
else
builtin echo -E "$RAW_LS_OUT"
fi
}
# for zsh only
if [[ -n "$ZSH" && ! -n "$BASH" ]]; then
# ctrl-z returns to the last process that was sent to background
fancy-ctrl-z () {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line
else
zle push-input
zle clear-screen
fi
}
zle -N fancy-ctrl-z
bindkey '^Z' fancy-ctrl-z
fi
# Create a data URL from a file
function dataurl() {
local mimeType=$(file -b --mime-type "$1");
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8";
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')";
}
function git-fixup() {
local amount=${1:-10}
local array=(${(@f)"$(git --no-pager log -n $amount --pretty=format:%s)"})
for (( i = 1; i <= $#array; i++ )) do
local key="$i"
if [[ $#i == 1 ]]; then
key=" $key"
fi
echo "$key $array[i]"
done
local choice
echo ""
read "choice?Commit to fixup?: "
if [[ "$choice" -le "0" ]]; then
echo "No commit given.. Exiting."
else
local commits_back=$(($choice - 1))
git commit --fixup HEAD~$commits_back
fi
}
function colortest() {
local esc="$(echo -en '\e')"
local blackf="${esc}[30m"; local redf="${esc}[31m"; local greenf="${esc}[32m"
local yellowf="${esc}[33m"; local bluef="${esc}[34m"; local purplef="${esc}[35m"
local cyanf="${esc}[36m"; local whitef="${esc}[37m"
local blackb="${esc}[40m"; local redb="${esc}[41m"; local greenb="${esc}[42m"
local yellowb="${esc}[43m" local blueb="${esc}[44m"; local purpleb="${esc}[45m"
local cyanb="${esc}[46m"; local whiteb="${esc}[47m"
local boldon="${esc}[1m"; local boldoff="${esc}[22m"
local italicson="${esc}[3m"; local italicsoff="${esc}[23m"
local ulon="${esc}[4m"; local uloff="${esc}[24m"
local invon="${esc}[7m"; local invoff="${esc}[27m"
local reset="${esc}[0m"
cat << EOF
${yellowf} ▄███████▄${reset} ${redf} ▄██████▄${reset} ${greenf} ▄██████▄${reset} ${bluef} ▄██████▄${reset} ${purplef} ▄██████▄${reset} ${cyanf} ▄██████▄${reset}
${yellowf}▄█████████▀▀${reset} ${redf}▄${whitef}█▀█${redf}██${whitef}█▀█${redf}██▄${reset} ${greenf}▄${whitef}█▀█${greenf}██${whitef}█▀█${greenf}██▄${reset} ${bluef}▄${whitef}█▀█${bluef}██${whitef}█▀█${bluef}██▄${reset} ${purplef}▄${whitef}█▀█${purplef}██${whitef}█▀█${purplef}██▄${reset} ${cyanf}▄${whitef}█▀█${cyanf}██${whitef}█▀█${cyanf}██▄${reset}
${yellowf}███████▀${reset} ${redf}█${whitef}▄▄█${redf}██${whitef}▄▄█${redf}███${reset} ${greenf}█${whitef}▄▄█${greenf}██${whitef}▄▄█${greenf}███${reset} ${bluef}█${whitef}▄▄█${bluef}██${whitef}▄▄█${bluef}███${reset} ${purplef}█${whitef}▄▄█${purplef}██${whitef}▄▄█${purplef}███${reset} ${cyanf}█${whitef}▄▄█${cyanf}██${whitef}▄▄█${cyanf}███${reset}
${yellowf}███████▄${reset} ${redf}████████████${reset} ${greenf}████████████${reset} ${bluef}████████████${reset} ${purplef}████████████${reset} ${cyanf}████████████${reset}
${yellowf}▀█████████▄▄${reset} ${redf}██▀██▀▀██▀██${reset} ${greenf}██▀██▀▀██▀██${reset} ${bluef}██▀██▀▀██▀██${reset} ${purplef}██▀██▀▀██▀██${reset} ${cyanf}██▀██▀▀██▀██${reset}
${yellowf} ▀███████▀${reset} ${redf}▀ ▀ ▀ ▀${reset} ${greenf}▀ ▀ ▀ ▀${reset} ${bluef}▀ ▀ ▀ ▀${reset} ${purplef}▀ ▀ ▀ ▀${reset} ${cyanf}▀ ▀ ▀ ▀${reset}
${boldon}${yellowf} ▄███████▄ ${redf} ▄██████▄ ${greenf} ▄██████▄ ${bluef} ▄██████▄ ${purplef} ▄██████▄ ${cyanf} ▄██████▄${reset}
${boldon}${yellowf}▄█████████▀▀ ${redf}▄${whitef}█▀█${redf}██${whitef}█▀█${redf}██▄ ${greenf}▄${whitef}█▀█${greenf}██${whitef}█▀█${greenf}██▄ ${bluef}▄${whitef}█▀█${bluef}██${whitef}█▀█${bluef}██▄ ${purplef}▄${whitef}█▀█${purplef}██${whitef}█▀█${purplef}██▄ ${cyanf}▄${whitef}█▀█${cyanf}██${whitef}█▀█${cyanf}██▄${reset}
${boldon}${yellowf}███████▀ ${redf}█${whitef}▄▄█${redf}██${whitef}▄▄█${redf}███ ${greenf}█${whitef}▄▄█${greenf}██${whitef}▄▄█${greenf}███ ${bluef}█${whitef}▄▄█${bluef}██${whitef}▄▄█${bluef}███ ${purplef}█${whitef}▄▄█${purplef}██${whitef}▄▄█${purplef}███ ${cyanf}█${whitef}▄▄█${cyanf}██${whitef}▄▄█${cyanf}███${reset}
${boldon}${yellowf}███████▄ ${redf}████████████ ${greenf}████████████ ${bluef}████████████ ${purplef}████████████ ${cyanf}████████████${reset}
${boldon}${yellowf}▀█████████▄▄ ${redf}██▀██▀▀██▀██ ${greenf}██▀██▀▀██▀██ ${bluef}██▀██▀▀██▀██ ${purplef}██▀██▀▀██▀██ ${cyanf}██▀██▀▀██▀██${reset}
${boldon}${yellowf} ▀███████▀ ${redf}▀ ▀ ▀ ▀ ${greenf}▀ ▀ ▀ ▀ ${bluef}▀ ▀ ▀ ▀ ${purplef}▀ ▀ ▀ ▀ ${cyanf}▀ ▀ ▀ ▀${reset}
EOF
}
function hex2dec() { awk 'BEGIN { printf "%d\n",0x$1}'; }
function dec2hex() { awk 'BEGIN { printf "%x\n",$1}'; }
# Shrinks size of pdf when it has lots of images.
function optimize_pdf() {
[[ "$#" -lt 2 ]] && \
echo "Usage: call with '$0 input.pdf output.pdf'" && \
return 1;
gs -q -dNOPAUSE -dBATCH -dSAFER \
-sDEVICE=pdfwrite \
-dCompatibilityLevel=1.3 \
-dPDFSETTINGS=/screen \
-dEmbedAllFonts=true \
-dSubsetFonts=true \
-dColorImageDownsampleType=/Bicubic \
-dColorImageResolution=76 \
-dGrayImageDownsampleType=/Bicubic \
-dGrayImageResolution=76 \
-dMonoImageDownsampleType=/Bicubic \
-dMonoImageResolution=76 \
-sOutputFile=$2 \
$1
}
function print_remotely() {
[[ "$#" -lt 2 ]] && \
echo "Usage: call with '$0 [FILENAME] [REMOTE_PRINTER_SSH_ALIAS] [optional:NUM_OF_COPIES]'" && \
return 1;
local file="$1"
local remote_printer_alias="$2"
local copies="${3-1}" # use third argument or 1 if it doesn't exist
# If you'd like to staple your prints, uncomment the following:
# # /usr/bin/ssh -q $remote_printer_alias lpoptions -o StapleLocation=UpperLeft || exit 1
# # And if you want color:
# # /usr/bin/ssh -q $remote_printer_alias lpoptions -o ColorModel=Color || exit 1
cat $file | /usr/bin/ssh -q $remote_printer_alias lpr -o ColorModel=Color -o StapleLocation=UpperLeft -#$copies || echo "Error"
}
function format_all_cpp_code() {
find "${1-.}" \( -name '*.h' -or -name '*.cxx' -or -name "*.cpp" \) -type f -print0 | xargs -0 clang-format -style=llvm -i
}
# repeat $1 times whatever is in the rest of the args
function repeat() {
[[ "$#" -lt 2 ]] && \
echo "Usage: call with '$0 [times to repeat] [command]'" && \
return 1;
local _
for _ in $(seq $1); do
eval "${@:2}"
done
}
xor_with_num() {
[[ "$#" -lt 2 ]] && \
echo "Usage: call with '$0 [xor key in decimal form] [text to xor]'" && \
return 1;
python -c "import sys; sys.stdout.write(''.join(chr(i) for i in [ord(a) ^ ${1} for a in \"${2}\"]))";
}
# Create a data URL from a file
dataurl() {
local mimeType=$(file -b --mime-type "$1");
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8";
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')";
}
# UTF-8-encode a string of Unicode symbols
utf8_encode() {
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u);
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo ""; # newline
fi;
}
# Decode \x{ABCD}-style Unicode escape sequences
utf8_decode() {
perl -e "binmode(STDOUT, ':utf8'); print \"$@\"";
# print a newline unless we’re piping the output to another program
if [ -t 1 ]; then
echo ""; # newline
fi;
}
# from https://gist.github.com/nl5887/a511f172d3fb3cd0e42d
transfersh() {
# check arguments
if [ $# -eq 0 ];
then
echo "No arguments specified. Usage:\n transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"
return 1
fi
# get temporarily filename, output is written to this file so progress can be showed
tmpfile=$( mktemp -t transferXXX )
# upload stdin or file
file=$1
if tty -s;
then
basefile=$(basename "$file" | sed -e 's/[^a-zA-Z0-9._-]/-/g')
if [ ! -e $file ];
then
echo "File $file doesn't exist."
return 1
fi
if [ -d $file ];
then
# zip directory and transfer
zipfile=$( mktemp -t transferXXX.zip )
cd $(dirname $file) && zip -r -q - $(basename $file) >> $zipfile
curl --progress-bar --upload-file "$zipfile" "https://transfer.sh/$basefile.zip" >> $tmpfile
rm -f $zipfile
else
# transfer file
curl --progress-bar --upload-file "$file" "https://transfer.sh/$basefile" >> $tmpfile
fi
else
# transfer pipe
curl --progress-bar --upload-file "-" "https://transfer.sh/$file" >> $tmpfile
fi
# cat output link
cat $tmpfile
# cleanup
rm -f $tmpfile
}
# $1 is backup destination
# Init: borg init --encryption=repokey $1/backup/folder
# To List: borg --list --prefix '{hostname}-' --show-rc --keep-daily 7 --keep-weekly 4 --keep-monthly 6 $1/backup/folder
borg_backup() {
borg create --stats --verbose --list --show-rc --compression lz4 --exclude-caches --exclude-from ~/.borg_filelist $1::'{hostname}-{now}' /
}
borg_prune() {
borg --list --prefix '{hostname}-' --show-rc --keep-daily 7 --keep-weekly 4 --keep-monthly 6 $1
}