Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

We have phasers, I vote we blast 'em! -- Bailey, "The Corbomite Maneuver", stardate 1514.2


devel / comp.unix.shell / Re: Nice prompt [Bash specific]

SubjectAuthor
* Nice prompt [Bash specific]Kaz Kylheku
+* Re: Nice prompt [Bash specific]Janis Papanagnou
|+* Re: Nice prompt [Bash specific]Kaz Kylheku
||`* Re: Nice prompt [Bash specific]Janis Papanagnou
|| `* Re: Nice prompt [Bash specific]Kaz Kylheku
||  `* Re: Nice prompt [Bash specific]Janis Papanagnou
||   +- Re: Nice prompt [Bash specific]Kaz Kylheku
||   `* Re: Nice prompt [Bash specific]Janis Papanagnou
||    `* Re: Nice prompt [Bash specific]Kaz Kylheku
||     `* Re: Nice prompt [Bash specific]Janis Papanagnou
||      `* Re: Nice prompt [Bash specific]Kaz Kylheku
||       `- Re: Nice prompt [Bash specific]Janis Papanagnou
|`- Re: Nice prompt [Bash specific]Keith Thompson
+* Re: Nice prompt [Bash specific]Kaz Kylheku
|`* Re: Nice prompt [Bash specific]Chris Elvidge
| `- Re: Nice prompt [Bash specific]Kaz Kylheku
+* Re: Nice prompt [Bash specific]Kaz Kylheku
|+- Re: Nice prompt [Bash specific]Kaz Kylheku
|`- Re: Nice prompt [Bash specific]Kaz Kylheku
+* Re: Nice prompt [Bash specific]Kaz Kylheku
|`* Re: Nice prompt [Bash specific]Kaz Kylheku
| `* Re: Nice prompt [Bash specific]Kaz Kylheku
|  `- Re: Nice prompt [Bash specific]Kaz Kylheku
+* Re: Nice prompt [Bash specific]Kaz Kylheku
|`* Re: Nice prompt [Bash specific]Janis Papanagnou
| `- Re: Nice prompt [Bash specific]Kaz Kylheku
+* Re: Nice prompt [Bash specific]Janis Papanagnou
|`* Re: Nice prompt [Bash specific]Janis Papanagnou
| `* Re: Nice prompt [Bash specific]Kaz Kylheku
|  `- Re: Nice prompt [Bash specific]Janis Papanagnou
`* Re: Nice prompt [Bash specific]Janis Papanagnou
 +* Re: Nice prompt [Bash specific]Richard Harnden
 |`- Re: Nice prompt [Bash specific]Janis Papanagnou
 +* Re: Nice prompt [Bash specific]Kaz Kylheku
 |`* Re: Nice prompt [Bash specific]Janis Papanagnou
 | `- Re: Nice prompt [Bash specific]Janis Papanagnou
 `- Re: Nice prompt [Bash specific]Kaz Kylheku

Pages:12
Re: Nice prompt [Bash specific]

<ucltoj$2ec1t$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6588&group=comp.unix.shell#6588

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Wed, 30 Aug 2023 01:07:53 +0200
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <ucltoj$2ec1t$1@dont-email.me>
References: <20230803112254.131@kylheku.com> <uaj711$1b2hb$1@dont-email.me>
<20230804101131.699@kylheku.com> <uall88$1p0c0$1@dont-email.me>
<20230805084100.353@kylheku.com> <uas7e5$370cu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 29 Aug 2023 23:08:03 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c69a7ca42a6efa389bf5dfc59d83120d";
logging-data="2568253"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZHz28Esk3hFipTc4hHB7c"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:KX+JhmrHF8aDn87OS9Wp7dkGWFw=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <uas7e5$370cu$1@dont-email.me>
 by: Janis Papanagnou - Tue, 29 Aug 2023 23:07 UTC

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
produced some weird output once before the first hit of <Enter>).
Therefore I changed that in the code to

typeset -i rows cols
stty size | read rows cols

> [...]
> } # namespace kosta
>

Janis

Re: Nice prompt [Bash specific]

<20230829162831.608@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6589&group=comp.unix.shell#6589

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Tue, 29 Aug 2023 23:42:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <20230829162831.608@kylheku.com>
References: <20230803112254.131@kylheku.com> <uaj711$1b2hb$1@dont-email.me>
<20230804101131.699@kylheku.com> <uall88$1p0c0$1@dont-email.me>
<20230805084100.353@kylheku.com> <uas7e5$370cu$1@dont-email.me>
<ucltoj$2ec1t$1@dont-email.me>
Injection-Date: Tue, 29 Aug 2023 23:42:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b657d1e36c82df8a461d523776bf923";
logging-data="2576216"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oqXAdU1569FzMLD+3c/E7yndAVtTgnOs="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:l+tm0xMIbZ0NPG7X2eaRemRXIP4=
 by: Kaz Kylheku - Tue, 29 Aug 2023 23:42 UTC

On 2023-08-29, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> 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

Yes. I discovered this too. A while ago, in fact.
Then I forgot about it, so that a bug recently crept in. I
I changed a certain global variable initialization to this:

basta_scroll_lines=${basta_scroll_lines-$LINES}

which left the variable with a blank value when initializing out
of .bashrc!

I pushed out a fix for this just this morning!

However, LINES is available by the Bash calls the prompt hook.
So the bulk of the initialization is done at that time.

Speaking of globals, I initializee all global variables conditionally:

basta_old_cmdno=${basta_old_cmdno-0}
basta_old_lines=${basta_old_lines-0}
basta_old_cols=${basta_old_cols-0}

basta_scroll_lines=${basta_scroll_lines-0}
basta_prev_reserved_rows=${basta_prev_reserved_rows-}

basta_status_alarm_pid=${basta_status_alarm_pid-}

This allows for hot reloading without clobbering the variables.

In Common Lisp there is a defvar macro operator which defines
a variable if it doesn't already exist. If a value is specified
it assigns that value. If the variable already exists, it does
not assign the value. E.g.

(defvar *obj-count* 0)

If you reload the code, it will not reset your *obj-count* to zero.

If you want that you use:

(defparameter *obj-count* 0)

whose second argument is mandatory and always assigned to the
variable.

> typeset -i rows cols
> stty size | read rows cols

I no longer trust the size, except on first invocation.

In the first-time, once-only initialization code that is done by the
prompt hook when it detects that startup, I trust that the $LINES
variable corresponds to the usable area of the terminal, not
necessary the whole thing. I then query the terminal to determine
its size: position the cursor to 999,999 and then inquire about
the current position, to get ther row number and that's the size.
From that, it is inferred whethere or not there is already a protected
status line; we put our status line above any protected status lines.

On subsequent terminal resize operations, the terminal is always
queried for the size, rather than believing that $LINES or the tty
driver has the correct size.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Nice prompt [Bash specific]

<ucm04t$2el5u$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6590&group=comp.unix.shell#6590

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Wed, 30 Aug 2023 01:48:44 +0200
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <ucm04t$2el5u$1@dont-email.me>
References: <20230803112254.131@kylheku.com> <uaj711$1b2hb$1@dont-email.me>
<20230804101131.699@kylheku.com> <uall88$1p0c0$1@dont-email.me>
<20230805084100.353@kylheku.com> <uas7e5$370cu$1@dont-email.me>
<ucltoj$2ec1t$1@dont-email.me> <20230829162831.608@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 29 Aug 2023 23:48:45 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c69a7ca42a6efa389bf5dfc59d83120d";
logging-data="2577598"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tIdgFJW6ovlo7OfRYe2G7"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:V6JXAih7fJS8EpbAG48Oc4wHZgQ=
In-Reply-To: <20230829162831.608@kylheku.com>
 by: Janis Papanagnou - Tue, 29 Aug 2023 23:48 UTC

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.)

Janis

> [...]

Re: Nice prompt [Bash specific]

<20230829165341.499@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6591&group=comp.unix.shell#6591

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Wed, 30 Aug 2023 00:02:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <20230829165341.499@kylheku.com>
References: <20230803112254.131@kylheku.com> <uaj711$1b2hb$1@dont-email.me>
<20230804101131.699@kylheku.com> <uall88$1p0c0$1@dont-email.me>
<20230805084100.353@kylheku.com> <uas7e5$370cu$1@dont-email.me>
<ucltoj$2ec1t$1@dont-email.me> <20230829162831.608@kylheku.com>
<ucm04t$2el5u$1@dont-email.me>
Injection-Date: Wed, 30 Aug 2023 00:02:08 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6b657d1e36c82df8a461d523776bf923";
logging-data="2578324"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cuWZ9+qnkJyb7AqyPW2oSweBibjjbRx4="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:NGS414898e21Z6S8R8ZF4cnha4Q=
 by: Kaz Kylheku - Wed, 30 Aug 2023 00:02 UTC

On 2023-08-29, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> 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.)

By the way, likewise, my code check $LINES and $COLUMNS to detect
a change:

[ $LINES -eq $basta_old_lines -a \
$COLUMNS -eq $basta_old_cols ] || basta.prepare_term

but beyond that basta.prepare_term function won't rely on $LINES; the
terminal is queried to determine its size, from which we subtract the
number of lines needing to be protected, and set LINES accordingly,
punching that into the TTY with stty.

I found that not trusting LINES eliminated the rare glitches I was still
seeing where the scrolling region got set wrong, clobbering the status.

A good change also was not doing the cursor position check when there is
pending input in the TTY. In Bash we can use the read function to detect
whether input is pending. If input from the user is pending, we can't
be starting a transaction to query the terminal; we would have to read
and discard the user's input to get to the termina's response.

Skipping the check is likely okay because most of the time the cursor
does not stray into the status line.

If the user has run some program that has wrecked the scroll region,
*and* is rapidly typing ahead, then we won't catch the bad cursor
position.

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.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Nice prompt [Bash specific]

<ucn6mq$2n3bh$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6592&group=comp.unix.shell#6592

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Wed, 30 Aug 2023 12:46:50 +0200
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <ucn6mq$2n3bh$1@dont-email.me>
References: <20230803112254.131@kylheku.com> <uaj711$1b2hb$1@dont-email.me>
<20230804101131.699@kylheku.com> <uall88$1p0c0$1@dont-email.me>
<20230805084100.353@kylheku.com> <uas7e5$370cu$1@dont-email.me>
<ucltoj$2ec1t$1@dont-email.me> <20230829162831.608@kylheku.com>
<ucm04t$2el5u$1@dont-email.me> <20230829165341.499@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 30 Aug 2023 10:46:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c7583cc8ae5b41d3c968d19a5437c201";
logging-data="2854257"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18aT9BkaB6sNVnWqIB+rmO+"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:0EsOrRD2kpvMCiPAx17ahQXVn6U=
In-Reply-To: <20230829165341.499@kylheku.com>
 by: Janis Papanagnou - Wed, 30 Aug 2023 10:46 UTC

On 30.08.2023 02:02, Kaz Kylheku wrote:
> [...]
> 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.

It's amazing how many things (including edge and corner cases)
have to be considered to make a piece of software bulletproof,
even for a function that appears so simple at first glance as
a shell status line. :-)

Janis

Re: Nice prompt [Bash specific]

<udf9kr$3fteu$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6613&group=comp.unix.shell#6613

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Fri, 8 Sep 2023 16:04:10 +0200
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <udf9kr$3fteu$1@dont-email.me>
References: <20230803112254.131@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 8 Sep 2023 14:04:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="64ecf13caed83f083d203d8261ebbac3";
logging-data="3667422"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+PGGSPkSn/0ADeEd5buWlz"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:wge14q2RpgWzBQASfC0n5eYSduI=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <20230803112254.131@kylheku.com>
 by: Janis Papanagnou - Fri, 8 Sep 2023 14:04 UTC

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

Re: Nice prompt [Bash specific]

<udfo7g$3j5q1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6615&group=comp.unix.shell#6615

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: richard....@gmail.com (Richard Harnden)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Fri, 8 Sep 2023 19:13:05 +0100
Organization: A noiseless patient Spider
Lines: 147
Message-ID: <udfo7g$3j5q1$1@dont-email.me>
References: <20230803112254.131@kylheku.com> <udf9kr$3fteu$1@dont-email.me>
Reply-To: nospam.harnden@gmail.com
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 8 Sep 2023 18:13:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6076ced4f3e3fc96f834891c270fafde";
logging-data="3774273"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/9B8wvyNH8/LuaXmF98eBTqOtVGV+JHsA="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Cancel-Lock: sha1:4KFWoitzHQMKOpCJLWtqfl255uw=
In-Reply-To: <udf9kr$3fteu$1@dont-email.me>
 by: Richard Harnden - Fri, 8 Sep 2023 18:13 UTC

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.
>
> Janis
>

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:

$ R_ARROW=$(/bin/echo -en "\xe2\x86\x92")
$ RED=$(/bin/echo -ne "\x1b[38;5;1m")
$ GREEN=$(/bin/echo -ne "\x1b[38;5;2m")
$ BOLD=$(/bin/echo -ne "\x1b[1m")
$ RESET=$(/bin/echo -ne "\x1b[0m")

$ TEST="${GREEN}This is a ${R_ARROW} ${BOLD}${RED}test!${RESET}"

$ echo ${#TEST}
43

It isn't 43 - there are only 17 printable characters, so printf gets
confused:

$ printf "%s%50s%s\n" ">>" "${TEST}" "<<"
>> This is a → test!<<

$ printf "%s%s%s\n" ">>" "$(str_pad "${TEST}" 50)" "<<"
>> This is a → test!<<

or left justifed:
$ printf "%s%s%s\n" ">>" "$(str_pad "${TEST}" -50)" "<<"
>>This is a → test! <<

Anyway ...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

static int str_length(const char *s)
{ int length = 0;

while ( *s )
{
/* ansi escapes don't use any glyphs */
if ( *s == 0x1b )
{
s++;

while ( *s )
{
if ( islower(*s) )
break;

s++;
}

s++;
continue;
}

if ( (*s & 0xc0) != 0x80 ) /* don't count uft8 continuation
bytes */
length++;

s++;
}

return length;
}

int main(int argc, char *argv[])
{ int pad;
char *endptr;
int len;
int p;

if ( argc != 3 )
{
fprintf(stderr, "Usage: %s <str> <pad>\n", argv[0]);
return EXIT_FAILURE;
}

if ( argc == 2 && ( strcmp(argv[1], "-?") == 0 || strcmp(argv[1],
"--help") == 0 || strcmp(argv[1], "--help") == 0 ) )
{
printf("Usage: %s <str> <pad>\n", argv[0]);
return 0;
}

pad = strtol(argv[2], &endptr, 10);

if ( *endptr != '\0' )
{
fprintf(stderr, "%s: \"%s\" is not a number!\n", argv[0], argv[2]);
return EXIT_FAILURE;
}

if ( abs(pad) > 200 )
{
fprintf(stderr, "%s: pad, %d, is out of range. [-200..200]\n",
argv[0], pad);
return EXIT_FAILURE;
}

len = str_length(argv[1]);

if ( pad == 0 || len >= abs(pad) )
pad = 0;
else
p = abs(pad) - len;

if ( pad > 0 )
printf("%*c", p, ' ');

printf("%s", argv[1]);

if ( pad < 0 )
printf("%*c", p, ' ');

return 0;
}

Re: Nice prompt [Bash specific]

<20230908194428.631@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6616&group=comp.unix.shell#6616

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Sat, 9 Sep 2023 02:53:25 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <20230908194428.631@kylheku.com>
References: <20230803112254.131@kylheku.com> <udf9kr$3fteu$1@dont-email.me>
Injection-Date: Sat, 9 Sep 2023 02:53:25 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="cbf43b42d7a4bc81e1a1f81e08c9fb9f";
logging-data="4113230"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX197lQ/QCV6pDTTfm0U5R3ibcT9pTWjSnnM="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:NH7dBTRu6NTIWMc4HRqMHkPj8wE=
 by: Kaz Kylheku - Sat, 9 Sep 2023 02:53 UTC

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.

Now the thing is that I have a code smell: the double invocation
of date! In status_nohl, I'm calling date a second time, only
to get the format for the purposee of counting length.

I would prefer some solution in which we mark up the ANSI stuff
somehow like:

@@$esc [m@@.

For the purposes of rendering the codes, we filter out the expanded
version of @@$esc[m@@ (and everything similar) into just $esc[m.

For the purposes of calculating size, we filter @@$esc[m@@ into
nothing.

Bash should be able to do this ${var/this/that} substitution;
I will look into it.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Nice prompt [Bash specific]

<20230908195903.275@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6617&group=comp.unix.shell#6617

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Sat, 9 Sep 2023 03:03:20 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <20230908195903.275@kylheku.com>
References: <20230803112254.131@kylheku.com> <udf9kr$3fteu$1@dont-email.me>
Injection-Date: Sat, 9 Sep 2023 03:03:20 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="cbf43b42d7a4bc81e1a1f81e08c9fb9f";
logging-data="4116818"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ki5cztXSzntkyz+t+gFb0uPpOQS6gy0k="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:GEVIkiEhtpUEiWI3AraSchb43Dk=
 by: Kaz Kylheku - Sat, 9 Sep 2023 03:03 UTC

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)?

In the current version of the code, I see I had abandoned the
above approach. I have only one string which is rendered.
There is still duplication, but only of the escapes:

local status_esc="$esc[7m$esc[m"

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

status_esc is a string consisting of all the escape material
which occurs in status, so has to be maintained in parallel.

While that sucks, it sucks less than repeating the $(date ...)
and $HOSTNAME and other details.

In the above loop, I should be using a cached sample of the
$(date ...) too.

Re: Nice prompt [Bash specific]

<udgoei$3tqc7$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6618&group=comp.unix.shell#6618

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Sat, 9 Sep 2023 05:22:57 +0200
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <udgoei$3tqc7$1@dont-email.me>
References: <20230803112254.131@kylheku.com> <udf9kr$3fteu$1@dont-email.me>
<udfo7g$3j5q1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 03:22:58 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="59e5ecef452fffccf067d527fb33f2f8";
logging-data="4123015"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/v7+6e2rhNFs3PmkMoeDfH"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:vPwgxHe0GT9G5ZEAq+73XtJSSw0=
In-Reply-To: <udfo7g$3j5q1$1@dont-email.me>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 9 Sep 2023 03:22 UTC

On 08.09.2023 20:13, Richard Harnden wrote:
> 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 ]

I'm looking for any ideas or insights, so thanks for your idea and
C-code.

I see that you seem to have used a heuristic, triggering with Esc
and terminating with any lower-case character. - This is (almost)
exactly what I tried in shell with patterns (operating on text
that uses an \E escape literal for Esc)...

plain=${var//\\E\[*([!a-z])[a-z]} ; len=${#plain}

I had used "Esc[" (for my cases) but "Esc]" might also be relevant
for a more general application of the pattern (an extension to both
brackets is straightforward, of course). I had included the brace
because I wasn't sure whether an Esc might occur in other contexts
as well alone. (I wasn't sure, though, whether such patterns are
just a hack or sufficient for all ANSI escapes and not conflicting
with other applications.)

The C-code reminds me that any clumsy solution could also be hidden
by creating a shell built-in from a C-function. (But that relocates
the task just to another place.)

Instead of a C program another option could also be a sed-script.
(Performance should not be an issue here.)

Initially I was hoping for a _standard_ tool (like col(1), a tool
to filter reverse line feeds from input), since shell patterns (or
C-code) appears to me to be a bit bulky. (But probably there isn't
any neat and bulletproof standard tool existing...?)

Janis

Re: Nice prompt [Bash specific]

<udgpcm$3ttp1$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6619&group=comp.unix.shell#6619

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Sat, 9 Sep 2023 05:39:01 +0200
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <udgpcm$3ttp1$1@dont-email.me>
References: <20230803112254.131@kylheku.com> <udf9kr$3fteu$1@dont-email.me>
<20230908194428.631@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 03:39:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="fc75b5a3e213ba810813c5c99c2bb467";
logging-data="4126497"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Aw+LMbp34VQuE/+VIx/iK"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:w38jEPEbuy/5LJIDs8q4JkYcq2M=
In-Reply-To: <20230908194428.631@kylheku.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 9 Sep 2023 03:39 UTC

On 09.09.2023 04:53, Kaz Kylheku wrote:
> 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").

Yes, any neat interface that prevents duplication would be nice.

>
> Let's recall that the code is Bash specific.

I took the code from your bash code for explanation only, but I
have the same issue with my ksh variant; it's a general task
independent from these status line projects.

>
> 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.

The interface question is crucial for a neat implementation.

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

> [...]

Re: Nice prompt [Bash specific]

<udgpsg$3tvht$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6620&group=comp.unix.shell#6620

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Nice prompt [Bash specific]
Date: Sat, 9 Sep 2023 05:47:27 +0200
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <udgpsg$3tvht$1@dont-email.me>
References: <20230803112254.131@kylheku.com> <udf9kr$3fteu$1@dont-email.me>
<20230908194428.631@kylheku.com> <udgpcm$3ttp1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 9 Sep 2023 03:47:28 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="6591c9d2ace165778d0432ce2fa69e1c";
logging-data="4128317"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ojK2t8tFJBNIRIecVp4Db"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:RW7Z7ZdggHpYYclQcZkBYNXHnWE=
In-Reply-To: <udgpcm$3ttp1$1@dont-email.me>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Sat, 9 Sep 2023 03:47 UTC

On 09.09.2023 05:39, Janis Papanagnou wrote:
>
> For ksh, maybe defining a discipline function for a "no_ansi"
> variable would be a sensible implementation, say,
>
> no_ansi=${status} ; printf "${no_ansi}"

no_ansi=${status} ; printf "${#no_ansi}"

Typo; I wanted to get the length of the string for the status line
calculation, but both calls are useful and the logic would anyway
be in the no_ansi.set discipline function.

Hmm.. - pondering also about a type definition with that property
(vs. a variable specific definition).

>
> with function no_ansi.set removing the ANSI escapes using a
> variable substitution (or a C-compiled shell built-in)?
>
> Janis
>

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor