-
Notifications
You must be signed in to change notification settings - Fork 20
/
.commonfuncs
228 lines (194 loc) · 6.67 KB
/
.commonfuncs
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
# 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
}