-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdot_commonfuncs.tmpl
More file actions
327 lines (275 loc) · 9.83 KB
/
dot_commonfuncs.tmpl
File metadata and controls
327 lines (275 loc) · 9.83 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# shellcheck shell=bash
makeMeLaugh() {
local output width term_width
if ! checkPath fortune; then
echo "No fortunes for you!"
return 0
fi
width=72
term_width=$(tput cols)
if [ "$term_width" -lt "$width" ]; then
width=$((term_width - 5))
fi
# display the fortune requested
if [ -n "$1" ]; then
output=$(fortune -e "$1")
elif [ -d "$HOME/.fortunes" ]; then
output=$(fortune -e "$HOME/.fortunes")
# try to run my custom comedy folder of goodness
elif [ -d /opt/local/share/games/fortune/comedy ] ||
[ -d /usr/share/games/fortune/comedy ] ||
[ -d /usr/share/games/fortunes/comedy ] ||
[ -d /usr/share/fortune/comedy ] ||
[ -d /usr/local/share/games/fortunes/comedy ]; then
output=$(fortune -e comedy)
# otherwise just any old fortune will do
else
output=$(fortune)
fi
# Spice it up if we can
if checkPath cowthink; then
output=$(cowthink -d -W "$width" "$output")
fi
echo -e "\n$output\n"
}
lulz() {
local VOICE
local msg
local comedian
while getopts "v:" OPTION
do
case $OPTION in
v)
VOICE=$OPTARG
shift $((OPTIND-1))
;;
?)
echo "Not a valid option"
exit 1
;;
esac
done
if [ -z "$VOICE" ]; then
VOICE="Fred"
fi
msg="I cannot make you have lulz"
if checkPath fortune && checkPath say; then
if [ -n "$1" ]; then
comedian="$1"
elif [ -d /opt/local/share/games/fortune/comedy_short ]; then
comedian=comedy_short
else
echo "$msg"
fi
if [ -n "$comedian" ]; then
# pipe through sed to get rid of the attribution line, then say it
fortune $comedian | sed -E '$,/^(-{1,2} |Deep Thoughts)/d' | say -v $VOICE
fi
else
echo "$msg"
fi
}
# helper function to search the $PATH for a given
# executable. useful for checks across different
# systems.
checkPath() {
which "$1" &>/dev/null
}
weShouldUpdate() {
local time_to_update
local time_marker
local last_mod
local current_time
local time_diff
# only update once a day
time_to_update=86400
# use a dot file as a marker
time_marker="$HOME/.dotfiles"
# if the file doesn't exist we'll end up touching it
last_mod=0
# stat is different between flavas
case $(uname) in
Linux) last_mod=$(stat -c %Y "$time_marker") ;;
FreeBSD|Darwin) last_mod=$(stat -f %a "$time_marker") ;;
esac
current_time=$(date +%s)
# see what the difference is in seconds
time_diff=$(( current_time - last_mod ))
if [ $time_diff -gt $time_to_update ]; then
touch "$time_marker"
echo "It's been a while since we updated the dotfiles..."
return 0
fi
return 1
}
# Git stash + fzf = awesome
stache() {
which fzf &>/dev/null || (echo "fzf is not installed. Install it and try again"; return)
local stash stashes stash_choices action opts show_all choice_count prev_action prev_choices
while getopts "a" OPTION; do
case $OPTION in
a) show_all="yes";;
esac
done
while true; do
# Get our list of stashes, cleaned up for human consumption
stashes=$(git stash list | sed -Ee 's/^stash@\{([^}]+)\}: (.*)/[\1] \2/')
if [[ -z "$show_all" ]]; then
# get only named stashes, this is the default
stashes=$(echo "$stashes" | grep -v WIP | sed -Ee 's/On [^:]+: //')
fi
stash_choices=$(echo "$stashes" | fzf -m -q "$prev_choices" --header="::::> choose a stash <::::$prev_action")
prev_choices=''
[[ $? -gt 0 ]] && return
stash_choices=$(echo "$stash_choices" | sed -En 's/\[(.*)\].*/\1/p')
# TODO: Ask for confirmation on destructive actions
action=$(echo "quit\nshow\napply\npop\ndrop" | fzf --header='::::> choose an action <::::')
# Bail out
[[ $? -gt 0 ]] && return
[[ "$action" == "quit" ]] && return
# Set extra options
if [[ "$action" == "show" ]]; then
opts='-p'
prev_choices=$(echo "$stash_choices" | sed -Ee 's/(.*)/^[\1] |/' | tr '\n' ' ' | sed 's/| $//')
fi
# Summary for the next round
choice_count=$(echo "$stash_choices" | wc -l | sed 's/ //g')
prev_action=$(echo "\n\n$action $choice_count item(s) complete")
# Loop over the selected items and perform the action
while read -r stash; do
git stash "$action" "$opts" stash@\{"$stash"\} || return
done <<< "$stash_choices"
# clear out opts that were set
opts=''
done
}
# Helper to clean up branches that were pushed, but no longer exist.
# The main reason for this is cleaning up branches that were squash and merged.
git_clean_remotes () {
comm -12 <(git remote prune -n origin | grep '\*' | sed 's/.* origin\///' | sort) <(git branch | grep -v "$(git br-main)" | sed 's/ //g' | sort) | vipe | xargs git branch -D
}
# Generate a pin code with pwgen
pwgen_pin () {
local length
local num
[ -n "$1" ] && length=$1 || length=4
[ -n "$2" ] && num=$2 || num=1
pwgen -Anr abcdefghijklmnopqrstuvwxyz $length $num
}
# requires neovim-remote python package and scripts in your path
vim_quit_all () {
local inst
local session_count
for inst in $(nvr --serverlist); do
nvr --servername "$inst" --remote-send ''
echo "closing $(nvr --servername "$inst" --remote-expr 'getcwd()')"
nvr --servername "$inst" --remote-send ':wqa<cr>'
done
session_count=$(nvr --serverlist | wc -l)
echo "$session_count vim instances left"
return "$session_count"
}
tmux_exit_all_idle () {
# close out any tmux pane that is running zsh (presumed idle)
tmux list-panes -a -F "#D #{pane_current_command}" | grep ' zsh$' | cut -d' ' -f1 | xargs -n1 -I{} tmux send-keys -t{} "exit" Enter
}
dark_mode () {
local inst
local bg_init
if [ "$1" = 'off' ]; then
bg_init=dark
dark_mode=false
else
bg_init=light
dark_mode=true
fi
# set the system dark mode
osascript -l JavaScript -e "Application('System Events').appearancePreferences.darkMode = $dark_mode" > /dev/null 2>&1
# Set all neovim instances to dark mode
for inst in $(nvr --serverlist); do
# Set the dark mode to the opposite of what we are trying to switch to so my custom ColorSwitcher function inverts it
nvr --servername "$inst" --remote-send ":set background=${bg_init}<cr>"
nvr --servername "$inst" --remote-send ':ColorSwitcher<cr>'
done
curl -X POST -H "Content-Type: application/json" -d "{ \"dark_mode\": \"$dark_mode\" }" {{ onepasswordRead "op://Private/Home Assistant/studio-meeting-webhook" }}
}
purge_setup() {
# Check if chezmoi is installed
if ! command -v chezmoi &> /dev/null; then
echo "Error: chezmoi is not installed or not in PATH"
return 1
fi
# Get the list of managed files
echo "Checking for chezmoi managed files..."
local file_count=$(chezmoi managed -i files 2>/dev/null | wc -l)
if [ "$file_count" -eq 0 ]; then
echo "No chezmoi managed files found."
return 0
fi
# Show what will be removed
echo -e "\nThe following $file_count file(s) will be removed:"
echo "----------------------------------------"
chezmoi managed -i files 2>/dev/null | head -20
if [ "$file_count" -gt 20 ]; then
echo "... and $(($file_count - 20)) more files"
fi
echo "----------------------------------------"
# Ask for confirmation
echo -e "\nAre you sure you want to remove ALL $file_count chezmoi managed files?"
echo -n "Type 'yes' to confirm: "
read confirmation
if [ "$confirmation" = "yes" ]; then
echo -e "\nRemoving chezmoi managed files..."
chezmoi managed -i files -0 2>/dev/null | xargs -0 -I{} rm -vf "{}"
echo -e "\nRunning chezmoi purge to clean up..."
chezmoi purge --verbose --force
echo -e "\nRemove teamocil config"
rm -rf ~/.teamocil
echo -e "\nRemove histories"
rm -f ~/.zsh_history ~/.bash_history ~/.python_history
if [ -d "$HOME/Documents/Obsidian" ]; then
open -a Obsidian
echo -e "\nAre you sure you want to remove Obsidian?"
echo -n "Type 'yes' to confirm: "
read confirm_obsidian
if [ "$confirm_obsidian" = "yes" ]; then
rm -rf "$HOME/Documents/Obsidian"
fi
echo -e "\nRemoved obsidian notes"
fi
op account forget --all
echo -e "\nLogged out of 1Password"
echo -e "\nDone! Removed $file_count files and cleaned up chezmoi state."
else
echo "Cancelled. No files were removed."
return 1
fi
}
{{- if eq .chezmoi.hostname "television" }}
hash -d H=/Volumes/theater
hash -d D=/Volumes/DELUGE/torrents
hash -d T=/Volumes/DELUGE/torrents
alias pms="/Applications/Plex\ Media\ Server.app/Contents/MacOS/Plex\ Media\ Scanner"
function copy_movie () {
local movie
local movie_path
movie=$(transmission-remote 9080 -l | sort | sed -E 's/.*(Stopped|Idle|Seeding|Downloading) +//' | sed '$d' | tac | fzf)
[ -z "$movie" ] && return
movie_path="/Volumes/DELUGE/torrents/$movie"
if [ -d "$movie_path" ];then
movie_path="$(find "$movie_path" | fzf)"
[ -z "$movie_path" ] && return
fi
echo $movie_path
cp -X "$movie_path" ~H/Movies/Movies/.
}
function beet_import () {
local album
local album_path
album=$(transmission-remote 9080 -l | sort | sed -E 's/.*(Idle|Seeding|Downloading|Stopped) +//' | sed '$d' | tac | fzf)
[ -z "$album" ] && return
album_path="/Volumes/DELUGE/torrents/$album"
echo $album_path
beet import "$album_path"
}
{{- end }}