-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction
More file actions
236 lines (201 loc) · 5.7 KB
/
function
File metadata and controls
236 lines (201 loc) · 5.7 KB
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
function strip_to_architecture()
{
find "${1:-.}" -type f -perm +111 -not -perm +7000 -not -name '*.*' -exec lipo {} -thin "${2:-`arch`}" -output {} \; -print 2>/dev/null
}
calc ()
{
echo "$@" | bc -l
}
checksha256()
{
if [ $# -lt 2 ] ; then
echo "Usage: checksha256 filepath sha256-sum ..." ;
echo " must specify at least one pair of file and checksum." ;
echo "Exiting." ;
fi
filehash=`openssl sha256 "${1}" | /usr/bin/awk -F"=" '{ print $2 }'` ;
if [[ $filehash -eq $2 ]] ; then
echo "** SHA256 matches **" ;
else
echo " ********** ERROR: SHA256 DOES NOT MATCH **********" ;
fi
}
checksha1()
{
if [ $# -lt 2 ] ; then
echo "Usage: checksha1 filepath sha1-sum ..." ;
echo " must specify at least one pair of file and checksum." ;
echo "Exiting." ;
fi
filesha1=`/usr/bin/openssl sha1 "${1}" | /usr/bin/awk -F"=" '{ print $2 }'` ;
if [ $filesha1 == $2 ] ; then
echo "** SHA1 matches **" ;
else
echo " ********** ERROR: SHA1 DOES NOT MATCH **********" ;
fi
}
checkmd5()
{
if [ $# -lt 2 ] ; then
echo "Usage: checkmd5 filepath sha1-sum ..." ;
echo " must specify at least one pair of file and checksum." ;
echo "Exiting." ;
fi
filesha1=`/usr/bin/openssl md5 "${1}" | /usr/bin/awk -F"=" '{ print $2 }'` ;
if [ $filesha1 == $2 ] ; then
echo "** MD5 matches **" ;
else
echo " ********** ERROR: MD5 DOES NOT MATCH **********" ;
fi
}
# From http://luo.ma/geek/ssh-color-iterm
function set_term_bgcolor
{
local R=$1
local G=$2
local B=$3
/usr/bin/osascript <<EOF
tell application "iTerm"
tell the current terminal
tell the current session
set background color to {$(($R*65535/255)), $(($G*65535/255)), $(($B*65535/255))}
end tell
end tell
end tell
EOF
}
function ssh-disabled
{
set_term_bgcolor 255 255 204
command ssh $@
set_term_bgcolor 255 255 255
}
if [ -f ~/bin/rmdir.func ]; then
. ~/bin/rmdir.func
fi
# Extract many types of compress files
# Source: http://nparikh.org/notes/zshrc.txt
extract() {
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar -jxvf "$1" ;;
*.tar.gz) tar -zxvf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.dmg) hdiutil mount "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar -xvf "$1" ;;
*.tbz2) tar -jxvf "$1" ;;
*.tgz) tar -zxvf "$1" ;;
*.zip) unzip "$1" ;;
*.ZIP) unzip "$1" ;;
*.pax) cat "$1" | pax -r ;;
*.pax.Z) uncompress "$1" --stdout | pax -r ;;
*.Z) uncompress "$1" ;;
*) echo "'$1' cannot be extracted/mounted via extract()" ;;
esac
else
echo "'$1' is not a valid file to extract"
fi
}
# Check if resource is served compressed
check_compression()
{
curl --write-out 'Size (uncompressed) = %{size_download}\n' --silent --output /dev/null $1
curl --header 'Accept-Encoding: gzip,deflate,compress' --write-out 'Size (compressed) = %{size_download}\n' --silent --output /dev/null $1
curl --head --header 'Accept-Encoding: gzip,deflate' --silent $1 | grep -i "cache\|content\|vary\|expires"
}
# Get gzipped file size
gz()
{
local origsize=$(wc -c < "$1")
local gzipsize=$(gzip -c "$1" | wc -c)
local ratio=$(echo "$gzipsize * 100/ $origsize" | bc -l)
local saved=$(echo "($origsize - $gzipsize) * 100/ $origsize" | bc -l)
printf "orig: %d bytes\ngzip: %d bytes\nsave: %2.0f%% (%2.0f%%)\n" "$origsize" "$gzipsize" "$saved" "$ratio"
}
# Create a data URL from a file
dataurl()
{
local mimeType=$(file --mime-type "$1" | cut -d ' ' -f2)
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8"
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
}
# -------------------------
# zsh
git-prompt-info() {
git rev-parse --is-inside-work-tree &>/dev/null || return
echo " %F{green}($(current-git-branch)%f $(git-dirty)%F{green})%f"
}
current-git-branch() {
git symbolic-ref --short -q HEAD
}
git-dirty() {
test -z "$(command git status --porcelain --ignore-submodules -unormal)"
if [[ $? -eq 1 ]]; then
echo '%F{red}✗%f'
else
echo '%F{green}✔%f'
fi
}
expand-or-complete-with-waiting-dots() {
echo -n "\e[31m......\e[0m"
zle expand-or-complete
zle redisplay
}
insert-last-command-output() {
LBUFFER+="$(eval $history[$((HISTCMD-1))])"
}
fancy-ctrl-z() {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line
else
zle push-input
zle clear-screen
fi
}
exit-shell() {
exit
}
start-paste() {
bindkey -A paste main
}
end-paste() {
bindkey -e
LBUFFER+=$_paste_content
unset _paste_content
}
paste-insert() {
_paste_content+=$KEYS
}
# https://www.pedaldrivenprogramming.com/2018/09/simple-virtualenv-auto-activation-zsh/
function _virtualenv_auto_activate() {
if [[ -d "venv" ]]; then
_VENV_PATH="venv"
# Check to see if already activated to avoid redundant activating
if [[ "$VIRTUAL_ENV" != $_VENV_PATH ]]; then
source $_VENV_PATH/bin/activate
fi
fi
}
function venvconnect (){
if [[ -n $VIRTUAL_ENV ]]; then
echo $(basename $VIRTUAL_ENV) > .venv
else
echo "Activate a virtualenv first"
fi
}
ps1 () {
if [[ "$PS1" == '$ ' ]]
then
PS1="$OLDPS1"
RPROMPT="$OLD_RPROMPT"
else
export OLDPS1="$PS1"
OLD_RPROMPT="$RPROMPT"
PS1='$ '
RPROMPT=''
fi
}