Nп/п : 42 из 100
 От   : Richard Harnden                     2:5075/128        08 сен 23 19:13:05
 К    : Janis Papanagnou                                      08 сен 23 21:14:02
 Тема : Re: Nice prompt [Bash specific]
----------------------------------------------------------------------------------
                                                                                 
@MSGID: 1@dont-email.me> c05ea27a
@REPLY: 1@dont-email.me> 4eb4ae59
@REPLYADDR Richard Harnden
<richard.nospam@gmail.com>
@REPLYTO 2:5075/128 Richard Harnden
@CHRS: CP866 2
@RFC: 1 0
@RFC-Message-ID: 1@dont-email.me>
@RFC-References: <20230803112254.131@kylheku.com>
1@dont-email.me>
@RFC-Reply-To: nospam.harnden@gmail.com
@TZUTC: 0100
@PID: Mozilla/5.0 (Windows NT 10.0; Win64; x64;
rv:102.0) Gecko/20100101 Thunderbird/102.15.0
@TID: FIDOGATE-5.12-ge4e8b94
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  errorsshlvlpath...),
> 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:

$ RED=$(/bin/echo -ne "")
$ GREEN=$(/bin/echo -ne "")
$ BOLD=$(/bin/echo -ne "")
$ RESET=$(/bin/echo -ne "")

$ 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
" ">>" "${TEST}" "<<"
 >>     This is a -> test!<<


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

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

Anyway ...

#include 
#include 
#include 
#include 

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  
", 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  
", argv[0]);
         return 0;
     }

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

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

     if ( abs(pad) > 200 )
     {
         fprintf(stderr, "%s: pad, %d,  is out of range. [-200..200]
", 
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;
}




 --- Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
 * Origin: A noiseless patient Spider (2:5075/128)
SEEN-BY: 5001/100 5005/49 5015/255 5019/40 5020/715
848 1042 4441 12000
SEEN-BY: 5030/49 1081 5075/128
@PATH: 5075/128 5020/1042 4441



   GoldED+ VK   │                                                 │   09:55:30    
                                                                                
В этой области больше нет сообщений.

Остаться здесь
Перейти к списку сообщений
Перейти к списку эх