Browse code

Add dotfiles

Robert Cranston authored on 13/02/2022 02:08:04
Showing 17 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1,10 @@
1
+  # https://wiki.debian.org/chroot
2
+__prompt_chroot="$(! [ -r '/etc/debian_chroot' ] || cat '/etc/debian_chroot')"
3
+
4
+__prompt_chroot()
5
+{
6
+  if [ "$__prompt_chroot" ]
7
+  then
8
+    printf "$1" "$__prompt_chroot"
9
+  fi
10
+}
0 11
new file mode 100644
... ...
@@ -0,0 +1,23 @@
1
+__prompt_color_enabled="$(command -v tput)"
2
+__prompt_color_colors="$(! [ "$__prompt_color_enabled" ] || tput colors)"
3
+
4
+__prompt_color()
5
+{
6
+
7
+  [ "$__prompt_color_enabled" ] || return 0
8
+
9
+  __prompt_color="${PROMPT_COLOR:-15}"
10
+  if [ "$__prompt_color_colors" -lt "$__prompt_color" ]
11
+  then
12
+    __prompt_color="$((__prompt_color - 8))"
13
+    __prompt_color_bold='bold'
14
+  fi
15
+
16
+  printf "$1" "$(
17
+    tput -S << EOF
18
+      $__prompt_color_bold
19
+      setaf $__prompt_color
20
+      $2
21
+EOF
22
+  )"
23
+}
0 24
new file mode 100644
... ...
@@ -0,0 +1,12 @@
1
+__prompt_end_uid="$(id -u)"
2
+
3
+__prompt_end()
4
+{
5
+  if [ "$__prompt_end_uid" -eq 0 ]
6
+  then
7
+    printf "$1" "$3"
8
+  else
9
+    printf "$1" "$2"
10
+  fi
11
+}
12
+
0 13
new file mode 100644
... ...
@@ -0,0 +1,25 @@
1
+__prompt_exit()
2
+{
3
+  if [ "$3" -eq 0 ]
4
+  then
5
+    return 0
6
+  elif [ "$3" -lt 128 ]
7
+  then
8
+    printf "$1" "$3"
9
+  else
10
+    __prompt_exit_signal="$(kill -l "$(($3 - 128))" 2> '/dev/null')"
11
+    if ! [ "$__prompt_exit_signal" ]
12
+    then
13
+      printf "$1" "$3"
14
+      return 0
15
+    fi
16
+    for __prompt_exit_signal_mask in $2
17
+    do
18
+      if [ "$__prompt_exit_signal" = "$__prompt_exit_signal_mask" ]
19
+      then
20
+        return 0
21
+      fi
22
+    done
23
+    printf "$1" "$__prompt_exit_signal"
24
+  fi
25
+}
0 26
new file mode 100644
... ...
@@ -0,0 +1,24 @@
1
+# NOTE: `git-sh-prompt` relies on non-POSIX features, but we use it anyway
2
+# since the risk of a user running a Bourne style shell without these
3
+# features as a login shell is low.
4
+
5
+# https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
6
+if [ -r '/usr/lib/git-core/git-sh-prompt' ]
7
+then
8
+  GIT_PS1_SHOWDIRTYSTATE='y'
9
+  GIT_PS1_SHOWSTASHSTATE='y'
10
+  GIT_PS1_SHOWUNTRACKEDFILES='y'
11
+  GIT_PS1_SHOWUPSTREAM='auto verbose'
12
+  # GIT_PS1_SHOWCOLORHINTS='y'
13
+  . '/usr/lib/git-core/git-sh-prompt'
14
+else
15
+  __git_ps1()
16
+  {
17
+    :
18
+  }
19
+fi
20
+
21
+__prompt_git()
22
+{
23
+  __git_ps1 "$@"
24
+}
0 25
new file mode 100644
... ...
@@ -0,0 +1,7 @@
1
+__prompt_intr_enabled="$(command -v stty)"
2
+
3
+__prompt_intr()
4
+{
5
+  [ "$__prompt_intr_enabled" ] || return 0
6
+  stty intr "$1"
7
+}
0 8
new file mode 100644
... ...
@@ -0,0 +1,7 @@
1
+__prompt_jobs()
2
+{
3
+  if [ "$2" -ne 0 ]
4
+  then
5
+    printf "$1" "$2"
6
+  fi
7
+}
0 8
new file mode 100644
... ...
@@ -0,0 +1,9 @@
1
+__prompt_pwd()
2
+{
3
+  if [ "$PWD" = "$HOME" ]
4
+  then
5
+    printf "$1" '~'
6
+  else
7
+    printf "$1" "${PWD##?*/}"
8
+  fi
9
+}
0 10
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+# https://github.com/ranger/ranger/blob/master/doc/ranger.1
2
+__prompt_ranger()
3
+{
4
+  if [ "${RANGER_LEVEL:-}" ]
5
+  then
6
+    printf "$1" "$RANGER_LEVEL"
7
+  fi
8
+}
0 9
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+__prompt_reset_style="$(! [ "$(command -v tput)" ] || tput sgr0)"
2
+__prompt_reset_cursor="$(
3
+  __prompt_reset_cursor=
4
+  # if echo "$TERM" | grep -q '^xterm\(-\|$\)'
5
+  # then
6
+    __prompt_reset_cursor='\033[1 q'
7
+  # fi
8
+  if echo "$TERM" | grep -q '^linux\(-\|$\)'
9
+  then
10
+    __prompt_reset_cursor='\033[?8c'
11
+  fi
12
+  printf "$__prompt_reset_cursor"
13
+)"
14
+
15
+__prompt_reset()
16
+{
17
+  printf "$1" "$__prompt_reset_style$__prompt_reset_cursor"
18
+}
0 19
new file mode 100644
... ...
@@ -0,0 +1,4 @@
1
+__prompt_run()
2
+{
3
+  printf "$1" "$(fc -ln -1 | sed 's/^\s*//')"
4
+}
0 5
new file mode 100644
... ...
@@ -0,0 +1,7 @@
1
+__prompt_shell="${0#-}"
2
+__prompt_shell="${__prompt_shell##*/}"
3
+
4
+__prompt_shell()
5
+{
6
+  printf "$1" "$__prompt_shell"
7
+}
0 8
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+__prompt_statusline_tput()
2
+(
3
+  [ "$(command -v tput)" ] || return 0
4
+  if echo "$TERM" | grep -q '^xterm\(-\|$\)'
5
+  then
6
+    export TERM='xterm+sl'
7
+  fi
8
+  tput "$@"
9
+)
10
+
11
+__prompt_statusline_tsl="$(__prompt_statusline_tput tsl)"
12
+__prompt_statusline_fsl="$(__prompt_statusline_tput fsl)"
13
+
14
+__prompt_statusline()
15
+{
16
+  [ "$__prompt_statusline_tsl" ] || return 0
17
+  printf "$1" "${__prompt_statusline_tsl}${2}${__prompt_statusline_fsl}"
18
+}
0 19
new file mode 100644
... ...
@@ -0,0 +1,41 @@
1
+__prompt_time_dir=''
2
+[ -d "$__prompt_time_dir" ] || __prompt_time_dir="${XDG_RUNTIME_DIR:-"/var/run/user/$(id -u)"}"
3
+[ -d "$__prompt_time_dir" ] || __prompt_time_dir="${XDG_CACHE_HOME:-"$HOME/.cache"}"
4
+
5
+__prompt_time_file="$__prompt_time_dir/prompt_time/$$"
6
+
7
+mkdir -p "$(dirname "$__prompt_time_file")"
8
+
9
+__prompt_time_start()
10
+{
11
+  date '+%s' >| "$__prompt_time_file"
12
+}
13
+
14
+__prompt_time_stop()
15
+{
16
+  [ -r "$__prompt_time_file" ] || return 0
17
+  __prompt_time="$(("$(date '+%s')" - "$(cat "$__prompt_time_file")"))"
18
+  rm "$__prompt_time_file"
19
+  [ "$__prompt_time" -gt 0 ] || return 0
20
+  __prompt_time_d="$((__prompt_time / (24*60*60)))"
21
+  __prompt_time_h="$((__prompt_time % (24*60*60) / (60*60)))"
22
+  __prompt_time_m="$((__prompt_time              % (60*60) / (60)))"
23
+  __prompt_time_s="$((__prompt_time                        % (60)))"
24
+  __prompt_time_d="$([ "$__prompt_time_d" -eq 0 ] || printf '%02dd' "$__prompt_time_d")"
25
+  __prompt_time_h="$([ "$__prompt_time_h" -eq 0 ] || printf '%02dh' "$__prompt_time_h")"
26
+  __prompt_time_m="$([ "$__prompt_time_m" -eq 0 ] || printf '%02dm' "$__prompt_time_m")"
27
+  __prompt_time_s="$([ "$__prompt_time_s" -eq 0 ] || printf '%02ds' "$__prompt_time_s")"
28
+  printf '%s' \
29
+    "$__prompt_time_d" \
30
+    "$__prompt_time_h" \
31
+    "$__prompt_time_m" \
32
+    "$__prompt_time_s"
33
+}
34
+
35
+__prompt_time()
36
+{
37
+  if [ "$2" ]
38
+  then
39
+    printf "$1" "$2"
40
+  fi
41
+}
0 42
new file mode 100644
... ...
@@ -0,0 +1,19 @@
1
+# https://man.openbsd.org/ssh#SSH_CONNECTION
2
+__prompt_user_host="$(
3
+  if [ "$(id -u)" != "1000" ]
4
+  then
5
+    printf '%s' "$(id -un)"
6
+  fi
7
+  if [ "$SSH_CONNECTION" != "" ]
8
+  then
9
+    printf '@%s' "$(hostname)"
10
+  fi
11
+)"
12
+
13
+__prompt_user_host()
14
+{
15
+  if [ "$__prompt_user_host" ]
16
+  then
17
+    printf "$1" "$__prompt_user_host"
18
+  fi
19
+}
0 20
new file mode 100644
... ...
@@ -0,0 +1,63 @@
1
+VIRTUAL_ENV_DISABLE_PROMPT='1'
2
+
3
+__prompt_venv_rel()
4
+{
5
+  __prompt_venv_rel_dir="$PWD"
6
+  __prompt_venv_rel_up=''
7
+  while [ "${1#$__prompt_venv_rel_dir/}" = "$1" ]
8
+  do
9
+    __prompt_venv_rel_dir="${__prompt_venv_rel_dir%/*}"
10
+    __prompt_venv_rel_up="../$__prompt_venv_rel_up"
11
+  done
12
+  printf '%s' "$__prompt_venv_rel_up${1#$__prompt_venv_rel_dir/}"
13
+}
14
+
15
+__prompt_venv()
16
+{
17
+  if [ "${VIRTUAL_ENV:-}" ]
18
+  then
19
+    printf "$1" "$(__prompt_venv_rel "$VIRTUAL_ENV")"
20
+  fi
21
+}
22
+
23
+# Ask to activate the first virtual environment found, searching from the
24
+# current directory up to `$HOME` or `/`, whichever comes first.
25
+# https://github.com/python/cpython/blob/master/Lib/venv/scripts/common/activate
26
+__prompt_venv_activate()
27
+{
28
+  if [ "${VIRTUAL_ENV:-}" ]
29
+  then
30
+    deactivate
31
+  fi
32
+  __prompt_venv_dir="$PWD"
33
+  while
34
+    [ "$__prompt_venv_dir" != "$HOME" ] &&
35
+    [ "$__prompt_venv_dir" != '' ]
36
+  do
37
+    for __prompt_venv_activate in \
38
+      "$__prompt_venv_dir/"*'/bin/activate' \
39
+      "$__prompt_venv_dir/."*'/bin/activate'
40
+    do
41
+      [ -r "$__prompt_venv_activate" ] || continue
42
+      grep -q 'VIRTUAL_ENV' "$__prompt_venv_activate" || continue
43
+      __prompt_venv="${__prompt_venv_activate%/*/*}"
44
+      if [ "$__prompt_venv" != "$__prompt_venv_prev" ]
45
+      then
46
+        while true
47
+        do
48
+          printf 'Activate %s? [y/N] ' "$(__prompt_venv_rel "$__prompt_venv")"
49
+          read -r __prompt_venv_reply
50
+          case "$__prompt_venv_reply" in
51
+            'y'|'n'|'') break; ;;
52
+          esac
53
+        done
54
+      fi
55
+      case "$__prompt_venv_reply" in
56
+        'y') . "$__prompt_venv_activate"; ;;
57
+      esac
58
+      __prompt_venv_prev="$__prompt_venv"
59
+      return 0
60
+    done
61
+    __prompt_venv_dir="${__prompt_venv_dir%/*}"
62
+  done
63
+}
0 64
new file mode 100644
... ...
@@ -0,0 +1,50 @@
1
+# https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_03
2
+
3
+# NOTE: `PS0` and `PROMPT_COMMAND` are not POSIX but supported by common
4
+# shells.
5
+
6
+__prompt_ps1()
7
+{
8
+  __prompt_exit="$?"
9
+  __prompt_time="$(__prompt_time_stop)"
10
+  __prompt_jobs="$(jobs > '/dev/null'; jobs | wc -l)"
11
+  __prompt_color     '\x01%s\x02'
12
+  __prompt_user_host '%s:'
13
+  __prompt_pwd       '%s'
14
+  __prompt_chroot    ' (chroot:%s)'
15
+  __prompt_ranger    ' (ranger:%s)'
16
+  __prompt_venv      ' (venv:%s)'
17
+  __prompt_git       ' (git:%s)'
18
+  __prompt_jobs      ' {%s}' "$__prompt_jobs"
19
+  __prompt_time      ' [%s]' "$__prompt_time"
20
+  __prompt_exit      ' <%s>' 'TSTP' "$__prompt_exit"
21
+  __prompt_end       ' ' '$' '#'
22
+  __prompt_statusline '\x01%s\x02' "$(
23
+    __prompt_shell     '%s:' 'sh'
24
+    __prompt_user_host '%s:'
25
+    __prompt_pwd       '%s'
26
+  )"
27
+  __prompt_intr '^]'
28
+}
29
+
30
+__prompt_ps0()
31
+{
32
+  __prompt_intr '^C'
33
+  __prompt_statusline '%s' "$(
34
+    __prompt_shell     '%s:' 'sh'
35
+    __prompt_user_host '%s:'
36
+    __prompt_pwd       '%s'
37
+    __prompt_run       ':%s'
38
+  )"
39
+  __prompt_reset '%s'
40
+  __prompt_time_start
41
+}
42
+
43
+__prompt_command()
44
+{
45
+  __prompt_venv_activate
46
+}
47
+
48
+PS1='$(__prompt_ps1)'
49
+PS0='$(__prompt_ps0)'
50
+PROMPT_COMMAND='__prompt_command'