-
Notifications
You must be signed in to change notification settings - Fork 0
/
rc
569 lines (485 loc) Β· 15.5 KB
/
rc
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#!/bin/bash
# Interactive shell detection {{{
# If either of these are true, the shell is non-interactive.
# Exit without changing any settings or printing any output.
[[ $- != *i* ]] && return
[ -z "${PS1}" ] && return
# }}}
# Configuration options {{{
prompt_hide_user="stephen"
auto_update_check_interval=30
tmux_env_update_check_interval=30
git_update_check_interval=7200 # 2 hours
[ -f ~/.dotfiles/.data/config-rc ] && source ~/.dotfiles/.data/config-rc
# }}}
# Helper functions {{{
__stat_mode()
{
if [ "$(uname -s)" = "Darwin" ]; then
stat -f '%A' "${@}"
else
stat -c '%a' "${@}"
fi
}
__stat_mtime()
{
if [ "$(uname -s)" = "Darwin" ]; then
stat -f '%m' "${@}"
else
stat -c '%Z' "${@}"
fi
}
# }}}
# Basic settings {{{
# Several of these are from the Gentoo/Ubuntu default bashrc.
# Increase session history size from the default 500
HISTSIZE=100000
# Bash won't get SIGWINCH if another process is in the foreground. Enable
# checkwinsize so bash will check the terminal size when it regains control.
# https://bugs.gentoo.org/show_bug.cgi?id=65623
# http://tiswww.case.edu/php/chet/bash/FAQ (E11)
# Disable terminal output pause/unpause
# This allows terminal applications to receive Ctrl+S
# http://unix.stackexchange.com/q/12107
stty -ixon
# }}}
# Terminal detection and color support {{{
[ -x /usr/bin/tput ] && __colors_supported=$(tput colors)
[ -z "${__colors_supported}" ] && __colors_supported=0
# Terminal detection is from an older version of Ubuntu's default bashrc
if [ "${TERM}" = "xterm" ] && [ -n "${COLORTERM}" ]; then
case "${COLORTERM}" in
gnome-terminal|mate-terminal|truecolor)
# Those crafty Gnome folks require you to check COLORTERM,
# but don't allow you to just *favor* the setting over TERM.
# Instead you need to compare it and perform some guesses
# based upon the value. This is, perhaps, too simplistic.
TERM="xterm-256color"
__colors_supported=256
;;
*)
echo "Warning: Unrecognized COLORTERM: $COLORTERM" >&2
;;
esac
fi
# }}}
# Shell prompt generator {{{
if [ -n "${BASH_VERSION}" ]; then
if [ ${__colors_supported} -ge 256 ]; then
__c_red="\\[$(tput setaf 196)\\]"
__c_green="\\[$(tput setaf 2)\\]"
__c_yellow="\\[$(tput setaf 227)\\]"
__c_orange="\\[$(tput setaf 202)\\]"
__c_blue="\\[$(tput setaf 4)\\]"
__c_purple="\\[$(tput setaf 96)\\]"
__c_github_codespace="\\[$(tput setaf 147)\\]"
else
__c_red="\\[$(tput setaf 1)\\]"
__c_green="\\[$(tput setaf 2)\\]"
__c_yellow="\\[$(tput setaf 3)\\]"
__c_orange="${__c_yellow}"
__c_blue="\\[$(tput setaf 4)\\]"
__c_purple="\\[$(tput setaf 5)\\]"
__c_github_codespace="${__c_purple}"
fi
__bold="\\[$(tput bold)\\]"
__reset="\\[$(tput sgr0)\\]"
elif [ -n "${ZSH_VERSION}" ]; then
if [ ${__colors_supported} -ge 256 ]; then
__c_red="%F{196}"
__c_green="%F{002}"
__c_yellow="%F{227}"
__c_orange="%F{202}"
__c_blue="%F{004}"
__c_purple="%F{096}"
__c_github_codespace="%F{147}"
else
__c_red="%F{001}"
__c_green="%F{002}"
__c_yellow="%F{003}"
__c_orange="${__c_yellow}"
__c_blue="%F{004}"
__c_purple="%F{005}"
__c_github_codespace="${__c_purple}"
fi
__bold="%B"
__reset="%b%u%s%f%k"
fi
# Color selection with fallback, to be used for host-specific prompt colors
__choose_color()
{
if [ ${__colors_supported} -ge 256 ]; then
tput setaf "${1}"
return
fi
tput setaf "${2}"
}
# Default user prompt colors
__c_prompt="${__c_green}${__bold}"
# Timer adapted from http://stackoverflow.com/a/1862762
__timer_start()
{
timer=${timer:-${SECONDS}}
}
# Converts number of seconds to human-readable time (ex. "1h 3m 30s")
__timer_formatter()
{
local div=
local str=
local mod=0
local count=${1}
for i in s m h d y; do
[ "${count}" -le 0 ] && break
case "${i}" in
y) div=0;;
d) div=365;;
h) div=24;;
m|s) div=60;;
esac
if [ "${div:-0}" -gt 0 ]; then
mod=$((count % div))
count=$((count / div))
str="${mod}${i}"
else
str="${count}${i}"
fi
done
echo "${str}"
}
__prompt_generator()
{
# Exit code
local exit_code=${?}
local exit_code_disp="${exit_code}"
local exit_color="${__c_red}${__bold}"
local dir_stack_count=$((${#DIRSTACK[@]} - 1))
local git_toplevel
git_toplevel=$(git rev-parse --show-toplevel 2>/dev/null)
# Stop command timer
local last_command_time=$((SECONDS - timer))
unset timer
# Prompt sections
local p_timer=
local p_exit=
local p_main=
local p_dirs=
local p_git=
local p_jobs=
local p_lastchar=
# Prompt codes
if [ "${ZSH_VERSION}" ]; then
local p_user="%n"
local p_host="%m"
local p_directory="%1d"
local p_jobs_count="%j"
else
local p_user="\u"
local p_host="\h"
local p_directory="\W"
local p_jobs_count="\j"
fi
# Last command run timer
if [ ${last_command_time} -ge 10 ]; then
p_timer="${__c_yellow}[$(__timer_formatter ${last_command_time})] "
fi
# Last command exit code
if [ ${exit_code} -ne 0 ]; then
case ${exit_code} in
130) exit_code_disp="^C"; exit_color="${__c_yellow}";;
148) exit_code_disp="bg"; exit_color="${__c_orange}";;
esac
p_exit="${exit_color}[${exit_code_disp}]${__reset} "
fi
# Main section
if [ -n "${GITHUB_CODESPACE_TOKEN}" ] && [ -n "${CODESPACES}" ]; then
# The GitHub Codespaces shell uses the root user.
# Use a different prompt color instead of red.
p_main="${p_main}${__c_github_codespace}";
p_main="${p_main}codespace/${p_host}${__reset} "
elif [ ${EUID} -eq 0 ]; then
p_main="${p_main}${__c_red}${__bold}${p_host}${__reset} "
else
p_main="${p_main}${__c_prompt}";
if [ "${USER}" != "${prompt_hide_user}" ]; then
p_main="${p_main}${p_user}@"
fi
p_main="${p_main}${p_host}${__reset} "
fi
# Current directory
if [ "${PWD}" != "${HOME}" ]; then
p_main="${p_main}${__bold}${__c_blue}${p_directory}${__reset} ";
fi
# Directory stack
if [ ${dir_stack_count} -gt 0 ]; then
p_dirs="${__c_blue}+${dir_stack_count} "
fi
# Git branch
if [ -n "${git_toplevel}" ] && [ "${git_toplevel}" != "${HOME}" ]; then
local git_branch
git_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ -n "${git_branch}" ] && [ "${git_branch}" != "main" ]; then
if [ "${git_branch}" = "HEAD" ]; then
git_branch=$(git rev-parse --short HEAD 2>/dev/null)
fi
if [ -n "${git_branch}" ]; then
p_git="${__c_purple}[${git_branch}] "
fi
fi
fi
# Background jobs
if [ -n "$(jobs -p)" ]; then
p_jobs="${__c_orange}+${p_jobs_count} "
fi
# Last character
if [ ${EUID} -eq 0 ]; then
p_lastchar="${__c_red}${__bold}#${__reset}"
else
p_lastchar="${__c_blue}${__bold}\$${__reset}"
fi
# Assemble prompt
PS1="${p_timer}${p_exit}${p_main}${p_dirs}${p_jobs}${p_git}${p_lastchar} "
if [ ${EUID} -eq 0 ]; then
PS2="${__bold}${__c_red}> ${__reset}"
else
PS2="${__bold}${__c_blue}> ${__reset}"
fi
# Set window title
local set_title=0
local title_prefix=
case ${TERM} in
xterm*|rxvt*|Eterm*|aterm|kterm|gnome*|mate*|interix)
set_title=1
;;
screen*)
set_title=1
if [ -n "${TMUX}" ]; then
title_prefix="$(tmux display-message -p "tmux:#S #I:#W") "
elif [ -n "${STY}" ]; then
title_prefix="screen:$(echo "${STY}" | sed -e 's/^[0-9]\+\.//g' \
-e "s/\\.${HOSTNAME}$//g") ${WINDOW} "
fi
;;
esac
if [ ${set_title} -ne 0 ]; then
echo -ne "\\033]0;${title_prefix}${USER}@${HOSTNAME%%.*} ${PWD/#$HOME/~}\\007"
fi
# Check for and process environment updates
__auto_update_check
if [ -n "${TMUX}" ] && [ -S "${TMUX%%,*}" ]; then
# Check for and process tmux environment updates
__tmux_env_update_check
fi
}
# Start command timer
[ -n "${BASH_VERSION}" ] && trap '__timer_start' DEBUG
[ -n "${ZSH_VERSION}" ] && preexec() { __timer_start; }
# Enable prompt generator
[ -n "${BASH_VERSION}" ] && PROMPT_COMMAND=__prompt_generator
[ -n "${ZSH_VERSION}" ] && precmd() { __prompt_generator; }
# }}}
# PATH configuration {{{
__prepend_path_if_exists()
{
# Remove any existing instances of directory from PATH
PATH=$(echo "${PATH}" | sed -e 's/:/\n/g' | grep -ve "^${1}\$" | xargs | tr " " ":")
# Prepend directory to PATH if directory exists
[ -d "${1}" ] && PATH="${1}:${PATH}"
return 0
}
__prepend_path_if_exists "/opt/homebrew/bin"
__prepend_path_if_exists "${HOME}/.dotfiles/bin"
__prepend_path_if_exists "${HOME}/.local/bin"
__prepend_path_if_exists "${HOME}/.ruby/bin"
__prepend_path_if_exists "${HOME}/bin"
# }}}
# Application settings {{{
# dotfiles repository configuration {{{
# Specify the data directory for dotfiles settings and state storage
export DOTFILES_DATA="${HOME}/.dotfiles/.data"
# }}}
# ls {{{
# Color configuration
if [ ${__colors_supported} -ge 2 ]; then
if [ "$(uname -s)" != "Darwin" ]; then
if [ ${__colors_supported} -ge 256 ] && [ -r ~/.dircolors ]; then
eval "$(dircolors -b ~/.dircolors)"
else
eval "$(dircolors -b)"
fi
fi
fi
# Use literal quoting style by default
export QUOTING_STYLE=literal
# }}}
# grep colors {{{
if [ ${__colors_supported} -ge 256 ]; then
# Set highlight color to bold orange
export GREP_COLORS="mt=1;38;5;202:se=0;38;5;245:fn=0;38;5;147:ln=0;38;5;70:bn=32"
else
export GREP_COLORS="mt=01;31:se=34:fn=35:ln=32:bn=32"
fi
# }}}
# Enable lesspipe {{{
# Make less more friendly for non-text input files, see lesspipe(1)
if [ -x /usr/bin/lesspipe ]; then
if grep -qe '^\s\+echo\ "export' "$(command -v lesspipe)"; then
eval "$(lesspipe)"
else
export LESSOPEN="|lesspipe %s"
fi
fi
# }}}
# Editor {{{
if command -v vim >/dev/null 2>&1; then
export EDITOR="vim"
fi
# }}}
# tmux environment update check {{{
__tmux_env_update_check()
{
local current_attach_time=
if [ "${SECONDS}" -lt "${__tmux_env_update_check_time-0}" ]; then
# The update check interval has not yet elapsed
return
fi
export __tmux_env_update_check_time=$((SECONDS+tmux_env_update_check_interval))
# Reload tmux environment if the tmux session socket's change time has
# changed
current_attach_time="$(tmux showenv __tmux_attach_time 2>/dev/null)"
if [ "${__tmux_env_reload_last_attach_time}" != \
"${current_attach_time}" ]; then
# Set desired environment variables from tmux
for env_var in \
DISPLAY \
SSH_AGENT_PID \
SSH_ASKPASS \
SSH_AUTH_SOCK \
SSH_CONNECTION \
WINDOWID \
XAUTHORITY \
; do
env_value=$(tmux showenv "${env_var}")
if [ "${env_value:0:1}" = "-" ]; then
unset "${env_var}"
continue
fi
env_value=$(echo "${env_value}" | cut -d= -f2-)
export "${env_var}"="${env_value}"
done
fi
export __tmux_env_reload_last_attach_time=${current_attach_time}
}
# }}}
# GnuPG configuration {{{
if [ "$(__stat_mode ~/.gnupg 2>/dev/null)" != "700" ]; then
chmod 0700 ~/.gnupg
chmod -R og-rwx ~/.gnupg
fi
GPG_TTY=$(tty)
export GPG_TTY
# }}}
# ssh configuration {{{
if [ -z "${SSH_CONNECTION}" ]; then
SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
export SSH_AUTH_SOCK
fi
# }}}
# ShellCheck configuration {{{
# Disable SC1090 (Can't follow non-constant source. Use a directive to specify
# location.) This silences warnings about source lines with non-absolute paths.
# Files should be checked individually.
export SHELLCHECK_OPTS="--exclude SC1090"
# }}}
# fzf configuration {{{
export FZF_DEFAULT_OPTS="
--color pointer:148
--color spinner:215
"
# }}}
# Ruby configuration {{{
export GEM_HOME="${HOME}/.ruby"
# }}}
# }}}
# Automatic update and reload {{{
# Partially based on:
# http://madebynathan.com/2012/10/29/auto-reloading-your-bashrc/
# https://github.com/ndbroadbent/dotfiles/blob/master/bashrc/auto_reload.sh
# Check for and process environment updates as part of PROMPT_COMMAND execution
__auto_update_check()
{
local rc_mod_time=
if [ "${SECONDS}" -lt "${__auto_update_check_time-0}" ]; then
# The update check interval has not yet elapsed
return
fi
export __auto_update_check_time=$((SECONDS+auto_update_check_interval))
# Check if auto update is disabled
[ -f "${DOTFILES_DATA}/disable_auto_update" ] && return
# Reload .bashrc if it or any file it sources has been modified
rc_mod_time=$(__stat_mtime "${__auto_reload_files[@]}" | sort | tail -n1)
if [ "${__auto_reload_last_modified-0}" -gt 0 ] &&
[ "${rc_mod_time}" -gt "${__auto_reload_last_modified}" ]; then
source ~/.dotfiles/shell/rc
fi
export __auto_reload_last_modified="${rc_mod_time}"
# Check for dotfiles repository updates
local git_check_time=
local update_ref_file="${DOTFILES_DATA}/update_ref"
local update_timestamp_file="${DOTFILES_DATA}/update_timestamp"
if [ -f "${update_ref_file}" ]; then
# If a previous check found an update, and it has not yet been applied,
# apply it
~/.dotfiles/bin/dotfiles-auto-update --quiet update \
--skip-ref="$(cat "${update_ref_file}")"
rm -f "${update_ref_file}"
else
git_check_time=$(__stat_mtime "${update_timestamp_file}" 2>/dev/null)
if [ "$(date +%s)" -ge \
"$((${git_check_time:-0}+git_update_check_interval))" ]; then
touch "${update_timestamp_file}"
~/.dotfiles/bin/dotfiles-auto-update --quiet --fork check \
--new-ref-file "${update_ref_file}"
fi
fi
}
# Temporarily alias . to record additional files sourced by .bashrc. These files
# will also be monitored for changes.
__auto_reload_source_alias()
{
for f in "${@}"; do
[[ "${f}" =~ ${HOME}/ ]] && __auto_reload_files+=("${f}")
done
builtin source "${@}"
}
__auto_reload_files=("${HOME}/.dotfiles/shell/rc")
export __auto_reload_files
alias .='__auto_reload_source_alias'
# }}}
# Load additional configuration {{{
__source_if_exists()
{
if [ -f "${1}" ]; then
. "${1}"
fi
}
# Aliases and helper functions
__source_if_exists ~/.dotfiles/shell/aliases
# Local configuration files
__source_if_exists ~/.local/shell-rc
__source_if_exists ~/.local/shell-aliases
# }}}
# Display basic settings on first invocation {{{
if [ ! -f "${DOTFILES_DATA}/welcome_message" ]; then
"${HOME}"/.dotfiles/bin/dotfiles-first-run
fi
# }}}
# Cleanup {{{
# Stop recording sourced file names for auto-reload detection
unset -f __auto_reload_source_alias
unalias .
# Unset local helper functions
unset -f __source_if_exists
unset -f __prepend_path_if_exists
unset -f __stat_mode
# }}}
# vim: set fdls=0 fdm=marker: