3 ms·
Show HN: Try my new Bash prompt
PS1='\$ ' # short and sweet prompt
old_cmdno=${old_cmdno-0}
old_lines=${old_lines-0}
old_cols=${old_cols-0}
prepare_terminal()
{
stty rows $((LINES - 1))
printf "\n\033[1A"
old_lines=$LINES
old_cols=$COLUMNS
}
update_status_line()
{
local exit=$?
local getcmdno='\#'
local cmdno=${getcmdno@P}
local esc=$(printf "\033")
local pwd=$PWD
local dots=
[ $LINES -eq $old_lines -a $COLUMNS -eq $old_cols ] || prepare_terminal
local status_esc="$esc[7m$esc[m"
while true; do
[ "${pwd#/*/}" == "$pwd" ] && break
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd"
local status_len=$((${#status} - ${#status_esc}))
[ $status_len -le $COLUMNS ] && break
pwd=${pwd#/}
pwd=/${pwd#*/}
dots='...'
done
status_len=$((${#status} - ${#status_esc}))
[ $status_len -gt $COLUMNS ] && status=
printf "${esc}7$esc[%s;1H$esc[K%s$esc[1;%sr${esc}8" $((LINES + 1)) "$status" $LINES
if [ $exit -ne 0 -a $cmdno -ne $old_cmdno ] ; then
printf "!%s!\n" $exit
fi
old_cmdno=$cmdno
}
PROMPT_COMMAND='update_status_line'
- anserin 3y agoCan you add a link to a screenshot?
- kazinator 3y agoIt was supposed to be a surprise! I just put one into this Mastodon post: https://mstdn.ca/@Kazinator/110832579322158690 https://mstdn.ca/@Kazinator/110832579322158690 Direct link to image: https://cdn.mastdn.ca/media_attachments/files/110/832/577/542/929/195/original/9460ab9f250f4c6c.png https://cdn.mastdn.ca/media_attachments/files/110/832/577/54...
- riddley 3y agobash: !\n: event not found bash: syntax error near unexpected token `fi' bash: syntax error near unexpected token `}' bash: update_status_line: command not found $ printf "!%s!\n" $exit^C bash: update_status_line: command not found $ echo $BASH_VERSION 5.2.15(1)-release
- kazinator 3y agoWhen ! occurs in an interactive command, Bash treats that specially (for history substitution or something; I don't use this so I'm foggy on the details). It's a POSIX violation which the Bash people think is okay because it doesn't affect scripts. So you can't just dump this into your shell interactively. It's meant to be put into a file that is sourced. Sorry about not mentioning it!
- kazinator 3y agoErratum: the break test at the top of the loop body needs to go to the bottom: while true; do local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd" local status_len=$((${#status} - ${#status_esc})) [ $status_len -le $COLUMNS ] && break pwd=${pwd#/} pwd=/${pwd#*/} dots='...' [ "${pwd#/*/}" == "$pwd" ] && break done This fixes an issue reported by a user on Mastodon: that when you change to a directory like / or /home, the status line disappears.
- kazinator 3y agoThis is now known as Basta! (BAsh STAtus line). https://www.kylheku.com/cgit/basta/tree/ https://www.kylheku.com/cgit/basta/tree/