-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
functions.fish
194 lines (156 loc) · 7.01 KB
/
functions.fish
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
# thx to https://github.com/mduvall/config/
function subl --description 'Open Sublime Text'
if test -d "/Applications/Sublime Text.app"
"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" $argv
else if test -d "/Applications/Sublime Text 2.app"
"/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" $argv
else if test -x "/opt/sublime_text/sublime_text"
"/opt/sublime_text/sublime_text" $argv
else if test -x "/opt/sublime_text_3/sublime_text"
"/opt/sublime_text_3/sublime_text" $argv
else
echo "No Sublime Text installation found"
end
end
function kill_process --description 'Kill process that user selects in fzf (from ps aux output)'
set -l pid (ps aux | fzf -m --header-lines=1 | awk '{print $2}')
if test -n "$pid"
echo "Killing processes: $pid"
kill -9 $pid
end
end
function kill_port --description 'Select a port to kill, by pid, port, or command line'
# Function to get the command line for a given PID
function get_command -a pid
ps -p $pid | awk 'NR>1 {for (i=4; i<=NF; i++) {printf "%s ", $i}; print ""}'
end
# Find listening processes, get commands, and format output
lsof -iTCP -sTCP:LISTEN -P | awk '{print $2, $9}' | uniq | tail -n +2 | \
while read -l pid port
set -l command (get_command $pid)
set -l port (string pad -w 8 (string replace 'localhost' '' $port))
set -l pid (string pad --right -w 6 $pid)
echo -e "$pid $port $command" | column
end | \
# Pipe the output to fzf for selection. Grab pid and show pstree
fzf --exact --tac --preview 'pstree -p (echo {} | awk "{print $2}")' --preview-window=down,30% --header "Select a process to kill (PID Command Port):" | \
# Kill the selected process
awk '{print $1}' | xargs -r kill -9
end
function clone --description "clone something, cd into it. install it."
git clone --depth=1 $argv[1]
cd (basename $argv[1] | sed 's/.git$//')
yarn install
end
function renameurldecode
for file in *
set -l original $file
set -l decoded (python3 -c "import urllib.parse; print(urllib.parse.unquote_plus('$original'))")
if test "$original" != "$decoded"
mv "$original" "$decoded"
end
end
end
function notif --description "make a macos notification that the prev command is done running"
# osascript -e 'display notification "hello world!" with title "Greeting" sound name "Submarine"'
osascript \
-e "on run(argv)" \
-e "return display notification item 1 of argv with title \"command done\" sound name \"Submarine\"" \
-e "end" \
-- "$history[1]"
end
function beep --description "make two beeps"
echo -e '\a'; sleep 0.1; echo -e '\a';
end
function all_binaries_in_path --description "list all binaries available in \$PATH, even if theres conflicts"
# based on https://unix.stackexchange.com/a/120790/110766 but tweaked to work on mac. and then made it faster.
find -L $PATH -maxdepth 1 -perm +111 -type f 2>/dev/null
#gfind -L $PATH -maxdepth 1 -executable -type f # shrug. probably can delete this.
end
function my_paths --description "list paths, in order"
echo "# "
printf '%s\n' (string split \n $PATH)
end
function stab --description "stabalize a video"
set -l vid $argv[1]
ffmpeg -i "$vid" -vf vidstabdetect=stepsize=32:result="$vid.trf" -f null -;
ffmpeg -i "$vid" -b:v 5700K -vf vidstabtransform=interpol=bicubic:input="$vid.trf" "$vid.mkv"; # :optzoom=2 seems nice in theory but i dont love it. kinda want a combo of 1 and 2. (dont zoom in past the static zoom level, but adaptively zoom out to full when possible)
ffmpeg -i "$vid" -i "$vid.mkv" -b:v 3000K -filter_complex hstack "$vid.stack.mkv"
# vid=Dalton1990/Paultakingusaroundthehouseagai ffmpeg -i "$vid.mp4" -i "$vid.mkv" -b:v 3000K -filter_complex hstack $HOME/Movies/"Paultakingusaroundthehouseagai.stack.mkv"
command rm $vid.trf
end
function md --wraps mkdir -d "Create a directory and cd into it"
command mkdir -p $argv
if test $status = 0
switch $argv[(count $argv)]
case '-*'
case '*'
cd $argv[(count $argv)]
return
end
end
end
# yes I love this gross combo of shell script, escapes, and node.
function gz --d "Get the gzipped size"
printf "%-20s %12s\n" "compression method" "bytes"
# TODO.. omg theres no need to go backwards. i can do this in 1 pass.
set origstr (printf "%-20s %'12.0f" "original" (cat "$argv[1]" | wc -c))
echo $origstr
set -l array "$origstr"
# -5 is what GH pages uses, dunno about others
# fwiw --no-name is equivalent to catting into gzip
set -a array (printf "%-20s %'12.0f" "gzipped (-5)" (cat "$argv[1]" | gzip -5 -c | wc -c)); echo $array[-1]
set -a array (printf "%-20s %'12.0f" "gzipped (--best)" (cat "$argv[1]" | gzip --best -c | wc -c)); echo $array[-1]
# brew install brotli to get these as well
if hash brotli
# googlenews uses about -5, walmart serves --best
set -a array (printf "%-20s %'12.0f\n" "brotli (-q 5)" (cat "$argv[1]" | brotli -c --quality=5 | wc -c)); echo $array[-1]
# set -a array (printf "%-20s %'12.0f\n" "brotli (--best)" (cat "$argv[1]" | brotli -c --best | wc -c)); echo $array[-1]
end
# brew install zstd to get these as well
if hash zstd
set -a array (printf "%-20s %'12.0f\n" "zstd (-3)" (cat "$argv[1]" | zstd -c -3 - | wc -c)); echo $array[-1]
set -a array (printf "%-20s %'12.0f\n" "zstd (--19)" (cat "$argv[1]" | zstd -c -19 - | wc -c)); echo $array[-1]
# set -a array (printf "%-20s %'12.0f\n" "zstd (--22 --ultra)" (cat "$argv[1]" | zstd -c -22 --ultra - | wc -c)); echo $array[-1]
end
sleep 0.05
for item in $array
# ANSI escape cursor movement https://tldp.org/HOWTO/Bash-Prompt-HOWTO/x361.html
printf "\033[1A" # up 1 row
end
set orig (string replace --all "," "" (string match --regex " [\d,]+" $origstr))
for item in $array
printf "$item "
set bytesnum (string replace --all "," "" (string match --regex " [\d,]+" $item))
echo "wid = $COLUMNS - 40; console.log('█'.repeat($bytesnum * wid / $orig) + '░'.repeat(wid - ($bytesnum * wid / $orig)))" | node
end
end
function sudo!!
eval sudo $history[1]
end
# `shellswitch [bash|zsh|fish]`
function shellswitch
chsh -s (brew --prefix)/bin/$argv
end
# requires my excellent `npm install -g statikk`
function server -d 'Start a HTTP server in the current dir, optionally specifying the port'
# arg can either be port number or extra args to statikk
if test $argv[1]
if string match -qr '^-?[0-9]+(\.?[0-9]*)?$' -- "$argv[1]"
set port $argv[1]
# fancy argv thing to pass all remaining args. eg `server --cors --jsprof`
statikk --open --port $argv[1..-1]
else
statikk --open $argv[1..-1]
end
else
statikk --open
end
end
function conda -d 'lazy initialize conda'
functions --erase conda
eval /opt/miniconda3/bin/conda "shell.fish" "hook" | source
# There's some opportunity to use `psub` but I don't really understand it.
conda $argv
end
# NVM doesnt support fish and its stupid to try to make it work there.