-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.sh
91 lines (78 loc) · 2.11 KB
/
common.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/sh
# common functions between different login shells
# sync boostnote directory
sync_boostnote() {
cd $BOOSTNOTE_HOME
git add --all
git commit -am "Update `date -u`"
git push origin
cd -
}
# strip prefixes from
# filenames in the current directory
strip_prefix() {
# requires prefix to strip
[[ -z "$1" ]] && exit 1
for file in *;
do mv "$file" "${file#"$1"}";
done
}
# cleans up control characters
# in most script usages
clean_script() {
[[ -z "$1" ]] && return 1
# courtesy of unixmonkey3987 at:
# https://www.commandlinefu.com/commands/view/2318/fix-a-typescript-file-created-by-the-script-program-to-remove-control-characters
cat "$1" | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > "$1-processed"
mv "$1-processed" "$1"
}
# clone something from github
gh () {
if [[ -z "$1" ]]; then
>&2 echo -e "Usage: \tgh [repo] [folder]\n\t[repo] can be a fully qualified name, or the short name\n\t[folder] is optional"
return 1
fi
local prefix='git@github.com:'
# clone based on arguments
if [[ -z "$2" ]] && [[ "$1" =~ : ]]; then
git clone $prefix"$1"
elif [[ ! -z "$2" ]] && [[ ! "$1" =~ : ]]; then
git clone $prefix"$1"
elif [[ -z "$2" ]] && [[ ! "$1" =~ : ]]; then
git clone $prefix"$1"
else
git clone $prefix"$1" "$2"
fi
}
# opening a subshell for testing something
wonder () {
f=${1:-`mktemp -d`}
# open subshell
bash --rcfile <(
echo "mkdir -p "$f" || rm -rf "$f/";
cd "$f";
PS1='testing subshell $ ';"
) -i
rm -rf "$f" # clean up
}
# xclip to clipboard function
xclip_cb () {
xclip -selection clipboard -i $@
}
generate_password() {
date | md5sum | head -c 32; echo
}
# download a site with wget
wget_slurp() {
wget \
--recursive \
--no-clobber \
--page-requisites \
--convert-links \
--restrict-file-names=windows \
--no-parent \
$1
}
# removing whitespace changes from staging
# courtesy <https://til.hashrocket.com/posts/696df00135-remove-whitespace-changes-then-git-add-p>
alias gwap="git diff -w --no-color | git apply --cached --ignore-whitespace && git checkout -- . && git reset && git add -p"