-
Notifications
You must be signed in to change notification settings - Fork 0
/
myshrc
886 lines (801 loc) · 24.3 KB
/
myshrc
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
#! /bin/bash
# Allen's unified custom shell config and alias script for bash and zsh
# Shell Options
export FIGNORE=".o:.pyc:~"
export EDITOR="vim"
set -o ignoreeof
# color and smart-case search for less
export LESS=Ri
# by default systemctl/journalctl sets LESS to FRSXMK,
# remove X (disable termcap alternate screen) to enable mouse scrolling
# remove S to allow line wrapping
# add i for ignore case search
export SYSTEMD_LESS=FRMKi
# Section search order for man pages. The default is
# "1 n l 8 3 0 2 5 4 9 6 7", but move 2 before 3 so that Linux-specific
# syscall pages are preferred to the section 3p generic posix pages, and
# move n to the end because tcl manpages have no need to be that high-precedence.
export MANSECT='1,l,8,2,3,0,5,4,9,6,7,n'
if [[ -t 1 ]]; then
# if we're in a terminal, disable C-S and C-Q flow control handling
stty -ixon
fi
# pathadd: add an entry to a colon-separated list, only if such an entry isn't
# already present. It's also checked that 'newpath' exists and is a directory,
# unless the 'always' option is given.
# usage: pathadd varname newpath [options]
# varname: shell variable name, such as PATH or MANPATH
# newpath: new path entry to add
# options: any of
# append: add newpath to the end of varname rather than the beginning
# trailing_colon: leave a ':' at the end of the new value of the variable,
# this is the default if varname is MANPATH
# always: add the entry even if 'newpath' doesn't exist or isn't a directory.
function pathadd {
local varname="$1"
local newpath="$2"
[[ -n "$varname" && -n "$newpath" ]] || return 1
shift 2
local append=0
local trailing_colon=0
local always=0
while (( $# )); do
case "$1" in
append) append=1 ;;
trailing_colon) trailing_colon=1 ;;
always) always=1 ;;
*) echo >&2 "pathadd: unknown option '$1'"; return 1 ;;
esac
shift
done
if (( ! always )) && [[ ! -d "$newpath" ]]; then
return 0
fi
# automatically enable for MANPATH
if [[ "$varname" == "MANPATH" ]]; then
trailing_colon=1
fi
local oldval
# indirect expansion is different for bash & zsh
if [[ -n "$ZSH_NAME" ]]; then
oldval="${(P)varname}"
else
oldval="${!varname}"
fi
# strip leading & trailing colons
oldval="${oldval#:}"
oldval="${oldval%:}"
local newval
if [[ ":${oldval}:" == *":${newpath}:"* ]]; then
# newpath already is present
return 0
elif (( append )); then
newval="${oldval}:${newpath}"
else
newval="${newpath}:${oldval}"
fi
# strip colons again
newval="${newval#:}"
newval="${newval%:}"
if (( trailing_colon )); then
newval="${newval}:"
fi
# set the variable in global scope
typeset -g "${varname}=${newval}"
}
pathadd PATH "$HOME/bin"
pathadd PATH "$HOME/scripts"
pathadd PATH "$HOME/.cargo/bin"
_rustup_dir="${RUSTUP_HOME:-${HOME}/.rustup}/toolchains/stable-$(uname -m)-unknown-linux-gnu"
pathadd MANPATH "$_rustup_dir/share/man"
if [[ -n $ZSH_NAME && -d "$_rustup_dir/share/zsh" ]]; then
fpath=("$_rustup_dir/share/zsh" "${fpath[@]}")
fi
unset _rustup_dir
pathadd MANPATH "$HOME/share/man"
pathadd MANPATH "$HOME/.local/share/man"
[[ -n "$MANPATH" ]] && export MANPATH
# function to filter out paths
# $1 : variable to check (e.g. PATH)
# $2 : regex applied to each PATH entry
function path_filter {
# filter out paths matching this regex
local OLDPATH
eval OLDPATH="\$$1"
local NEWPATH=''
while read -r -d: entry; do
if ! [[ "$entry" =~ "$2" ]]; then
NEWPATH="$NEWPATH:$entry"
fi
done <<<"${OLDPATH}:"
eval $1="'${NEWPATH#:}'"
}
[[ -d /mnt/c/Windows ]] && path_filter PATH '/mnt/c/'
[[ -d /cygdrive ]] && path_filter PATH '/cygdrive/c/(Python|Dwimperl)'
# colors for ls
[[ -e ~/.dircolors ]] && eval `dircolors ~/.dircolors`
# Core aliases
alias 0='cd "$PWD"'
alias ls='ls --color=auto --group-directories-first'
alias lsl='ls -alh --color=auto --group-directories-first'
alias df='df -h'
alias du='du -h'
alias af='ag --nocolor -lG'
function lspath {
local _pathvar="${1:-PATH}"
eval "echo \$$_pathvar" | tr ':' '\n'
}
function psag {
local __grep
if which ag &>/dev/null; then
__grep=ag
else
__grep='egrep --color=auto'
fi
local __pattern="$1"
if [[ "${1:0:1}" =~ ^[A-Za-z0-9/] ]]; then
__pattern="[${1:0:1}]${1:1}"
fi
ps aux | $__grep -- "$__pattern"
}
function man {
# pretty man pages
# mb = blink (rarely seen)
# md = bold (bold, section headings, and option names)
# so = standout (reverse video, status line)
# us = underline (e.g. option arguments)
LESS_TERMCAP_mb=$'\e[1;36m' \
LESS_TERMCAP_md=$'\e[1;37m' \
LESS_TERMCAP_so=$'\e[1;33m' \
LESS_TERMCAP_us=$'\e[1;4m' \
LESS_TERMCAP_me=$'\e[0m' \
LESS_TERMCAP_se=$'\e[0m' \
LESS_TERMCAP_ue=$'\e[0m' \
command man "$@"
}
# rust (cargo) aliases
if which cargo &>/dev/null; then
alias cb='cargo build'
alias cbr='cargo build --release'
alias cbc='cargo check'
alias cbt='cargo test'
alias cr='cargo run -q --'
alias crr='cargo run --release -q --'
fi
if which bat &>/dev/null; then
# env vars for bat(1) rather than ~/.config/bat/config
export BAT_STYLE='plain'
export BAT_THEME='Solarized (dark)'
fi
if which fzf &>/dev/null; then
export FZF_DEFAULT_OPTS='--bind ctrl-a:select-all+accept --height=30 --reverse'
fi
if which sk &>/dev/null; then
export SKIM_DEFAULT_OPTIONS='--bind ctrl-a:select-all+accept --height=30 --reverse'
if sk --help 2>/dev/null | grep -qF -- --auto-height; then
SKIM_DEFAULT_OPTIONS="$SKIM_DEFAULT_OPTIONS --auto-height"
fi
fi
if which rg &>/dev/null; then
export RIPGREP_CONFIG_PATH="${__linuxfiles_dir:-${HOME}/linuxfiles}/ripgreprc"
fi
# sudo aliases
if [[ ! -d /cygdrive ]] && [[ $USER != root ]] && ( sudo -knv </dev/null &>/dev/null ); then
_sudo_aliases=(apt apt-get pacman)
for _cmd in "${_sudo_aliases[@]}"; do
if /usr/bin/which "$_cmd" &>/dev/null; then
alias "$_cmd"="sudo $_cmd"
fi
done
unset _sudo_aliases _cmd
alias ip='sudo ip -color=auto'
elif [[ ! -d /cygdrive ]]; then
alias ip='ip -color=auto'
fi
# Conditional aliases
# ubuntu-specific
[[ -d /etc/update-motd.d ]] && alias update-motd='sudo run-parts /etc/update-motd.d | sudo tee /run/motd.dynamic'
[[ -x /usr/lib/update-notifier/update-motd-reboot-required ]] && alias rboot='/usr/lib/update-notifier/update-motd-reboot-required || echo "No Update Needed"'
alias findmod='find /lib/modules/$(uname -r) -name "*.ko" | grep -i'
# git aliases/functions (alphabetical)
alias gam='git commit -v --amend'
alias gb='git branch'
alias gba='git branch -a'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gcam='git commit -a -m'
alias gcm='git commit -m'
alias gd='git diff'
alias gdh='git diff HEAD'
alias gdn='git diff --name-status'
alias gds='git diff --staged'
function gdiffall {
(
gbase
gvim -c "set diffopt=filler,vertical | tabdo :Gdiff $1" -p $(git diff --name-only $1)
)
}
alias gfp='git format-patch -N'
alias gl='git pull'
alias glr='git pull --rebase'
alias glg='git log --oneline --decorate --color --graph'
alias glga='git log --oneline --decorate --color --graph --all'
alias glgu='git log --oneline --decorate --color --graph @{u}..'
alias gll='git log --decorate --color --graph --name-status'
alias glla='git log --decorate --color --graph --name-status --all'
alias gllp='git log --decorate --color --graph --patch'
alias gllu='git log --name-status --decorate --color @{u}..'
alias gllup='git log --patch --decorate --color @{u}..'
alias gp='git push'
alias gri='git rebase -i'
alias grv='git remote -v'
alias gs='git status'
alias gss='git status --short'
alias gsh='git show --decorate'
alias gshn='git show --name-status --decorate'
if which less &>/dev/null; then
if (( $(less --version 2>/dev/null | sed -nr 's/^less ([0-9]+).*/\1/p') >= 530 )); then
# less version 530 and later handles -F properly
export GIT_PAGER='less -FRi'
elif which smartless &>/dev/null; then
# use smartless wrapper script for older distros (inefficient, but usually works)
export GIT_PAGER='smartless'
else
# fall back to just always paging even if one screen
export GIT_PAGER='less -Ri'
fi
else
# less isn't installed? oof...
unset GIT_PAGER
fi
# wrap gitk in a function to automatically run it in the background
function gitk {
command gitk "$@" &
}
function gbase {
local dir=$(git rev-parse --show-toplevel 2>/dev/null)
[[ -n $dir ]] && cd $dir
}
# Core functions
function mdcd {
mkdir -p "$1" && cd "$1"
}
function cdh {
[[ -n "$1" ]] || return 1
local _dir
while read _dir; do
if grep -q "$1" <<<"$_dir"; then
cd "$_dir"
return 0
fi
done < <(dirs -v | cut -f2)
echo "No history dir matching '$1' found"
return 1
}
unalias ll 2>/dev/null
function ll {
ls -lAh --color=always --group-directories-first "$@" | sed 's/[ ]\+/ /g' | cut -d' ' -f9-
}
function up {
local dir="$(dirname "$PWD")"
while [[ "$dir" != / ]]; do
if [[ "${dir##*/}" =~ "$1" ]]; then
cd "$dir"
return 0
fi
dir="$(dirname "$dir")"
done
echo "dir matching pattern not found: $1"
return 1
}
alias ff='selectf -A'
alias ffi='selectf -Ai'
# fz aliases. These only work when fd and fzf are installed, but the fz script checks that
alias vimz='fz vim -p -- :: -tf'
alias gvimz='fz gvim -p -- :: -tf'
alias bz='fz bat --style=header-filename,grid -- :: -tf'
# cdz: like cdf but using fz rather than selectf
function cdz {
local __dir
__dir="$(fz --no-multi echo :: -td "$@")"
[[ -n "$__dir" ]] && [[ -d "$__dir" ]] && cd "$__dir"
}
function findpath {
local _saveifs=$IFS
IFS=:
local _patharr=($PATH)
IFS=$_saveifs
if [[ "$1" == -* ]]; then
find "${_patharr[@]}" "$@"
elif [[ "$1" == *\** ]]; then
# If pattern includes a *, use it as-is with -name
find "${_patharr[@]}" -name "$@"
else
# if pattern doesn't have a *, wrap it with them
local _name="*$1*"
shift
find "${_patharr[@]}" -name "$_name" "$@"
fi
}
function cdf {
local __dir=$(selectf -dp "$@")
[[ -n "$__dir" ]] && [[ -d "$__dir" ]] && cd "$__dir"
}
alias cdfr="cdf -r"
alias cdfi="cdf -i"
alias cdfri="cdf -ri"
function walltime {
local starttime=$(date -u +%s)
"$@"
local ret=$?
local endtime=$(date -u +%s)
local diff=$(($endtime - $starttime))
local min=$(($diff / 60))
local sec=$(($diff % 60))
local hour_s
if (( $min >= 60 )); then
hour_s="$(($min / 60))h "
min=$(($min % 60))
else
hour_s=''
fi
local R=$'\033[0m'
if [[ $ret == 0 ]]; then
local C=$'\033[32m' # green
else
local C=$'\033[31m' # red
fi
printf >&2 "\n $C'${*#command }' returned $ret in ${hour_s}${min}m ${sec}s$R\n"
return $ret
}
alias nicebuild='walltime schedtool -n10 -Be'
# rehash after any pip install
function pipwrap {
local _pip_cmd="$1"
shift
local _need_rehash=0
if [[ "$1" == install ]]; then
shift
command $_pip_cmd install --no-warn-script-location "$@" || return
echo >&2 "$(basename "$SHELL"): rehashing"
hash -r
else
command $_pip_cmd "$@"
fi
}
for _pipname in pip pip2 pip3; do
eval "${_pipname}() { pipwrap $_pipname \"\$@\" ; }"
done
unset _pipname
function waitproc {
if [[ -z $1 ]]; then
echo >&2 "Need to specify a PID!"
return 1
fi
while [[ -d /proc/$1 ]]; do
sleep 2
done
}
# tmx function usually duplicated in ~/bin/tmx
function tmx {
local args=()
if [[ -n "$1" ]]; then
args+=(-t "$1")
fi
if tmux list-sessions &>/dev/null; then
echo "attaching to session"
tmux attach "${args[@]}"
else
echo "starting new session"
tmux new
fi
}
# diff the hexdump of 2 files in vimdiff
# Warning: could be slow if the files are big
function bindiff {
if [[ "$1" == "-g" ]]; then
local _vimdiff_cmd=gvimdiff
shift
else
local _vimdiff_cmd=vimdiff
fi
if [[ $# != 2 ]]; then
echo "usage: bindirr <file 1> <file 2>"
return 1
fi
if [[ ! -f "$1" ]]; then
echo "Error: $1 doesn't exist or isn't a file"
return 1
fi
if [[ ! -f "$2" ]]; then
echo "Error: $2 doesn't exist or isn't a file"
return 1
fi
$_vimdiff_cmd <(hexdump -C "$1") <(hexdump -C "$2")
}
alias gbindiff="bindiff -g"
function b64 {
local _opt=''
if [[ "$1" == '-d' ]]; then
_opt='-d'
shift
fi
if [[ -n "$1" ]]; then
# use command substitution to strip trailing newlines from base64 decode,
# then echo it with a trailing newline.
echo "$(printf '%s' "$*" | base64 $_opt)"
else
base64 $_opt
fi
}
# pva: python venv activate
# semi-intelligent shortcut for '. ./venv/bin/activate'
# that can also create venvs
function pva {
local _venv=./venv
local _force=0
local _make=0
local _python='python3'
local _opt
while getopts 'hdfmp:' _opt; do
case "$_opt" in
h)
echo "usage: $0 [-h] [-f] [[-m] [-p PYTHON]] [venv_dir]"
echo "Activate a python virtualenv, and optionally make a new one"
echo "venv_dir should be the venv directory, containing a bin/activate script to source"
echo "Options:"
echo " -h show this help"
echo " -d deactivate: just run 'deactivate' and exit"
echo " -f force: deactivate first if in a virtualenv. With -m, delete the venv and re-create"
echo " -m make: make a venv if it doesn't exist"
echo " -p PYTHON - use PYTHON command instead of 'python3'"
return 0
;;
d)
if [[ -z "$VIRTUAL_ENV" ]]; then
echo "Not in a virtualenv"
return 1
fi
deactivate
return $?
;;
f) _force=1 ;;
m) _make=1 ;;
p) _python="$OPTARG" ;;
\?) return 1 ;;
esac
done
shift $((OPTIND-1))
if (( $# )); then
_venv="$1"
shift
fi
if [[ -n "$VIRTUAL_ENV" ]]; then
if (( $_force )); then
deactivate || return 1
else
echo "already in venv and -f not given"
return 0
fi
fi
if (( $_make )); then
if [[ -d "$_venv" ]]; then
if (( $_force )); then
echo "removing '$_venv'"
rm -rf "$_venv"
else
echo "refusing to overwrite existing venv without -f"
return 1
fi
fi
echo >&2 "+ $_python -m venv '$_venv'"
"$_python" -m venv "$_venv"
fi
source "$_venv/bin/activate"
}
####### Bitbake/Repo/Yocto Aliases/Functions #######
# download Google's git-repo tool
function install-repo-tool {
if [[ -n $1 ]]; then
local _dlpath=$1
else
local _dlpath="$PWD/repo"
fi
[[ -d $_dlpath ]] && _dlpath=${_dlpath%/}/repo
if [[ $_dlpath = /usr/* ]]; then
local _sudo=sudo
else
local _sudo=
fi
(
set -e
$_sudo wget -O $_dlpath https://storage.googleapis.com/git-repo-downloads/repo
$_sudo chmod 755 $_dlpath
)
if [[ $? == 0 ]]; then
echo "Repo successfully installed to $_dlpath"
else
echo "Failed to install repo to $_dlpath"
fi
}
function rbase {
local repodir=$PWD
while true; do
if [[ -d $repodir/.repo ]]; then
cd $repodir
return 0
elif [[ $repodir == / ]]; then
gbase
if (( $? )); then
echo >&2 "no .repo or .git dir found"
return 1
else
return 0
fi
else
repodir=$(dirname $repodir)
fi
done
}
function base {
if [[ -d "$TOP" && "$PWD/" == "$TOP"/* ]]; then
cd "$TOP"
return
fi
rbase
}
function clearbbpath {
# remove previous bitbake bin paths from PATH
path_filter PATH 'openembedded-core'
}
function setbbpath {
if [[ ! -d $TOP ]]; then
echo "TOP not set or invalid! ($TOP)"
return 1
fi
local _oe_candidates=( "$TOP" \
"$TOP/openembedded-core" \
"$TOP/yocto/openembedded-core" \
)
local _oec
for _oec in "${_oe_candidates[@]}"; do
if [[ -x "$_oec/bitbake/bin/bitbake" ]]; then
local _oedir="$_oec"
break
fi
done
if [[ -d "$_oedir" ]]; then
clearbbpath
export PATH=$_oedir/scripts:$_oedir/bitbake/bin:$PATH
else
echo "Bitbake not found! (TOP=$TOP)"
return 1
fi
}
function bb {
if ( ! which bitbake &>/dev/null ); then
setbbpath || return 1
echo >&2 "Updated PATH"
fi
walltime command bitbake "$@"
}
alias bbclean='bb -c cleansstate'
for __bbalias in fetch unpack patch configure compile install; do
alias "bb$__bbalias"="bb -c $__bbalias"
eval "$(echo "function bbre$__bbalias { bbclean \"\$@\" && bb -c $__bbalias \"\$@\"; }")"
done
unset __bbalias
function bbrebuild {
bbclean "$@" && bb "$@"
}
function bbenv {
local vim="vim"
if [[ $1 == -g ]]; then
vim="gvim"
shift
fi
if ( ! which bitbake &>/dev/null ); then
setbbpath || return 1
echo >&2 "Updated PATH"
fi
if [[ -t 1 ]]; then
bitbake -e "$@" | $vim -R -c 'set ft=bitbake' -
else
bitbake -e "$@"
fi
}
function localconf-parallel {
if [[ $1 == -j* ]]; then
local __parallel=$(sed 's/-j\(.*\)/\1/' <<<$1)
shift
else
local __cpus=$(grep -c ^processor /proc/cpuinfo)
if (( $__cpus < 16 )); then
local __parallel=$__cpus
else
local __parallel=16
fi
fi
if [[ $# == 0 ]]; then
if [[ -f ./conf/local.conf ]]; then
local __files=(./conf/local.conf)
else
echo >&2 "Error: no file specified and can't find conf/local.conf"
return 1
fi
else
local __files=("$@")
fi
sed -i.bak -e 's/^\(BB_NUMBER_THREADS\).*/\1 = "'$__parallel'"/' \
-e 's/^\(PARALLEL_MAKE\).*/\1 = "-j'$__parallel'"/' \
"${__files[@]}"
}
# set WORKSPACES_ROOT
__TRY_WORKSPACES=( "/export/$USER" "/workspace" )
for __ws in "${__TRY_WORKSPACES[@]}"; do
if [[ -d $__ws ]]; then
WORKSPACES_ROOT=$__ws
break
fi
done
unset __TRY_WORKSPACES __ws
function ebase {
[[ -d "$WORKSPACES_ROOT" ]] && cd "$WORKSPACES_ROOT"
}
function sview_workspace_list {
local _name
local _found=0
local _all=0
if [[ "$1" == "-a" ]]; then
_all=1
shift
fi
local _pattern="$1"
[[ -n "$_pattern" ]] || _pattern='.'
# important optimizations to make this function fast enough to be "instant"
# * only one grep command total rather than one grep per workspace candidate
# * filter workspace names before checking for .git/.repo existence to limit fs syscalls
# Doing this the "slow" way by checking for workspace validity first and grepping each
# individually makes this take a couple hundred ms.
while read _name; do
local _ws="$WORKSPACES_ROOT/$_name"
if [[ ( "$_all" == "1" && -d "$_ws" ) || \
( ( -e "$_ws/.git" || -e "$_ws/.repo" || -e "$_ws/.sview" ) && \
! -e "$_ws/.no-sview" ) ]]; then
echo "$_name"
_found=1
fi
done < <(ls "$WORKSPACES_ROOT" | grep -Ei -- "$_pattern")
if [[ "$_found" == 0 ]]; then
echo >&2 "No matching workspaces found"
return 1
fi
}
if which sk &>/dev/null; then
# new and improved sview function that uses skim to select a workspace
function sview {
if [[ ! -d "$WORKSPACES_ROOT" ]]; then
echo "WORKSPACES_ROOT '$WORKSPACES_ROOT' doesn't exist!"
return 1
fi
local _name
_name="$(sview_workspace_list "$@" | sk --exit-0 --select-1 --prompt='Select Workspace> ')"
[[ -n "$_name" ]] || return 1
export TOP="${WORKSPACES_ROOT}/${_name}"
if ! [[ "$PWD" == "$TOP" || "$PWD" == "$TOP"/* ]]; then
cd "$TOP"
fi
}
else
# old sview function that uses the shell's `select` builtin
function sview {
if [[ ! -d "$WORKSPACES_ROOT" ]]; then
echo "WORKSPACES_ROOT '$WORKSPACES_ROOT' doesn't exist!"
return 1
fi
# Warning: this won't work right if there's workspaces that have spaces in their names,
# good thing that's not something I ever actually do.
local choices
choices=( $(sview_workspace_list "$@") )
if [[ ${#choices[@]} == 0 ]]; then
echo "no matching view found"
return 1
elif [[ ${#choices[@]} == 1 ]]; then
[[ -n $ZSH_NAME ]] && local i=1 || local i=0
export TOP=$WORKSPACES_ROOT/${choices[$i]}
else
select choice in ${choices[@]}; do
if [[ -n $choice ]]; then
break
elif [[ $REPLY == "q" ]]; then
return 0
else
return 1
fi
done
export TOP=$WORKSPACES_ROOT/$choice
fi
if ! [[ "$PWD" == "$TOP" || "$PWD" == "$TOP"/* ]]; then
cd "$TOP"
fi
}
fi
function settop {
if [[ -d $WORKSPACES_ROOT ]] && [[ $PWD == $WORKSPACES_ROOT/* ]]; then
export TOP=$(pwd | sed -r 's|^('$WORKSPACES_ROOT'/[^/]+).*$|\1|')
echo >&2 "Set TOP to $TOP"
fi
}
# set TOP if starting in a workspace
settop
if [[ -e /usr/bin/pacman ]]; then
function pkgclone {
if [[ -z "$1" ]]; then
echo >&2 "usage: pkgclone REPO [...]"
return 1
fi
local url="https://gitlab.archlinux.org/archlinux/packaging/packages/${1}.git"
shift
echo >&2 "-- Cloning $url"
git clone "$url" "$@"
}
fi
####### System/utility things #######
# gvim function for when running in cygwin
if [[ -d /cygdrive ]]; then
while read -r -d '' __vimdir; do
if [[ -e "$__vimdir/gvim.exe" ]]; then
__WINDOWS_GVIM="$__vimdir/gvim.exe"
break
fi
done < <(find "/cygdrive/c/Program Files/Vim" "/cygdrive/c/Program Files (x86)/Vim" \
-mindepth 1 -maxdepth 1 -type d -print0 2>/dev/null)
unset __vimdir
if [[ -e "$__WINDOWS_GVIM" ]]; then
export WINDOWS_HOME="C:\\Users\\${USER}"
function gvim() {
if [[ -z $1 ]]; then
SHELL='' HOME="$WINDOWS_HOME" cygstart "$__WINDOWS_GVIM"
return
fi
for i in "$@"; do
SHELL='' HOME="$WINDOWS_HOME" cygstart "$__WINDOWS_GVIM" \"$i\"
done
}
function gvimf() {
# In cygwin the selectf script can't launch gvim directly
local _file="$(selectf -p "$@")" || return 1
gvim "$_file"
}
fi
# wrap fd to try to do automatic path translation, since rust code
# doesn't understand cygwin paths.
if [[ -n "$ZSH_NAME" && -n "$(type -p fd 2>/dev/null)" ]] || \
[[ -n "$BASH" && -n "$(type -P fd 2>/dev/null)" ]]; then
fd() {
local _args=(--path-separator=/)
local _arg
for _arg in "$@"; do
if [[ "$_arg" == /* ]]; then
_args+=("$(cygpath -w "$_arg")")
else
_args+=("$_arg")
fi
done
command fd "${_args[@]}"
}
fi
fi
# bash specific
if [[ -n $BASH ]]; then
# immediate report background job changes
set -b
alias srcrc='source ~/.bashrc'
if [[ $USER == root ]]; then
# red prompt for root
PS1='\[\e[1;31m\][\u@\[\e[1;34m\]\h \[\e[1;36m\]\W\[\e[1;31m\]]\$\[\e[0m\] '
else
# green prompt for normal users
PS1='\[\e[1;32m\][\u@\[\e[1;34m\]\h \[\e[1;36m\]\W\[\e[1;32m\]]\$\[\e[0m\] '
fi
fi