-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallshrc.sh
384 lines (338 loc) · 13.4 KB
/
allshrc.sh
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# When running directly on the linux ttys
if [[ "$TERM" == "linux" ]] ; then
case $(tty) in
/dev/tty10)
;;
/dev/tty[0-9]*)
# Try to turn on numlock, but make sure return code is 0 even if it
# doesn't work.
setleds -D +num && true
;;
*)
esac
fi
function show_git_status() {
local header RED GREEN BLUE YELLOW NC dir repo
header="WTree, Issue #, Stash, UnRel -- Repository"
RED='\033[0;31m'
GREEN='\033[1;32m'
BLUE='\033[1;34m'
YELLOW='\033[1;33m'
NC='\033[0m'
function getStats() {
local dir gitDir print printReason issues branchName upstream localChanges remoteChanges unreleasedChanges stashes MATCH MBEGIN MEND
dir="$1"
if [ -f "$dir" ] ; then
exit
fi
cd "$dir" || exit
gitDir=$(git rev-parse --git-dir 2>&1 || true)
if [[ "$gitDir" != ".git" ]] ; then
exit
fi
print=0
printReason=""
issues=""
if (git update-index --ignore-submodules --really-refresh > /dev/null && git diff-files --quiet --ignore-submodules && git diff-index --cached --quiet HEAD --ignore-submodules) ; then
issues="Clean,"
else
issues="${RED}Dirty${NC},"
print=1
fi
branchName="$(git rev-parse --abbrev-ref HEAD)"
upstream="$(git config --get "branch.$branchName.remote" || true)"
if [[ "$upstream" != "" ]]; then
upstream="$(git rev-list --left-right --count "$branchName...$upstream/$branchName" 2>/dev/null || true)"
fi
if [[ "$branchName" =~ issue/.* ]] ; then
branchName="#$(basename "$branchName")"
print=1
declare -R8 branchName
issues="$issues [${GREEN}$branchName${NC}],"
elif [[ "$branchName" =~ release/.* ]] ; then
if [[ "$branchName" =~ release/v.* ]] ; then
branchName="$(basename "$branchName")"
else
branchName="v$(basename "$branchName")"
fi
print=1
declare -R8 branchName
issues="$issues [${GREEN}$branchName${NC}],"
else
declare -L8 branchName
issues="$issues [$branchName],"
fi
stashes="$(wc -l "${dir}.git/logs/refs/stash" 2>/dev/null | xargs | cut -d ' ' -f 1)"
if [[ "$stashes" != "0" && "$stashes" != "" ]] ; then
print=1
declare -R3 stashes
issues="$issues [${BLUE}$stashes${NC}],"
else
declare -R3 stashes
issues="$issues [ 0],"
fi
if [[ "$branchName" != "main" ]] ; then
unreleasedChanges="$(git log HEAD ^origin/main --no-merges --oneline --no-show-signature 2>/dev/null | wc -l | sed 's/ //g')"
if [[ "$unreleasedChanges" != "0" && "$unreleasedChanges" != "" ]] ; then
print=1
declare -R3 unreleasedChanges
issues="$issues [${GREEN}$unreleasedChanges${NC}]"
else
issues="$issues [ 0]"
fi
else
issues="$issues [ 0]"
fi
if [[ "$upstream" != "" && "$upstream" != "$(echo -e '0\t0')" ]]; then
localChanges="$(echo "$upstream" | cut -f 1)"
remoteChanges="$(echo "$upstream" | cut -f 2)"
if [[ "$localChanges" == "0" && "$remoteChanges" != "0" ]] ; then
upstream="${RED}↓${NC} "
elif [[ "$localChanges" != "0" && "$remoteChanges" == "0" ]] ; then
upstream="${GREEN}↑${NC} "
else
upstream="${YELLOW}X${NC} "
fi
print=1
else
upstream=""
fi
if [[ "$issues" != "" && "$print" == "1" ]] ; then
echo "$issues -- $upstream$(basename "$dir")"
fi
}
for dir in ~/dev/* ~/gists/* ; do
repo=$(getStats "$dir")
if [[ "$repo" != "" ]] ; then
if [[ "$header" != "" ]] ; then
echo "$header"
header=""
fi
echo -e "$repo"
fi
done
}
# Add these so there's a place in the home directory to put binaries
export PATH="$HOME/.local/bin:$HOME/.local/lib:$PATH"
if [ -d "$HOME/.cargo/bin" ]; then
export PATH="$HOME/.cargo/bin:$PATH"
fi
# Turn off telemetry for "dotnet" commands
export DOTNET_CLI_TELEMETRY_OPTOUT=1
# Typo aliases for git
alias got=git
alias gti=git
alias gut=git
# Alias "todo" as the to-do script
alias todo="$HOME/dev/dotfiles/todo.sh"
cdgit(){
cd ~/dev/$1
echo -n -e "\033]0;$(basename "$(pwd)")\007"
}
localUname="$(uname -a)"
if [[ "$(uname)" == "Darwin" ]]; then
export HOMEBREW_NO_AUTO_UPDATE=1
export HOMEBREW_NO_INSTALL_CLEANUP=1
export HOMEBREW_BAT=1
export HOMEBREW_DISPLAY_INSTALL_TIMES=1
export HOMEBREW_NO_ANALYTICS=1
export HOMEBREW_NO_GITHUB_API=1
export HOMEBREW_NO_INSTALL_FROM_API=1
export HOMEBREW_NO_INSTALL_UPGRADE=1
export HOMEBREW_CASK_OPTS="--require-sha"
fi
# Alias for the logrepos script
alias logrepos=$HOME/dev/dotfiles/logrepos.sh
# This is a helper for use on NixOS, which grabs the git commit for the current
# environment, checks for updates, and then grabs the git commit for the updated
# environment. If the commits differ, it produces a GitHub URL where the changes
# can be reviewed. This is not quite as helpful as pacman's `VerbosePkgLists`
# option on Arch linux, but is good enough that I can see which things I might
# break by updating.
nix-check-update(){
local oldCommit, newCommit
oldCommit=$(sudo nix-instantiate --find-file nixpkgs)
oldCommit=$(dirname "$oldCommit")
oldCommit=$(readlink "$oldCommit" | rev | cut -d '.' -f 1 | rev | cut -d '/' -f 1)
sudo nix-channel --update nixos
newCommit=$(sudo nix-instantiate --find-file nixpkgs)
newCommit=$(dirname "$newCommit")
newCommit=$(readlink "$newCommit" | rev | cut -d '.' -f 1 | rev | cut -d '/' -f 1)
if [[ "${newCommit:-}" == "${oldCommit:-}" ]] ; then
echo "Nothing has changed"
else
echo "Changes can be reviewed here:"
echo "https://github.com/NixOS/nixpkgs-channels/compare/$oldCommit...$newCommit"
echo "If they look good, run:"
echo "sudo nixos-rebuild switch"
fi
}
# Function for starting tmux and configuring it for git on different machines
#
# This currently chooses its setup based on the console size, so that, e.g., all
# of my *nix boxes running fbterm with the same screen resolution will get the
# same configuration, while allowing my work machine can get a different one (I
# keep Terminal.app open on a 1920x1080 monitor rotated portrait at work).
tmuxgit(){
# This function has X stages:
#
# Stage 1 starts tmux
#
# Stage 2 configures panes based on the console size
#
# Stage 3 changes directory to the target location
# If TMUXGIT isn't set yet, move to stage 1
if [[ "${TMUXGIT:-}" == "" ]]; then
export TMUXGITPATH="$(pwd)"
# If the user gave an argument, and it's contents matches the name of a
# directory under ~/dev, then use that as the target path instead of the
# current directory
if [[ "${1:-}" != "" ]]; then
if [ -d "$HOME/dev/${1:-}" ]; then
export TMUXGITPATH="$HOME/dev/${1:-}"
printf '\e]1;%s\a' "dev/${1:-}"
fi
fi
export TMUXGITSIZE="$(stty size)"
# If already running under tmux, just set TMUXGIT=1 and start over
if [[ "${TMUX:-}" != "" ]]; then
export TMUXGIT=1
tmuxgit
# Otherwise, start tmux
else
if [[ "${1:-}" == "" ]]; then
TMUXGIT=1 tmux
else
TMUXGIT=1 tmux -L "${1:-}"
fi
fi
elif [[ "${TMUXGIT:-}" == "2" ]]; then
cd "$TMUXGITPATH"
export TMUXGIT=3
# If stage 1 just finished, start stage 2 by creating new panes and then
# setting TMUXGIT=2 (set it after creating a new pane so that each pane
# has to run through this too
elif [[ "${TMUXGIT:-}" == "1" ]]; then
# System-specifics here:
# This is my 1920x1080 monitor in portrait orientation at work. It gets
# three panes; a large top pane for `git diff` and `git lg1`, a smaller
# pane that I usually use for `git commit`, and then an even smaller one
# that I use for `git commit` when I find myself needing `git lg1` up
# top /and/ `git diff` in the middle pane. Weird, but it works for me.
if [[ "${TMUXGITSIZE:-}" == "123 152" ]]; then
if [[ "${TMUX_PANE:-}" == "%0" ]]; then
tmux split-window -v
tmux resize-pane -D 19
elif [[ "${TMUX_PANE:-}" == "%1" ]]; then
tmux split-window -v
tmux resize-pane -D 7
elif [[ "${TMUX_PANE:-}" == "%2" ]]; then
tmux select-pane -t 0
fi
# This is an old Dell laptop I found at a thrift store for about $20?
# Circa 2003, it's running a Pentium 4M and has a screen-size of
# 1280x800. Works well enough for little things. It gets two panes
# side-by-side, where the left one is for `git lg1` and `git diff`, and
# the right one is for `git commit`. Not sure what to do with the extra
# vertical space just yet? At any rate, I also only want this setup on
# a particular tty (I use 10 of them on that system). The rest of my
# ttys are likely to have neovim/vim open, which has its own
# split-screen abilities.
elif [[ "${TMUXGITSIZE:-}" == "66 213" && "${OLD_TTY:-}" == "/dev/tty1" ]]; then
if [[ "${TMUX_PANE:-}" == "%0" ]]; then
tmux split-window -h
tmux resize-pane -R 20
elif [[ "${TMUX_PANE:-}" == "%1" ]]; then
tmux select-pane -t 0
fi
else
echo "Uhh, your screen size isn't set-up yet?"
fi
export TMUXGIT=2
tmuxgit
fi
}
# If the shell just started and is partway through configuring the tmuxgit
# stuff, start it back up
if [[ "${TMUXGIT:-}" != "" ]]; then
tmuxgit
fi
# Function for starting tmux and configuring it for a "dashboard" on different
# machines
dashboard(){
# This function has X stages:
#
# Stage 1 starts tmux
#
# Stage 2 configures panes based on the console size
# IF TMUXDASHBOARD isn't set yet, move to stage 1
if [[ "${TMUXDASHBOARD:-}" == "" ]]; then
export TMUXDASHBOARDSIZE="$(stty size)"
printf '\e]1;%s\a' "dashboard"
# If already running under tmux, just set TMUXDASHBOARD=1 and start over
if [[ "${TMUX:-}" != "" ]]; then
export TMUXDASHBOARD=1
dashboard
# Otherwise, start tmux
else
TMUXDASHBOARD=1 tmux -L tmux_dashboard
fi
elif [[ "${TMUXDASHBOARD:-}" == "1" ]]; then
# System-specifics here:
# This is my 1920x1080 monitor in portrait orientation at work. It gets
# five panes; a large top pane for neomutt, a middle section divided
# into an unclaimed pane I use for terminal commands and a section that
# shows the output from `show_git_status`, and a bottom section divided
# into my current "to-do list" and the current weather/calendar.
if [[ "${TMUXDASHBOARDSIZE:-}" == "123 152" ]]; then
if [[ "${TMUX_PANE:-}" == "%0" ]]; then
if [[ "$(tmux list-panes | wc -l)" == "1" ]] ; then
tmux split-window -v
tmux split-window -v -t "%1"
tmux split-window -h -t "%1"
tmux split-window -h -t "%2"
tmux resize-pane -t "%0" -D 29
tmux resize-pane -t "%1" -D 16
tmux resize-pane -t "%1" -R 14
tmux resize-pane -t "%2" -R 44
tmux select-pane -t "%2"
tmux select-pane -t "%1"
tmux select-pane -t "%0"
neomutt
else
echo "Re-running the dashboard in this tmux pane would cause additional panes to be created. Just call 'neomutt' again."
fi
elif [[ "${TMUX_PANE:-}" == "%1" ]] ; then
sleep 1 ; clear
elif [[ "${TMUX_PANE:-}" == "%2" ]] ; then
sleep 1 ; $HOME/dev/dotfiles/todo.sh
elif [[ "${TMUX_PANE:-}" == "%3" ]] ; then
while [ 1 ]
do
output=$(show_git_status)
clear
echo "$output"
sleep 900
done
elif [[ "${TMUX_PANE:-}" == "%4" ]] ; then
while [ 1 ]
do
cal
echo
# 'q' disables the caption
# 'Q' disables the city name -- I know where I am
# '0' indicates that I want zero days' forecast
(wget -qO - "wttr.in/${WEATHER_LOCATION:-}\?q\&Q\&0" 2>/dev/null | sed 's/Thunderstorm/TStorm/' | sed 's/In Vicinity/Nearby/' || true)
sleep 900
done
fi
else
echo "Uhh, your screen size isn't set-up yet?"
fi
fi
}
# If the shell just started and is partway through configuring the dashboard
# stuff, just start it back up
if [[ "${TMUXDASHBOARD:-}" != "" ]]; then
dashboard
fi