PYENV prompt. master
authorIain Patterson <me@iain.cx>
Sat, 8 May 2021 09:05:41 +0000 (11:05 +0200)
committerIain Patterson <me@iain.cx>
Mon, 10 May 2021 11:35:45 +0000 (13:35 +0200)
.gitconfig
.profile.d/k8s.bashrc [new file with mode: 0644]
.profile.d/prompt.bashrc
.profile.d/ps1.bashrc
.prompt.d/title.prompt [new file with mode: 0644]
.ps1.d/k8s.ps1 [new file with mode: 0644]
.ps1.d/pyenv.ps1 [new file with mode: 0644]
opt/bin/kubectl [new file with mode: 0755]

index 4a23a84..38e51c0 100644 (file)
@@ -22,3 +22,4 @@
        pretty = log --graph --pretty=format:'%C(green)%ai%Creset %C(magenta)%h%Creset %s %C(cyan)%d%Creset'
        reabse = rebase
        submit = commit -vuno
+       whereami = rev-parse --abbrev-ref HEAD
diff --git a/.profile.d/k8s.bashrc b/.profile.d/k8s.bashrc
new file mode 100644 (file)
index 0000000..635e92e
--- /dev/null
@@ -0,0 +1,33 @@
+function __k8s() {
+  local text=
+  if [ -n "$KUBECTL_NAMESPACE" ]; then
+    if [ ! "$KUBECTL_NAMESPACE" = "default" ]; then
+      echo -n "$1$KUBECTL_NAMESPACE"
+      return 0
+    fi
+  fi
+}
+
+function context() {
+  if [ "$1" = "reset" ]; then
+    unset KUBECTL_CONTEXT
+  elif [ -n "$1" ]; then
+    export KUBECTL_CONTEXT=$1
+  elif [ -n "$KUBECTL_CONTEXT" ]; then
+    echo "$KUBECTL_CONTEXT"
+  else
+    kubectl config current-context
+  fi
+}
+
+function namespace() {
+  if [ "$1" = "reset" ]; then
+    unset KUBECTL_NAMESPACE
+  elif [ -n "$1" ]; then
+    export KUBECTL_NAMESPACE=$1
+  elif [ -n "$KUBECTL_NAMESPACE" ]; then
+    echo "$KUBECTL_NAMESPACE"
+  else
+    return 1
+  fi
+}
index da94e97..8114b29 100644 (file)
@@ -1,48 +1,13 @@
-# Set the title of an xterm to the hostname unless the remote IP is matched by 
-# a regular expression in ~/.ssh/forwarded.
-SSH_FORWARDED=$(get_remote_ip)
-if [ ! -z "$SSH_FORWARDED" ]; then
-  if [ "$SSH_FORWARDED" = "localhost" ]; then
-    SSH_FORWARDED=
-  elif [ -f ${PROFILE_HOME:-~}/.ssh/forwarded ]; then
-    for forwarded in $(< ${PROFILE_HOME:-~}/.ssh/forwarded); do
-      if echo "$SSH_FORWARDED" | grep "$forwarded" &>/dev/null; then
-        SSH_FORWARDED=
-      fi
-    done
-    unset forwarded
-  fi
-  [ ! -z "$SSH_FORWARDED" ] && SSH_FORWARDED=" (from $(get_remote_ip -n))"
-fi
-
-# Unscientific check for grid jobs.
-case ${SSH_CLIENT##* } in
-  ""|22|2222);;
-  *) SSH_FORWARDED="$SSH_FORWARDED [grid]"
-esac
-
-info="$HOSTNAME$SSH_FORWARDED"
-
-# Check for vim.
-vimchild=" [vim]"
-if ps -o comm= -p $PPID 2>/dev/null | grep '^vim*$' &>/dev/null; then
-  # Clear [vim] when exiting.
-  trap "echo -ne '\\033]0;$info\\033\\0134'" exit
-  info="$info$vimchild"
-fi
-if [ "${TERM##screen}" = "$TERM" -o "${TERM%%-bce}" = "$TERM" ]; then
-  PROMPT_COMMAND="echo -ne '\033]0;$info\033\0134\r\033[K'"
-else
-  PROMPT_COMMAND='echo -ne "\033k\033\0134"'
-fi
-unset blank info vimchild SSH_FORWARDED
+_prompt_command_escape=1
 
 # Actual dtterm barfs on escape sequences.
 if [ "$TERMINAL_EMULATOR" = "dtterm" ]; then
-  unset PROMPT_COMMAND
+  unset _prompt_command_escape
 fi
 
 # As does screen on an ancient Solaris host.
 if [ -n "$OLDSOLARIS" -a ! "${TERM##screen}" = "$TERM" ]; then
-  unset PROMPT_COMMAND
+  unset _prompt_command_escape
 fi
