From 31afd5e3580cf158984884404b4c38fd8f97e9f4 Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Tue, 2 Apr 2024 22:22:51 -0700 Subject: [PATCH] overengineer zsh and bash prompts --- home/bashrc | 42 ++++++++++++++++++++++++++++++++++-------- home/zshrc | 12 +++++++----- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/home/bashrc b/home/bashrc index ca0ced8..290a2b8 100644 --- a/home/bashrc +++ b/home/bashrc @@ -15,19 +15,45 @@ set +o histexpand # disable expansion of ! characters _temporary_scope_() { local t="${TERM%%-*}" title= + + local ESC="\\E" + local BEL="\\a" + local SI="\\017" # switch to G0 charset + local SO="\\016" # switch to G1 charset + + local CSI="$ESC[" + local OSC="$ESC]" + if [ "$t" = xterm ] || [ "$t" = screen ] || [ "$t" = tmux ]; then - title='\[\033]2;\w\a\]' + title="\\\\w" + [ "$t" = tmux ] || title="\\\\H: $title" + title="${OSC}2;$title$BEL" fi - local color_okay='\\[\e[42m\\]' - local color_fail='\\[\e[41m\\]' - local line1=' \\[\e[0m\\]' - local line2='\\[\e[1;37m\\]\\$\\[\e[0m\\] ' - local ret_okay="$title$color_okay$line1\n$color_okay$line2" - local ret_fail="$title$color_fail$line1\n$color_fail$line2" + local G0="(" # change G0 charset + local G1=")" # change G1 charset + local USASCII="B" # sane default + local SPECIAL="0" # DEC Special Character and Line Drawing Set + + local DECTCEM="25" + local DECSET="h" + local SGR="m" + + local sanity="$ESC$G0$USASCII$SI$CSI?$DECTCEM$DECSET" + local hide="\\\\[" + local show="\\\\]" + local prompt="\\\\$" + local reset="$hide$CSI$SGR$show" + local color1="$CSI;--$SGR" + local color2="$CSI--;97$SGR" + local line1="$hide$title$sanity$color1$show $reset" + local line2="$hide$color2$show$prompt$reset " + local ret="$line1\\n$line2" + local ret_okay="${ret//--/42}" # use green background + local ret_fail="${ret//--/41}" # use red background PROMPT_COMMAND="[ \$? = 0 ] && PS1=\$'$ret_okay' || PS1=\$'$ret_fail'" -}; _temporary_scope_; unset _temporary_scope_ +}; _temporary_scope_; unset -f _temporary_scope_ . ~/.shrc diff --git a/home/zshrc b/home/zshrc index 92a346e..e98e06a 100644 --- a/home/zshrc +++ b/home/zshrc @@ -234,9 +234,12 @@ function { # zsh adds a % symbol to newline-less output, so my bash prompt is overkill # NOTE: i've started hardcoding escapes instead of relying on zsh # because detecting terminal features is too troublesome. - local s=$'\x1B\x5B' # start escape code - local e=m # end escape code - local reset="${s}0${e}" + # https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_ + local chset=$'\033\050B\017' # set G0 to USASCII charset and "shift-in" to it + local s=$'\033\133' # CSI + local e=m # SGR + local reset=$s$e + local curse=$s$'?25h' # show cursor (DECTCEM) local good=42 # green local bad=41 # red @@ -247,8 +250,7 @@ function { (sabotage) good=45;; # magenta esac - # NOTE: i had ${s}10${e} here before, is it still necessary? - PROMPT="%{$reset${s}%(?.${good}.${bad})${e}${s}97${e}%}%#%{$reset%} " + PROMPT="%{$chset$curse$reset$s%(?.$good.$bad);97$e%}%#%{$reset%} " } reload() { ### @-