-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions.zsh
292 lines (258 loc) · 8.58 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
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
# Hooks
# chpwd() {
# ls -lG
# }
# Functions
# On Mac OS X, cd to the path of the front Finder window
# Found at <http://brettterpstra.com/2013/02/09/quick-tip-jumping-to-the-finder-location-in-terminal>
function cdf() {
target=$(osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)')
if [ "$target" != "" ]; then
cd "$target"
pwd
else
echo 'No Finder window found' >&2
fi
}
function ..() {
if (( $# == 0 )); then
# echo "✓ cd up once"
cd ..
else
if [[ $1 =~ ^[0-9]+$ ]]; then
# echo "✓ argument received $1"
for i in {1..$1}
do
cd ..
done
else
echo "✗ I don't understand !!"
return 1
fi
fi
}
function dot_if_exists() {
[[ -s $1 ]] && . $1
}
function source_if_exists() {
[[ -s $1 ]] && source $1
}
# List all files, long format, colorized, permissions in octal
# function ll(){
# ls -lG "$@" | awk '
# {
# k=0;
# for (i=0;i<=8;i++)
# k+=((substr($1,i+2,1)~/[rwx]/) *2^(8-i));
# if (k)
# printf("%0o ",k);
# printf(" %9s %3s %2s %5s %6s %s %s %s\n", $3, $6, $7, $8, $5, $9,$10, $11);
# }'
# }
# reduce size of a multiple files
# for f in *.jpeg
# do
# convert -resize 60% "$f" "$f-reduced.jpeg"
# done
# ff jpeg -x convert -resize {} {.}-reduced.jpeg
# placeholder syntax https://github.com/sharkdp/fd#placeholder-syntax
## who is using the laptop's iSight camera?
#camerausedby() {
# echo "Checking to see who is using the iSight camera… 📷"
# usedby=$(lsof | grep -w "AppleCamera\|USBVDC\|iSight" | awk '{printf $2"\n"}' | xargs ps)
# echo -e "Recent camera uses:\n$usedby"
#}
## preview csv files. source: http://stackoverflow.com/questions/1875305/command-line-csv-viewer
#function csvpreview(){
# sed 's/,,/, ,/g;s/,,/, ,/g' "$@" | column -s, -t | less -#2 -N -S
#}
## Extract archives - use: extract <file>
## Based on http://dotfiles.org/~pseup/.bashrc
#function extract() {
# if [ -f "$1" ] ; then
# local filename=$(basename "$1")
# local foldername="${filename%%.*}"
# local fullpath=`perl -e 'use Cwd "abs_path";print abs_path(shift)' "$1"`
# local didfolderexist=false
# if [ -d "$foldername" ]; then
# didfolderexist=true
# read -p "$foldername already exists, do you want to overwrite it? (y/n) " -n 1
# echo
# if [[ $REPLY =~ ^[Nn]$ ]]; then
# return
# fi
# fi
# mkdir -p "$foldername" && cd "$foldername"
# case $1 in
# *.tar.bz2) tar xjf "$fullpath" ;;
# *.tar.gz) tar xzf "$fullpath" ;;
# *.tar.xz) tar Jxvf "$fullpath" ;;
# *.tar.Z) tar xzf "$fullpath" ;;
# *.tar) tar xf "$fullpath" ;;
# *.taz) tar xzf "$fullpath" ;;
# *.tb2) tar xjf "$fullpath" ;;
# *.tbz) tar xjf "$fullpath" ;;
# *.tbz2) tar xjf "$fullpath" ;;
# *.tgz) tar xzf "$fullpath" ;;
# *.txz) tar Jxvf "$fullpath" ;;
# *.zip) unzip "$fullpath" ;;
# *) echo "'$1' cannot be extracted via extract()" && cd .. && ! $didfolderexist && rm -r "$foldername" ;;
# esac
# else
# echo "'$1' is not a valid file"
# fi
#}
#function flushdns() {
# sudo killall -HUP mDNSResponder
# sudo killall mDNSResponderHelper
# sudo dscacheutil -flushcache
# say cleared
#}
#function g() {
# # echo "000 Argument 1 ($1) 2 ($2) 3 ($3)"
#
# if (( $# == 0 )); then
# # echo "XXX - no arguments supplied"
# git status
# else
# case "$1" in
# root)
# # head to the root of this repo
# [ ! -z `git rev-parse --show-cdup` ] && cd `git rev-parse --show-cdup || pwd`
# ;;
# mr)
# # handle "MR" (merge requests from git lab)
# case "$2" in
# help)
# echo "we have two commands:"
# echo " -- g mr origin <MR-NUMBER>"
# echo " -- g mr pull <MR-NUMBER>"
# ;;
# origin)
# # g 1 2 3
# # g mr origin 2597
# # mr = !sh -c 'git fetch $1 merge-requests/$2/head:mr-$1-$2 && git checkout mr-$1-$2' -
# git fetch "$2" merge-requests/"$3"/head:mr-"$2-$3" && git checkout mr-"$2-$3"
# ;;
#
# pull)
# # g mr pull
# # g pull origin merge-requests/10/head
# mrno="$(git symbolic-ref --short HEAD | awk -F'-' '{print $NF}')"
# git pull origin merge-requests/"$mrno"/head
# ;;
#
# *)
# echo "🚨 I don't understand !!"
# return 1
# ;;
# esac
# ;;
# *)
# # echo "ZZZ - git $argv"
# git $argv
# ;;
# esac
# fi
#}
# switch jdks (courtesy: https://twitter.com/JakeWharton/status/1463524757765251082?s=20&t=3Zhu54Kul_i3iai2DzCXJQ)
#jdk() {
# version=$1
# export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
# java -version
#}
#function test() {
# # use $#
#
# # say if you are expecting exactly 2 parameters
# # -ne is bash's way of saying not equal
# if (( $# < 2 )); then
# echo "this script needs exactly 2 parameters"
# return 1
# fi
#
# return 0
#}
#function tot() {
# if ! type "almostontop" > /dev/null; then
# echo "installing almostontop first..."
# source $HOME/.plugins.zsh/almostontop/almostontop.plugin.zsh
# fi
#
# if (( $# == 0 )); then
# echo "almostontop toggled."
# almostontop_toggle
# else
# case "$1" in
# off)
# almostontop off
# ;;
# on)
# almostontop on
# ;;
# esac
# fi
#}
# function ts() {
# local YELLOW
# YELLOW='\033[1;33m' # switching section
# local GRAY
# GRAY='\033[1;30m' # info
# local PURPLE
# PURPLE='\033[1;35m' # making change
# local NC
# NC='\033[0m' # No Colo
# local count
# count=$(task +next count status:pending rc.verbose=nothing)
# if (( $count > 0 )); then
# echo -n -e "\n${PURPLE} ----------------------------------------\n"
# echo -n -e " You planned to do these +next ($count)"
# echo -n -e "\n ----------------------------------------\n${NC}"
# task rc.verbose=nothing +next
# fi
# local month
# month="+dec"
# count=$(task rc.verbose=nothing $month count )
# if (( $count > 0 )); then
# echo -n -e "\n${PURPLE} ----------------------------------------\n"
# echo -n -e " Goal: Monthly Tasks ($count) $month"
# echo -n -e "\n ----------------------------------------\n${NC}"
# task rc.verbose=nothing $month
# fi
# echo "\n"
# count=$(task "(+OVERDUE or +DUE) status:pending" count rc.verbose=nothing)
# if (( $count > 0 )); then
# echo -n -e "\n${PURPLE}----------------------------------------\n"
# echo -n -e " These are overdue or due ! ($count)"
# echo -n -e "\n ----------------------------------------\n${NC}"
# task rc.verbose=nothing "(+OVERDUE or +DUE) status:pending"
# fi
# if (( $# == 0 )); then
# return 0;
# fi
# count=$(task priority:H count rc.verbose=nothing)
# if (( $count > 0 )); then
# echo -n -e "\n${PURPLE}----------------------------------------\n"
# echo -n -e " You think these are high priority ($count)"
# echo -n -e "\n ----------------------------------------\n${NC}"
# task rc.verbose=nothing priority:H
# fi
# count=$(task priority:H start: count rc.verbose=nothing)
# if (( $count > 0 )); then
# echo -n -e "\n${PURPLE}----------------------------------------\n"
# echo -n -e " You haven't started these high priority tasks ($count)"
# echo -n -e "\n ----------------------------------------\n${NC}"
# task rc.verbose=nothing priority:H start:
# fi
# count=$(task project: due: status:pending count rc.verbose=nothing)
# if (( $count > 0 )); then
# echo -n -e "\n${PURPLE}----------------------------------------\n"
# echo -n -e " INBOX ($count)"
# echo -n -e "\n ----------------------------------------\n${NC}"
# task rc.verbose=nothing project: due: status:pending
# fi
# echo -n -e "\n${PURPLE}----------------------------------------\n"
# echo -n -e " List of projects "
# echo -n -e "\n ----------------------------------------\n"
# task projects
# }