-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.bash_functions
330 lines (287 loc) · 9.58 KB
/
.bash_functions
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
#!/usr/bin/env bash
wib() {
# words in blog
# determine total words in my blog
blog_dir="$HOME"/Desktop/Bhupesh-V.github.io/
printf "Total Blogs: %s" "$(ls "$blog_dir"/_posts/ | wc -l)"
printf "\n%s" "Total Words: "
words_blogs=$(grep -nir -o -P '.{0,5} totalwords' --exclude-dir=tag --exclude "feed.xml" "$blog_dir/"_site | awk '{print $2}' | awk '{s+=$0} END {print s}')
echo $words_blogs
til_data=("CleanCode" "Python" "Shell" "Go" "WebDev" "Miscellaneous")
for dir in ${til_data[@]}; do
files=$(ls "$HOME/Documents/til/$dir")
for til in ${files[@]}; do
words=$(wc -w "$HOME/Documents/til/$dir/$til" | awk '{print $1}')
let total_words+=$words
# printf "%s%s\n" "words = ${words}, total = ${total_words}"
done
done
printf "\n%s\n" "Total TILs: $(awk '/count/ {print $2}' Documents/til/count.json)"
printf "%s" "Total Words: ${total_words}"
printf "\n\n%s" "Grand Total: $((total_words + words_blogs))"
}
gdl() {
# Get actual drive public link
# Read More: bhupesh-v.github.io/extract-file-id-from-drive-shareable-link/
str="$1"
# remove everything after the last /
remove_last=${str%/*}
# get everything after the last /
get_last=${remove_last##*/}
echo "https://drive.google.com/uc?export=view&id=$get_last"
}
alarm() {
# Set an alarm
#
# Usage: alarm 10 drink-water (for minutes use 'm' e.g alarm 15m "go piss")
#
# set +m disables monitor mode for bg jobs. Another way to deactivate it: set +o monitor
# Use 'help set' to see more options
set +m
(
sleep "$1"
notify-send -a "CLI Alarm" -u critical -i time "$2" "Alarm Notification Alert"
) &
}
vcd() {
# [v]irtual [cd] automatically activates/deactivates python virtual environments on cd
#
# WARNING: vcd right now only works for following python project setup
#
# virtual-env-folder
# ├── bin
# ├── include
# ├── lib
# └── your-project-folder-or-repo
# ├── .git
# └── .. other files/folders ..
userpath=$1
# deactivate any existing virtual env
[[ $VIRTUAL_ENV ]] && deactivate
if [[ $userpath != "" ]]; then
case $1 in
"..") cd .. && return ;;
"-") cd - && return ;;
"/") cd / && return ;;
*) cd "$userpath" || return ;;
esac
current_dir=$(pwd)
while [[ "$current_dir" != "$HOME" ]]; do
# check if current dir contains an activate script (the venv folder)
if [[ -f "$current_dir/venv/bin/activate" ]]; then
echo "got venv"
source "$current_dir/venv/bin/activate"
break
fi
# remove base directory name
current_dir="${current_dir%/*}"
done
fi
}
scd() {
# [s]mart [cd] : find absolute paths & automatically switch to them
# Also see scd-completions.bash for automatic tab suggestions
if [[ "$1" ]]; then
case $1 in
"..") cd .. || return ;;
"-") cd - || return ;;
"/") cd / || return ;;
*) if [[ $1 = /* ]]; then
# match absolute path
cd "$1" || return
else
# redo work if tab suggestions are not used
while read -r value; do
files+=($value)
done < <(locate -e -r "/$1$" | grep "$HOME")
if [[ ${#files} == 0 ]]; then
# do loose search
while read -r value; do
files+=($value)
done < <(locate -e -b -r "$1" | grep "$HOME")
fi
for file_match in "${files[@]}"; do
if [[ -d $file_match ]]; then
printf "%s\n" "Hit 🎯: $file_match"
cd "$file_match" || return
fi
done
unset files
fi ;;
esac
else
cd ~ || return
fi
# why tf is this not working
# [[ -z "$1" ]] && cd "$HOME" || exit
}
# gcd() {
# cd "$1" || return
# default_branch=$(git remote show origin | awk '/HEAD/ {print $3}')
# remote_commit=$(git ls-remote --head --exit-code origin "$default_branch" | cut -f 1 | head -1)
# local_commit=$(git --no-pager log --pretty=tformat:"%H" -1)
# if [[ $remote_commit != $local_commit ]]; then
# printf "%s\n" "Your repository seems to be out of sync with remote"
# printf "%s\n" "Please take a git pull"
# fi
# }
netu() {
# [net]work [u]sage: check network usage stats
net_device=$(ip route | awk '/via/ {print $5}')
TRANSMITTED=$(ifconfig "$net_device" | awk '/TX packets/ {print $6$7}')
RECEIVED=$(ifconfig "$net_device" | awk '/RX packets/ {print $6$7}')
pc_uptime=$(uptime -p | awk '{for (i=2; i<NF; i++) printf $i " "; if (NF >= 1) print $NF; }')
printf "%s\n\n" "Network Usage since $pc_uptime"
printf "%s\n" "$(tput bold)🔼 TRANSMITTED $(tput sgr0): $TRANSMITTED"
printf "%s\n" "$(tput bold)🔽 RECEIVED $(tput sgr0): $RECEIVED"
}
extract() {
# Extract different archives
# FROM: https://tldp.org/LDP/abs/html/sample-bashrc.html
if [ -f "$1" ]; then
case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.tgz) tar xvzf "$1" ;;
*.tar) tar xvf "$1" ;;
*.zip) unzip "$1" ;;
*) echo "'$1' is not a valid archive file!" ;;
esac
else
echo "extract requires a filepath"
fi
}
myip() {
# Show my IP
echo -e "$(ip route | awk '/via/ {print $3}')"
}
perm() {
# Show file permissions
ls -l "$1" | awk '{ print $1}'
}
hg() {
# [h]istory [g]rep searches history for unique commands
if [[ "$1" ]]; then
# cut uses ' ' as a delimiter & prints everything from
# the 3 field to the end of line
history | grep "$1" --color=always | cut -c 8- | uniq -u
# Other variations:
# history | grep "$1" --color=always | cut -d' ' -f3- | uniq -u
else
echo -e "hg needs a pattern to look for"
fi
}
eye() {
# custom text file viewer
filename=$(basename -- "$1")
extension="${filename##*.}"
if [[ $extension == "md" ]]; then
glow -w "$COLUMNS" "$1"
elif [[ $extension == "json" ]]; then
python3 -m json.tool "$1"
elif [[ $(file --mime "$1" | awk '{print $3}') == "video/mp4;" ]]; then
browse "$1"
else
batcat "$1"
fi
}
export -f eye
cb() {
# copy stuff from terminal to clipboard
# TODO: way to handle -a
# clear the cliboard
xsel -bc
# add to clipboard
xsel -b <"$1"
}
h() {
# Combined help lookup
#
# man -k == apropos
# man -f == whatis
if [[ -z "$1" ]]; then
echo "h needs an argument to look 👀"
else
type "$1"
whereis "$1"
whatis "$1"
apropos "$1"
fi
}
hl () {
# FROM: http://www.wassen.net/highlight-output.html
# Use: tail file.txt | hl word-to-highlight
if [[ $1 = '-i' ]]; then
ARGS='--ignore-case'
shift
fi
egrep $ARGS --color=always -e '' $(echo $* | xargs -n1 printf "-e%s ");
}
fino() {
# get data if piped
declare filepath=${1:-$(</dev/stdin)};
if [[ -f "$filepath" ]]; then
echo -e "$(basename "$filepath")"
info=$(file "$filepath" | awk -F ":" '{print $2}')
echo -e "$info"
ls -alh "$filepath" | awk '{print $1 "\nSize: " $5 "\nLast Modify: " $6 " " $7 " " $8}'
fi
# printf "Created: %s" "$(sudo debugfs -R "stat <$(ls -i "$1" | awk '{ print $1}')>" /dev/sda1 | grep 'crtime')"
}
export -f fino
lk() {
grep -wnirI --color=always --exclude='*.gitignore' --exclude-dir={_site,.git,.github} "$@" | awk '{$1=$1};1' | awk -f ~/Documents/.Varshney/scripts/pretty-grep.awk
}
fcd() {
cd "$(xfi | fzf --cycle --preview "[[ -d {} ]] && tree -C {} || head -200" --height 40% --reverse)"
# cd "$(find ~ -maxdepth 5 -not -path '*/\.git/*' -type d | fzf --preview 'tree -C {} | head -200' --height 40% --reverse)"
}
todo() {
if [[ $# -gt 1 ]]; then
local key="$1"
case "$key" in
--remove|-r)
grep -v -- "$2" ~/todo.md > tmpfile && mv tmpfile ~/todo.md
;;
*)
printf "%s\n" "ERROR: Unrecognized argument $key"
exit 1
;;
esac
elif [[ -z "$1" ]]; then
cat ~/todo.md
else
xargs -I TODO echo "- [ ] TODO" >> ~/todo.md <<< "$1"
fi
}
epoch() {
# script to ouput current unix epoch and convert to readable datetime
# TODO: check if gdate is available on Mac if not use date
# case "$(uname)" in
# Linux ) echo "on Linux" ;;
# Darwin ) echo "on Mac" ;;
# esac
case "$1" in
now)
date +'%s'
[ $(uname) == "Darwin" ] && date -r $(date -u +%s) || date --date="@$(date -u +%s)" ;;
later)
# e.g epoch later "10 days"
[ -z "$2" ] && echo "argument missing, 'epoch later <modifier>'" && return
date -d "$2"
date -u +%s -d "$2";;
*)
[ -z $1 ] && echo "argument missing, 'epoch <unix-timestamp>'" && return
[ $(uname) == "Darwin" ] && date -r "$1" || date --date="@$1";;
esac
}
pp() {
# pretty print $PATH
echo "${PATH//:/$'\n'}"
}
gga() {
gists -au Bhupesh-V | fzf | awk -F "," '{print $2}' | xargs -0 browse > /dev/null 2>&1
}
# gg() {
# gists -u Bhupesh-V | fzf | awk -F "," '{print $2}' | xargs -0 browse > /dev/null 2>&1
# }