From 2f4249cac9aa299fd3e2c36b7b6e17c19ff92854 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Fri, 11 May 2018 10:21:09 +0000 Subject: [PATCH 1/7] Kubernetes stuff. --- .profile.d/k8s.bashrc | 33 +++++++++++++++++++++++++++++++++ .ps1.d/k8s.ps1 | 15 +++++++++++++++ opt/bin/kubectl | 9 +++++++++ 3 files changed, 57 insertions(+) create mode 100644 .profile.d/k8s.bashrc create mode 100644 .ps1.d/k8s.ps1 create mode 100755 opt/bin/kubectl diff --git a/.profile.d/k8s.bashrc b/.profile.d/k8s.bashrc new file mode 100644 index 0000000..635e92e --- /dev/null +++ b/.profile.d/k8s.bashrc @@ -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 +} diff --git a/.ps1.d/k8s.ps1 b/.ps1.d/k8s.ps1 new file mode 100644 index 0000000..d572afc --- /dev/null +++ b/.ps1.d/k8s.ps1 @@ -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/opt/bin/kubectl b/opt/bin/kubectl new file mode 100755 index 0000000..3faadd2 --- /dev/null +++ b/opt/bin/kubectl @@ -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+"$@"} -- 2.7.4 From 8f2c4b17bd7ca83ecf043c5c5e2d05edc91a1cd6 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Mon, 22 Oct 2018 13:27:56 +0100 Subject: [PATCH 2/7] Allow running multiple PROMPT_COMMAND snippets. Files matching the .prompt.d/*.prompt glob will be sourced before printing the prompt. --- .profile.d/prompt.bashrc | 45 +++++---------------------------------------- .prompt.d/title.prompt | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 .prompt.d/title.prompt diff --git a/.profile.d/prompt.bashrc b/.profile.d/prompt.bashrc index da94e97..e67a8fd 100644 --- a/.profile.d/prompt.bashrc +++ b/.profile.d/prompt.bashrc @@ -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='for snippet in ${PROFILE_HOME:-~}/.prompt.d/*.prompt; do . $snippet; done; unset snippet' diff --git a/.prompt.d/title.prompt b/.prompt.d/title.prompt new file mode 100644 index 0000000..beadd34 --- /dev/null +++ b/.prompt.d/title.prompt @@ -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 -- 2.7.4 From a11b3078d41e36fbdbab3f1773896a1d5c07cb15 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Mon, 22 Oct 2018 13:31:43 +0100 Subject: [PATCH 3/7] Some ps1 state variables need wider scope. --- .profile.d/ps1.bashrc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index e7d3658..80ad1da 100644 --- a/.profile.d/ps1.bashrc +++ b/.profile.d/ps1.bashrc @@ -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 } @@ -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 -- 2.7.4 From 8f5487f1fed69fc48c5681c506cf9076a6be5042 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Tue, 20 Nov 2018 16:20:59 +0000 Subject: [PATCH 4/7] Silently handle the case of no *.prompt files existing. --- .profile.d/prompt.bashrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.profile.d/prompt.bashrc b/.profile.d/prompt.bashrc index e67a8fd..8114b29 100644 --- a/.profile.d/prompt.bashrc +++ b/.profile.d/prompt.bashrc @@ -10,4 +10,4 @@ if [ -n "$OLDSOLARIS" -a ! "${TERM##screen}" = "$TERM" ]; then unset _prompt_command_escape fi -PROMPT_COMMAND='for snippet in ${PROFILE_HOME:-~}/.prompt.d/*.prompt; do . $snippet; done; unset snippet' +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' -- 2.7.4 From fcd45e190b92e5f5f25df29df408b31038c94816 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Thu, 1 Aug 2019 16:20:47 +0100 Subject: [PATCH 5/7] Added git whereami alias. --- .gitconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitconfig b/.gitconfig index 4a23a84..38e51c0 100644 --- a/.gitconfig +++ b/.gitconfig @@ -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 -- 2.7.4 From b9a1695a61ef662d278566e596417922f3d79833 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Sat, 8 May 2021 10:45:44 +0200 Subject: [PATCH 6/7] Fix short path in prompt. --- .profile.d/ps1.bashrc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.profile.d/ps1.bashrc b/.profile.d/ps1.bashrc index 80ad1da..7c4cf2c 100644 --- a/.profile.d/ps1.bashrc +++ b/.profile.d/ps1.bashrc @@ -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 } -- 2.7.4 From 27a9f1535fb2589c7dc0a57a82b42dfbfa46b91a Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Sat, 8 May 2021 11:05:41 +0200 Subject: [PATCH 7/7] PYENV prompt. --- .ps1.d/pyenv.ps1 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .ps1.d/pyenv.ps1 diff --git a/.ps1.d/pyenv.ps1 b/.ps1.d/pyenv.ps1 new file mode 100644 index 0000000..e551f37 --- /dev/null +++ b/.ps1.d/pyenv.ps1 @@ -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 +} -- 2.7.4