I "redesigned" my shell prompt yesterday.
[...]
prepare_terminal()
{
stty rows $((LINES - 1))
printf "\n\033[1A"
old_lines=$LINES
old_cols=$COLUMNS
}
old_cmdno=0
old_lines=0
old_cols=0
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
while true; do
[ "${pwd#/*/}" == "$pwd" ] && break
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd"
local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
pwd=${pwd#/}
pwd=/${pwd#*/}
dots='...'
done
[ ${#status_nohl} -gt $COLUMNS ] && return
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'
while true; do
[ "${pwd#/*/}" == "$pwd" ] && break
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd"
local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
pwd=${pwd#/}
pwd=/${pwd#*/}
dots='...'
done
On 03.08.2023 20:31, Kaz Kylheku wrote:
I "redesigned" my shell prompt yesterday.
[...]
Hi Kaz, I wanted to try out your script and have two questions...
My bash doesn't seem to support the @P in cmdno=${getcmdno@P}
what does it do?
At certain places you use in printf contexts \033 or define a
variable esc to carry that value. In Kornshell I use \E or \e
for ANSI escapes, and my bash also supports that. Is there in
bash with PROMPT_COMMAND some technical necessity to use $esc ?
Hi Kaz, I wanted to try out your script and have two questions...
My bash doesn't seem to support the @P in cmdno=${getcmdno@P}
what does it do?
On 2023-08-03, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
while true; do
[ "${pwd#/*/}" == "$pwd" ] && break
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd"
local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
pwd=${pwd#/}
pwd=/${pwd#*/}
dots='...'
done
Erratum: the break test at the top of the loop body should be moved 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 causes an issue reported by a user on Mastodon, that
when you change to a directory like / or /home, the status
line disappears. We need the loop body executed unconditionally
at least to calculate a status line, and then to stop iterating
if the path cannot be reduced further after that.
There is one more check after the loop for a status line that cannot fit
the current $COLUMNS with, in which case it is turned off.
Yes, nice, but you lose a line on the terminal (but who cares?). If you
use Esc]2; you can update the terminal title. However, no highlights.
Should PROMPT_COMMAND= be PROMPT_COMMAND[0]= for modernity?
On 2023-08-03, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
I "redesigned" my shell prompt yesterday.
I have discovered an issue. When I run the "top" utility from procps-ng,
and then exit, stupidly puts the cursor into the scroll-protected area,
for whatever reason.
I "redesigned" my shell prompt yesterday.
if [ $curln > $LINES ]; then
I "redesigned" my shell prompt yesterday.
I "redesigned" my shell prompt yesterday.
On 2023-08-03, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
I "redesigned" my shell prompt yesterday.
LOL, I got the clock in the status line to update spontaneously.
$ trap update_status_line ALRM
$ while true; do kill -ALRM $$ ; sleep 15 ; done &
[1] 19410
Background process sends SIGALRM every 15 seconds; status
line updates.
Thanks for trying it; if you get it working 100%, you will like it,
I'm sure. The idea is to set the prompt to just PS=1'\$ ' which
is clean and refreshing, and feels like old Unix. All the junk
like showing the date, time, host name and current directory, goes into
a scroll-protected status line at the bottom.
I "redesigned" my shell prompt yesterday.
[...]
printf "${esc}7$esc[%s;1H$esc[K%s$esc[1;%sr${esc}8" $((LINES + 1)) "$status" $LINES
On 03.08.2023 20:31, Kaz Kylheku wrote:
I "redesigned" my shell prompt yesterday.
[...]
Yet another question...
printf "${esc}7$esc[%s;1H$esc[K%s$esc[1;%sr${esc}8" $((LINES + 1)) "$status" $LINES
What does $esc[1;%sr (with $LINES expanded for %s) do?
Say, \033[1;24r
(It seems I cannot find the ANSI control character 'r'.)
Janis
On 05.08.2023 15:21, Janis Papanagnou wrote:
On 03.08.2023 20:31, Kaz Kylheku wrote:
I "redesigned" my shell prompt yesterday.
[...]
Yet another question...
printf "${esc}7$esc[%s;1H$esc[K%s$esc[1;%sr${esc}8" $((LINES + 1)) "$status" $LINES
What does $esc[1;%sr (with $LINES expanded for %s) do?
Say, \033[1;24r
(It seems I cannot find the ANSI control character 'r'.)
I suppose I found the answer...
| The basics are: configure your device's shell to set scroll
| region once the command is accepted from user (equivalent of
| printf "\033[1,24r") -- and reset it once it completes --
| printf "\033[r".
[Stackexchange]
...presuming the differences (';' vs. ',') are not changing semantics.
On 05.08.2023 10:10, Kaz Kylheku wrote:
On 2023-08-03, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
I "redesigned" my shell prompt yesterday.
LOL, I got the clock in the status line to update spontaneously.
$ trap update_status_line ALRM
$ while true; do kill -ALRM $$ ; sleep 15 ; done &
[1] 19410
Background process sends SIGALRM every 15 seconds; status
line updates.
I think this extension introduced a bug; I started a bash shell,
sourced the script, exited the bash instance, and on a new start
and sourcing I get every 15s (even after leaving bash completely)
./bash-prompt-V0: line 73: kill: (4118) - No such process
On 04.08.2023 19:26, Kaz Kylheku wrote:
Thanks for trying it; if you get it working 100%, you will like it,
I'm sure. The idea is to set the prompt to just PS=1'\$ ' which
is clean and refreshing, and feels like old Unix. All the junk
like showing the date, time, host name and current directory, goes into
a scroll-protected status line at the bottom.
I can already say that I like the idea! Formerly we already had "all
the junk" in a separate line by adding a \n before the $ prompt. The
fixed (and protected) status line is certainly a step forward.
(or if) I write my ksh variant - bash is not my standard shell -, I
want to make further changes; the attributes I like to be displayed
are of course different, but I also prefer to change the cursor
position for the command input to be constantly at the bottom of the
screen.
On 2023-08-05, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 05.08.2023 15:21, Janis Papanagnou wrote:
[ANSI escape codes, Stackexchange quote]
...presuming the differences (';' vs. ',') are not changing semantics.
The documented syntax requires a semicolon. If it works with the
comma, it's some exstension; it doesn't on the terminal I'm using.
On 2023-08-03, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
I "redesigned" my shell prompt yesterday.
This now lives here:
https://www.kylheku.com/cgit/kabapro/tree/bash-prompt.sh
On 2023-08-05, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: [...]
(or if) I write my ksh variant - bash is not my standard shell -, I
want to make further changes; the attributes I like to be displayed
are of course different, but I also prefer to change the cursor
position for the command input to be constantly at the bottom of the
screen.
The logic is already there; it's executed when the cursor intrudes
into the status line.
We just have to execute that logic whenever the prior cursor position is
not exactly one line before the status line.
Maybe just executing the move unconditionally would also work,
in which case saving the previous position can be dropped.
On 05.08.2023 17:45, Kaz Kylheku wrote:
On 2023-08-05, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
[...]
(or if) I write my ksh variant - bash is not my standard shell -, I
want to make further changes; the attributes I like to be displayed
are of course different, but I also prefer to change the cursor
position for the command input to be constantly at the bottom of the
screen.
The logic is already there; it's executed when the cursor intrudes
into the status line.
We just have to execute that logic whenever the prior cursor position is
not exactly one line before the status line.
Maybe just executing the move unconditionally would also work,
in which case saving the previous position can be dropped.
I've written some experimental code using some Kornshell mechanisms,
so it differs from yours. (I've adopted/adapted your name though; I
like "basta" and named mine accordingly "kosta", kornshell status :-)
Anyway, resize will just have to be handled more smoothly, so there
is no glitch.
On 2023-08-05, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
On 2023-08-03, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
I "redesigned" my shell prompt yesterday.
This now lives here:
https://www.kylheku.com/cgit/kabapro/tree/bash-prompt.sh
Renamed. It's not a prompt but a status line.
So it's called Basta! (BAsh STAtus).
https://www.kylheku.com/cgit/basta/tree/
[...a Kornshell variant of "basta"...]
namespace kosta
{
typeset -i rows=${LINES} cols=${COLUMNS}
[...]
} # namespace kosta
On 08.08.2023 03:57, Janis Papanagnou wrote:
[...a Kornshell variant of "basta"...]
namespace kosta
{
typeset -i rows=${LINES} cols=${COLUMNS}
When installing the module to be sourced during .profile/.kshrc initialization I noticed that these two shell variables aren't
yet defined at that instance of time (which at least initially
typeset -i rows cols
stty size | read rows cols
On 2023-08-29, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote: [...]
typeset -i rows cols
stty size | read rows cols
I no longer trust the size, except on first invocation.
[...]
On 30.08.2023 01:42, Kaz Kylheku wrote:
On 2023-08-29, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
[...]
typeset -i rows cols
stty size | read rows cols
I no longer trust the size, except on first invocation.
This is only called initially once in my ksh-code, later I use the
discipline function to update the variable on LINES changes:
function LINES.set { .kosta.rows=${.sh.value} ; .kosta.update ;}
(That's Kornshell-specific, of course.)
[...]
I think I have it debugged close to as well as it can be; I put
almost 30 commits into this thing since I started the git repo.
[...]
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd"
local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
[...]
On 03.08.2023 20:31, Kaz Kylheku wrote:
[...]
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd"
local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
[...]
One more question; is there any neat way to get the "net" length of a
string that carries ANSI control characters (without duplicating the
code as above)?
Initially I wanted to add [ANSI-escape] colors to my "kosta" variant
(e.g. somthing like <red>errors<blue>shlvl<green>path<yellow>...),
but lacking a good idea I abstained from supporting that feature.
Janis
This is a → test!<<
This is a → test!<<
This is a → test! <<
On 03.08.2023 20:31, Kaz Kylheku wrote:
[...]
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd"
local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
[...]
One more question; is there any neat way to get the "net" length of a
string that carries ANSI control characters (without duplicating the
code as above)?
On 03.08.2023 20:31, Kaz Kylheku wrote:
[...]
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd"
local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
[...]
One more question; is there any neat way to get the "net" length of a
string that carries ANSI control characters (without duplicating the
code as above)?
On 2023-09-08, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
On 03.08.2023 20:31, Kaz Kylheku wrote:
[...]
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME $dots$pwd" >>> local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
[...]
One more question; is there any neat way to get the "net" length of a
string that carries ANSI control characters (without duplicating the
code as above)?
There could be ways, like using a function like $(ansi "$esc[m").
Let's recall that the code is Bash specific.
Bash has dynamically scoped local variables, allowing us to cleanly
control the behavior of functions with flags, without resorting
to blatant global variables.
local ansi_enable=y
such that $(ansi ...) will output its argument, rather than
consume it and output nothing. When the scope terminates
the prior value of ansi_enable is restored automatically.
[...]
For ksh, maybe defining a discipline function for a "no_ansi"
variable would be a sensible implementation, say,
no_ansi=${status} ; printf "${no_ansi}"
with function no_ansi.set removing the ANSI escapes using a
variable substitution (or a C-compiled shell built-in)?
Janis
On 08/09/2023 15:04, Janis Papanagnou wrote:
On 03.08.2023 20:31, Kaz Kylheku wrote:
[...]
local status="$esc[7m$(date +%m-%d/%H:%M)$esc[m $HOSTNAME
$dots$pwd"
local status_nohl="$(date +%m-%d/%H:%M) $HOSTNAME $dots$pwd"
[ ${#status_nohl} -le $COLUMNS ] && break
[...]
One more question; is there any neat way to get the "net" length of a
string that carries ANSI control characters (without duplicating the
code as above)?
Initially I wanted to add [ANSI-escape] colors to my "kosta" variant
(e.g. somthing like <red>errors<blue>shlvl<green>path<yellow>...),
but lacking a good idea I abstained from supporting that feature.
I was also having problems with printf and ansi/utf8.
Don't have a ksh solution, but a short bit of C ... maybe it's useful.
eg:
[snip samples ]
Anyway ...
[ snip C-code ]
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 297 |
Nodes: | 16 (2 / 14) |
Uptime: | 22:03:09 |
Calls: | 6,667 |
Calls today: | 1 |
Files: | 12,216 |
Messages: | 5,337,347 |