From 03c2e469dd06ce836d6d7e46a491c001ea2619a0 Mon Sep 17 00:00:00 2001 From: xdavidwu Date: Tue, 18 Feb 2020 20:46:33 +0800 Subject: [PATCH] bash: ssh and screen compatible level prompt --- .bashrc | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/.bashrc b/.bashrc index 5c75fd3..fa998d5 100644 --- a/.bashrc +++ b/.bashrc @@ -85,3 +85,50 @@ export SDL_VIDEODRIVER=wayland export QT_WAYLAND_DISABLE_WINDOWDECORATION=1 export GTK_IM_MODULE=fcitx export QT_IM_MODULE=fcitx + +# shell level prompt +# PTERM: poisoned TERM, $TERM/$LVLSTR, ssh pass TERM by default +# if $PPID is a GUI term, treat it as a fresh start +# if $TERM contains /, it is $PTERM passed from ssh +case "$TERM" in + */*) + PTERM=$TERM + TERM=${TERM%%/*} + ;; +esac +# bash complains if $() faces null +PCMD=$(tr -d '\000' < /proc/$PPID/cmdline) +PTERM=${PTERM:-$TERM/} +case "$PCMD" in + SCREEN*) + PTERM="${PTERM}S" + ;; + sshd*) + PTERM="${PTERM}s" + ;; + *term*) + PTERM="$TERM/" + ;; + *) + [ -z "$INITLVL" ] && PTERM="${PTERM}n" + ;; +esac +LVLSTR=${PTERM##*/} +PS1A= +while [ -n "$LVLSTR" ];do + case "$LVLSTR" in + S*) + PS1A="$PS1A\[\033[01;33m\]>" + ;; + s*) + PS1A="$PS1A\[\033[01;31m\]>" + ;; + n*) + PS1A="$PS1A\[\033[01;32m\]>" + ;; + esac + LVLSTR=${LVLSTR#?} +done +PS1="$PS1A$PS1" +export PTERM +alias ssh="env TERM=$PTERM ssh" -- 2.45.2