-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbash_functions.sh
More file actions
313 lines (270 loc) · 9.3 KB
/
Copy pathbash_functions.sh
File metadata and controls
313 lines (270 loc) · 9.3 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
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
#!/bin/bash
##################################
# UTILITY FUNCTIONS
#################################
function countdown() {
date1=$((`date +%s` + $1));
while [ "$date1" -ne `date +%s` ]; do
echo -ne "$(date -u --date @$(($date1 - `date +%s`)) +%H:%M:%S)\r";
sleep 0.1
done
}
function stopwatch() {
date1=`date +%s`;
while true; do
echo -ne "$(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S)\r";
sleep 0.1
done
}
function waitkey() {
(tty_state=$(stty -g)
stty -icanon
LC_ALL=C dd bs=1 count=1 >/dev/null 2>&1
stty "$tty_state"
) </dev/tty
}
function play_youtube(){
mplayer -fs -quiet $(youtube-dl -g -t "$1")
}
function play_youtube_audio(){
mplayer -vo null $(youtube-dl -g -t --extract-audio --audio-format mp3 "$1")
}
function play_youtube_mp3(){
wget -q -O - `youtube-dl -g -t --extract-audio $1`| ffmpeg -i - -f mp3 -vn -acodec libmp3lame -| mpg123 -
}
# Iterate through a youtube list of links and play only mp3
# PARAMS:
# $1 : URLS_FILE_PATH
# The URLS_FILE must respect that structure:
# YOUTUBE_URL_1
# YOUTUBE_URL_2
# ...
# YOUTUBE_URL_N
function yt_pl(){
COUNT=1;
for url in $(cat $1); do
echo "Playing $COUNT/$(wc -l $1) - $url";
COUNT=$((COUNT+1));
play_youtube_mp3 $url;
done
}
apk_install() {
find . -name *.apk | xargs -I {} adb install -r {}
}
open_dpms_dev_apk() {
adb shell monkey -p com.kaefer.pms.dev -c android.intent.category.LAUNCHER 1
}
open_dpms_demo_apk() {
adb shell monkey -p com.kaefer.pms.demo -c android.intent.category.LAUNCHER 1
}
open_dpms_apk() {
adb shell monkey -p com.kaefer.dpms -c android.intent.category.LAUNCHER 1
}
open_dpms_ea1_apk() {
adb shell monkey -p com.kaefer.dpms.stable.ea1 -c android.intent.category.LAUNCHER 1
}
#### Use it inside folder where the video is present!
## Converts a video (.mov) to gif (.gif).
## If no params is passed, the 'a.mov' video will be converted to 'a.gif' gif
## If a param is passed, the 'param.mov' video will be converted to a 'param.gif' gif
# Usage1: video_to_gif 'mymovie'
# Usage2: video_to_gif
video_to_gif() {
if [[ "${#1}" > 0 ]]; then
if [[ $((`ls | grep "$1" | wc -m`)) > 0 ]]; then
#ffmpeg -i "$1" -s 600x250 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=10 > "$1.gif";
#ffmpeg -i "$1" -s 660x350 -pix_fmt rgb24 -r 5 -f gif - | gifsicle --optimize=3 --delay=10 > "$1.gif";
if [ "$2" == "mobile" ]; then
ffmpeg -i "$1" -s 412x660 -pix_fmt rgb24 -r 5 -f gif - | gifsicle --optimize=3 --delay=10 > "$1.gif";
fi
if [ "$2" == "web" ]; then
ffmpeg -i "$1" -s 1220x700 -pix_fmt rgb24 -r 5 -f gif - | gifsicle --optimize=3 --delay=10 > "$1.gif";
fi
else
echo "There is no file named: $1";
fi
else
echo `ls | grep 'a.mov' | wc -m`;
if [[ $((`ls | grep 'a.mov' | wc -m`)) > 0 ]]; then
ffmpeg -i a.mov -s 919x443 -pix_fmt rgb24 -r 10 -f gif - | gifsicle --optimize=3 --delay=10 > a.gif;
else
echo 'There is no file named: a.mov';
fi
fi
# Greetings to...
# https://gist.github.com/vitorleal/563771e821cef668eef5
}
ffind() {
find . -name \*$1\*
}
multissh_tmux() {
if [ -z "$HOSTS" ]; then
echo -n "Please provide of list of hosts separated by spaces [ENTER]: "
read HOSTS
fi
local hosts=( $HOSTS )
local target="ssh-multi ${host[0]}"
tmux new-window -n "${target}" ssh ${hosts[0]}
unset hosts[0];
for i in "${hosts[@]}"; do
tmux split-window -t :"${target}" -h "ssh $i"
tmux select-layout -t :"${target}" tiled > /dev/null
done
tmux select-pane -t 0
if [ "$1" == "sync" ]; then
tmux set-window-option -t :"${target}" synchronize-panes on > /dev/null
fi
}
multissh_tmux_sync() {
if [ -z "$HOSTS" ]; then
echo -n "Please provide of list of hosts separated by spaces [ENTER]: "
read HOSTS
fi
local hosts=( $HOSTS )
local target="ssh-multi ${host[0]}"
if [ "$1" == "on" ]; then
tmux set-window-option -t :"${target}" synchronize-panes on > /dev/null
fi
if [ "$1" == "off" ]; then
tmux set-window-option -t :"${target}" synchronize-panes off > /dev/null
fi
}
redukt_tmux() {
local target="ssh-multi redukt"
tmux new-window -n "${target}" 'adb logcat -c && adb logcat | grep -E "Redukt> has start"'
tmux split-window -t :"${target}" -h 'adb logcat -c && adb logcat | grep -i objectbox'
tmux select-layout -t :"${target}" tiled > /dev/null
tmux split-window -t :"${target}" -h 'adb logcat -c && adb logcat | grep "has start \[SYNC_DONE"'
tmux select-layout -t :"${target}" tiled > /dev/null
tmux split-window -t :"${target}" -h 'adb logcat -c && adb logcat | grep "POST\|PUT\|DELETE"'
tmux select-layout -t :"${target}" tiled > /dev/null
tmux select-pane -t 0
}
## FZF - function utilities
# https://github.com/junegunn/fzf/wiki/examples#changing-directory
# fd - cd to selected directory
fd() {
local dir
dir=$(find ${1:-.} -path '*/\.*' -prune \
-o -type d -print 2> /dev/null | fzf +m) &&
cd "$dir"
}
# cf - fuzzy cd from anywhere
# ex: cf word1 word2 ... (even part of a file name)
# zsh autoload function
cf() {
local file
file="$(locate -Ai -0 $@ | grep -z -vE '~$' | fzf --read0 -0 -1)"
if [[ -n $file ]]
then
if [[ -d $file ]]
then
cd -- $file
else
cd -- ${file:h}
fi
fi
}
# using ripgrep combined with preview
# find-in-file - usage: fif <searchTerm>
fif() {
if [ ! "$#" -gt 0 ]; then echo "Need a string to search for!"; return 1; fi
rg --files-with-matches --no-messages "$1" | fzf --preview "highlight -O ansi -l {} 2> /dev/null | rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' || rg --ignore-case --pretty --context 10 '$1' {}"
}
# fh - repeat history
runcmd (){ perl -e 'ioctl STDOUT, 0x5412, $_ for split //, <>' ; }
fh() {
([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf --tac | sed -re 's/^\s*[0-9]+\s*//' | runcmd
}
# fhe - repeat history edit
writecmd (){ perl -e 'ioctl STDOUT, 0x5412, $_ for split //, do{ chomp($_ = <>); $_ }' ; }
fhe() {
([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf --tac | sed -re 's/^\s*[0-9]+\s*//' | writecmd
}
# fkill - kill processes - list only the ones you can kill. Modified the earlier script.
fkill() {
local pid
if [ "$UID" != "0" ]; then
pid=$(ps -f -u $UID | sed 1d | fzf -m | awk '{print $2}')
else
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
fi
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
fi
}
alias glNoGraph='git log --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr% C(auto)%an" "$@"'
_gitLogLineToHash="echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
_viewGitLogLine="$_gitLogLineToHash | xargs -I % sh -c 'git show --color=always % | diff-so-fancy'"
fco_preview() {
local tags branches target
branches=$(
git --no-pager branch --all \
--format="%(if)%(HEAD)%(then)%(else)%(if:equals=HEAD)%(refname:strip=3)%(then)%(else)%1B[0;34;1mbranch%09%1B[m%(refname:short)%(end)%(end)" \
| sed '/^$/d') || return
tags=$(
git --no-pager tag | awk '{print "\x1b[35;1mtag\x1b[m\t" $1}') || return
target=$(
(echo "$branches"; echo "$tags") |
fzf --no-hscroll --no-multi -n 2 \
--ansi --preview="git --no-pager log -150 --pretty=format:%s '..{2}'") || return
git checkout $(awk '{print $2}' <<<"$target" )
}
# fcoc_preview - checkout git commit with previews
fcoc_preview() {
local commit
commit=$( glNoGraph |
fzf --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview="$_viewGitLogLine" ) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# fshow_preview - git commit browser with previews
fshow_preview() {
glNoGraph |
fzf --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview="$_viewGitLogLine" \
--header "enter to view, alt-y to copy hash" \
--bind "enter:execute:$_viewGitLogLine | less -R" \
--bind "alt-y:execute:$_gitLogLineToHash | xclip"
}
# mem java - display the total memory used by a process
mem() {
ps -C "$1" -O rss | awk '{ count ++; sum += $2 }; END {count --; print "Number of processes:\t",count; print "Mem. usage per process:\t",sum/1024/count, "MB"; print "Total memory usage:\t", sum/1024, "MB" ;};';
}
# j - fuzzy jump to directory
j() {
local preview_cmd="ls {2..}"
if command -v exa &> /dev/null; then
preview_cmd="exa -l {2}"
fi
if [[ $# -eq 0 ]]; then
cd "$(autojump -s | sort -k1gr | awk -F : '$1 ~ /[0-9]/ && $2 ~ /\s*\// {print $1 $2}' | fzf --height 40% --reverse --inline-info --preview "$preview_cmd" --preview-window down:50% | cut -d$'\t' -f2- | sed 's/^\s*//')"
else
cd $(autojump $@)
fi
}
fpass() {
#pass $(find ~/.password-store/ | sed 's/.*.password-store\///g' | sed 's/.gpg//g' | sort | uniq | fzf --reverse --height 40% --preview="pass {1}")
pass $(find ~/.password-store/ | sed 's/.*.password-store\///g' | sed 's/.gpg//g' | sort | uniq | fzf --reverse)
}
echolor() {
echo -e "\e[1;31m$1\e[0m"
}
#!/bin/bash
# Converts JSON to GraphQL Input Syntax
json2graphql() {
jq -r '
def convert:
if type == "object" then
"{ " + (to_entries | map(.key + ": " + (.value | convert)) | join(" ")) + " }"
elif type == "array" then
"[ " + (map(convert) | join(" ")) + " ]"
elif type == "string" then
@json
else
tostring
end;
convert
'
}