+
+PROMPT_COMMAND='shopt -q nullglob; ng=$?; shopt -s nullglob; for snippet in ${PROFILE_HOME:-~}/.prompt.d/*.prompt; do . $snippet; done; unset snippet; [ $ng = 0 ] || shopt -u nullglob; unset ng'
index e7d3658..7c4cf2c 100644 (file)
@@ -131,7 +131,7 @@ function __ps1_colour_for() {
   local colour=
   local ret=
   for colour in "${1}_colour${__ps1_colours}" "${1}_colour"; do
-    ret=$(eval echo -n "\$$colour")
+    eval "export ret=\$$colour"
     [ -n "$ret" ] && break
   done
   echo -n $ret
@@ -140,7 +140,7 @@ function __ps1_colour_for() {
 function __ps1_prefix() {
   local var=\$${2#\$}
   local prefix=${__ps1_all%$var*}
-  local all="$(eval echo $prefix)"
+  eval "all=\$$prefix"
   [ "${all/1/}" = "$all" ] || echo -n " "
   return $1
 }
@@ -238,7 +238,7 @@ function __ps1_short() {
   local pwd=${PWD/#$home/\~}
   local dirtrim=${PROMPT_DIRTRIM//[^0-9]/}
 
-  if [ "${dirtrim:0:1}" = "0" ]; then
+  if [ "${dirtrim:0:1}" = "0" -o "$PWD" = "$HOME" ]; then
     echo "$pwd"
     return $1
   fi
@@ -265,7 +265,7 @@ function __ps1_short() {
   local n=1
   local short=
   for component in $reversed; do
-    [ $n = 1 -a "$PWD" = "$pwd" ] || short="/$short"
+    [ $n = 1 ] || short="/$short"
     if [ $n -ge $dirtrim ]; then
       short="${component:0:1}$short"
     else
@@ -274,7 +274,7 @@ function __ps1_short() {
     n=$((n+1))
   done
 
-  [ "${short:0:1}" = "~" ] || short="/$short"
+  [ "${short:0:1}" = "~" -o -z "$basename" ] || short="/$short"
   echo "$short/$dirname"
   return $1
 }
@@ -299,7 +299,7 @@ function prompt() {
   fi
 
   what="$(echo $2 | env LANG= LC_ALL= LC_CTYPE= tr '[:upper:]' '[:lower:]')"
-  eval __ps1_$what=$action
+  eval "__ps1_$what=$action"
 }
 
 __ps1
diff --git a/.prompt.d/title.prompt b/.prompt.d/title.prompt
new file mode 100644 (file)
index 0000000..beadd34
--- /dev/null
@@ -0,0 +1,40 @@
+if [ -n "$_prompt_command_escape" ]; then
+  # Set the title of an xterm to the hostname unless the remote IP is matched by 
+  # a regular expression in ~/.ssh/forwarded.
+  SSH_FORWARDED=$(get_remote_ip)
+  if [ ! -z "$SSH_FORWARDED" ]; then
+    if [ "$SSH_FORWARDED" = "localhost" ]; then
+      SSH_FORWARDED=
+    elif [ -f ${PROFILE_HOME:-~}/.ssh/forwarded ]; then
+      for forwarded in $(< ${PROFILE_HOME:-~}/.ssh/forwarded); do
+        if echo "$SSH_FORWARDED" | grep "$forwarded" &>/dev/null; then
+          SSH_FORWARDED=
+        fi
+      done
+      unset forwarded
+    fi
+    [ ! -z "$SSH_FORWARDED" ] && SSH_FORWARDED=" (from $(get_remote_ip -n))"
+  fi
+
+  # Unscientific check for grid jobs.
+  case ${SSH_CLIENT##* } in
+    ""|22|2222);;
+    *) SSH_FORWARDED="$SSH_FORWARDED [grid]"
+  esac
+
+  info="$HOSTNAME$SSH_FORWARDED"
+
+  # Check for vim.
+  vimchild=" [vim]"
+  if ps -o comm= -p $PPID 2>/dev/null | grep '^vim*$' &>/dev/null; then
+    # Clear [vim] when exiting.
+    trap "echo -ne '\\033]0;$info\\033\\0134'" exit
+    info="$info$vimchild"
+  fi
+  if [ "${TERM##screen}" = "$TERM" -o "${TERM%%-bce}" = "$TERM" ]; then
+    echo -ne '\033]0;'"$info"'\033\0134\r\033[K'
+  else
+    echo -ne "\033k\033\0134"
+  fi
+  unset blank info vimchild SSH_FORWARDED
+fi
diff --git a/.ps1.d/k8s.ps1 b/.ps1.d/k8s.ps1
new file mode 100644 (file)
index 0000000..d572afc
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bash
+
+# This part of the prompt is taken from k8s.bashrc.
+# It is shown only if __ps1_k8s is 1.  By default it is 0.
+
+__ps1_k8s=${__ps1_k8s:-0}
+__ps1_k8s_colour256="0;38;5;127"
+__ps1_k8s_colour88="0;38;5;49"
+__ps1_k8s_colour="0;35"
+
+function __ps1_k8s() {
+  [ "$__ps1_k8s" = "1" ] || return $1
+  __k8s "$(__ps1_prefix $1 __ps1_k8s)"
+  return $1
+}
diff --git a/.ps1.d/pyenv.ps1 b/.ps1.d/pyenv.ps1
new file mode 100644 (file)
index 0000000..e551f37
--- /dev/null
@@ -0,0 +1,14 @@
+#!bash
+
+# This part of the prompt is shown only if __ps1_pyenv is 1.  The default is 0.
+
+__ps1_pyenv=${__ps1_pyenv:-0}
+__ps1_pyenv_colour256="0;38;5;227"
+__ps1_pyenv_colour88="0;38;5;77"
+__ps1_pyenv_colour="0;33"
+
+function __ps1_pyenv() {
+  [ "$__ps1_pyenv" = "1" ] || return $1
+  [ -n "$PYENV_VERSION" ] && echo "$(__ps1_prefix $1 __ps1_pyenv)$PYENV_VERSION"
+  return $1
+}
diff --git a/opt/bin/kubectl b/opt/bin/kubectl
new file mode 100755 (executable)
index 0000000..3faadd2
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+cmd=/usr/bin/kubectl
+args=""
+
+[ -n "$KUBECTL_CONTEXT" ] && args="$args --context $KUBECTL_CONTEXT"
+[ -n "$KUBECTL_NAMESPACE" ] && args="$args --namespace $KUBECTL_NAMESPACE"
+
+exec $cmd $args ${1+"$@"}