-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.zsh
121 lines (110 loc) · 2.89 KB
/
functions.zsh
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
# Create directory and cd to it
mkcd() {
mkdir -p "$1" && cd "$1";
}
# join arguments using $1 as separator, e.g. `join_by , a b c` returns "a,b,c"
join_by() {
local IFS="$1";
shift;
echo "$*";
}
# Add directories to PATH
add_to_path() {
for dir in $@ ; do
if ! [[ $PATH =~ "$dir" ]]; then
PATH="$PATH:$dir"
fi
done
}
prefix_path() {
for dir in $@ ; do
if ! [[ $PATH =~ "$dir" ]]; then
PATH="$dir:$PATH"
fi
done
}
# Remove duplicate PATH entries
if [[ $(which perl) ]]; then
dedupe_path() {
PATH="$(perl -e 'print join(":", grep { not $seen{$_}++ } split(/:/, $ENV{PATH}))')"
}
else
dedupe_path() {
PATH=$(awk -v RS=: '!($0 in a) {a[$0]; printf("%s%s", length(a) > 1 ? ":" : "", $0)}' <<<$PATH)
}
fi
# compressed file extractor
# (from https://github.com/myfreeweb/zshuery/blob/master/zshuery.sh)
extract() {
if [[ -f $1 ]]; then
case $1 in
*.tar.bz2) tar xvjf $1;;
*.tar.gz) tar xvzf $1;;
*.tar.xz) tar xvJf $1;;
*.tar.lzma) tar --lzma xvf $1;;
*.bz2) bunzip $1;;
*.rar) unrar $1;;
*.gz) gunzip $1;;
*.tar) tar xvf $1;;
*.tbz2) tar xvjf $1;;
*.tgz) tar xvzf $1;;
*.zip) unzip $1;;
*.Z) uncompress $1;;
*.7z) 7z x $1;;
*.dmg) hdiutul mount $1;; # mount OS X disk images
*) echo "'$1' cannot be extracted";;
esac
else
echo "'$1' is not a file that can be extracted"
fi
}
# shell function to define words
# http://vikros.tumblr.com/post/23750050330/cute-little-function-time
define() {
if [[ $# -ge 2 ]] then
echo "error: too many arguments" >&2
return 1
else
curl "dict://dict.org/d:$1" | less
fi
}
# Configures a git repo for gerrit by adding some git hooks
gerrit-init() {
gitdir=`git rev-parse --git-dir` 2>/dev/null
if [ "$?" -ne 0 ]
then
echo "ERROR: Not a git repository"
return 1
fi
# get commit-msg hook
echo "Copying commit-msg hook to ${gitdir}/hooks"
curl -sSLo ${gitdir}/hooks/commit-msg \
https://gerrit-review.googlesource.com/tools/hooks/commit-msg \
&& chmod +x ${gitdir}/hooks/commit-msg
# copy pre-push hook
echo "Copying pre-push hook to ${gitdir}/hooks"
cp ${ZSH_CUSTOM}/gerrit-pre-push ${gitdir}/hooks/pre-push \
&& chmod +x ${gitdir}/hooks/pre-push
# copy pre-push hook
echo "Copying pre-commit hook to ${gitdir}/hooks"
cp ${ZSH_CUSTOM}/gerrit-pre-commit ${gitdir}/hooks/pre-commit \
&& chmod +x ${gitdir}/hooks/pre-commit
}
# Add shuf on Darwin
if [[ $(uname) = 'Darwin' ]]; then
if [[ $(which ruby) ]]; then
shuf() {
ruby -e 'Signal.trap("SIGPIPE", "SYSTEM_DEFAULT");
puts ARGF.readlines.shuffle' "$@";
}
elif [[ $(which perl) ]]; then
shuf() {
perl -MList::Util=shuffle -e 'print shuffle(<>);' "$@";
}
else
shuf() {
awk 'BEGIN {srand(); OFMT="%.17f"} {print rand(), $0}' "$@" |
sort -k1,1n | cut -d ' ' -f2-;
}
fi
fi