• gnuforth ascii terminal colors

    From goblinrieur@gmail.com@21:1/5 to All on Wed May 3 11:48:31 2023
    hello,

    I'm using gnu-forth currently as interpreter,
    I use escape sequences for coloration of strings in terminal with a

    decimal
    27 constant ESC \ to manage escape sequences used as colorations
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \ ASCII TERMINAL

    so I can use it as

    31 colorize ." print something" 0 colorize

    for example.

    is this a good method for my xterm compatibles ascii-terminals ?
    is there a better way to do so ?

    regards.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lorem Ipsum@21:1/5 to gobli...@gmail.com on Wed May 3 13:57:58 2023
    On Wednesday, May 3, 2023 at 2:48:33 PM UTC-4, gobli...@gmail.com wrote:
    hello,

    I'm using gnu-forth currently as interpreter,
    I use escape sequences for coloration of strings in terminal with a

    decimal
    27 constant ESC \ to manage escape sequences used as colorations
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \ ASCII TERMINAL

    so I can use it as

    31 colorize ." print something" 0 colorize

    for example.

    is this a good method for my xterm compatibles ascii-terminals ?
    is there a better way to do so ?

    Maybe I'm missing something, or don't understand BASE. I thought it is the address where the number conversion base is stored. You save it on the return stack, then restore it after printing the number, but I don't see where you change it. So why save
    and restore if you are not going to change it?

    Also, at the very beginning, you change the value stored at BASE to 10 with the word decimal. Is that intentional? the word decimal is not going to have any impact if BASE is changed elsewhere in the code, before colorize is called.

    What am I missing?

    --

    Rick C.

    - Get 1,000 miles of free Supercharging
    - Tesla referral code - https://ts.la/richard11209

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From minforth@21:1/5 to gobli...@gmail.com on Wed May 3 14:35:25 2023
    gobli...@gmail.com schrieb am Mittwoch, 3. Mai 2023 um 20:48:33 UTC+2:
    hello,

    I'm using gnu-forth currently as interpreter,
    I use escape sequences for coloration of strings in terminal with a

    decimal
    27 constant ESC \ to manage escape sequences used as colorations
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \ ASCII TERMINAL

    so I can use it as

    31 colorize ." print something" 0 colorize

    for example.

    is this a good method for my xterm compatibles ascii-terminals ?
    is there a better way to do so ?

    This is a rather subjective question. If it is good enough for you, it is good.

    FWIW sometimes I use

    \ : BOUNDS over + swap ;
    \ : ESC[ 27 emit '[' emit ;

    VARIABLE _VT \ Virtual/ANSI terminal enabled flag
    -1 _VT !

    : __VTSET \ ( n..1 a u -- ) set VT through string (^=ESC[, ~=number)
    base @ >r decimal bounds ?DO i c@
    dup '^' = IF drop esc[ ELSE
    dup '~' = IF drop 0 .r ELSE
    emit THEN THEN
    LOOP r> base ! ;

    : PAGE \ ( -- ) 10.6.1.2005 clear terminal screen
    _vt @ IF s" ^2J^H" __vtset THEN ;

    : AT-XY \ ( col row -- ) 10.6.1.0742 set text cursor coordinates
    _vt @ IF 1+ swap 1+ swap s" ^~;~f" __vtset THEN ;

    : SAVE-XY \ ( -- ) MF save cursor coordinates to memory
    _vt @ IF esc[ 's' emit THEN ;

    : RESTORE-XY \ ( -- ) MF restore cursor coordinates from memory
    _vt @ IF esc[ 'u' emit THEN ;

    : __VTCHAR \ ( n -- ) MF set terminal character attribute
    _vt @ IF s" ^~m" __vtset ELSE drop THEN ;

    \ text attributes
    : VTNORMAL 0 __vtchar ; \ ( -- ) MF
    : VTBRIGHT 1 __vtchar ; \ ( -- ) MF
    : VTUNDERLINE 4 __vtchar ; \ ( -- ) MF
    : VTREVERSE 7 __vtchar ; \ ( -- ) MF
    : VTHIDDEN 8 __vtchar ; \ ( -- ) MF

    \ text colors
    : VTBLACK 30 __vtchar ; \ ( -- ) MF
    : VTRED 31 __vtchar ; \ ( -- ) MF
    : VTGREEN 32 __vtchar ; \ ( -- ) MF
    : VTYELLOW 33 __vtchar ; \ ( -- ) MF
    : VTBLUE 34 __vtchar ; \ ( -- ) MF
    : VTMAGENTA 35 __vtchar ; \ ( -- ) MF
    : VTCYAN 36 __vtchar ; \ ( -- ) MF
    : VTWHITE 37 __vtchar ; \ ( -- ) MF

    \ move cursor
    : VTUP _vt @ IF s" ^~A" __vtset THEN ; \ ( n -- ) MF
    : VTDOWN _vt @ IF s" ^~B" __vtset THEN ; \ ( n -- ) MF
    : VTRIGHT _vt @ IF s" ^~C" __vtset THEN ; \ ( n -- ) MF
    : VTLEFT _vt @ IF s" ^~D" __vtset THEN ; \ ( n -- ) MF

    \ test
    CR vtcyan vtbright ." !! BELLA ITALIA !!" vtreverse
    CR vtgreen 18 spaces
    CR vtwhite 18 spaces
    CR vtred 18 spaces
    CR vtwhite vtnormal

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Brian Fox@21:1/5 to minforth on Wed May 3 17:27:08 2023
    On Wednesday, May 3, 2023 at 5:35:27 PM UTC-4, minforth wrote:
    gobli...@gmail.com schrieb am Mittwoch, 3. Mai 2023 um 20:48:33 UTC+2:
    hello,

    I'm using gnu-forth currently as interpreter,
    I use escape sequences for coloration of strings in terminal with a

    decimal
    27 constant ESC \ to manage escape sequences used as colorations
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \ ASCII TERMINAL

    so I can use it as

    31 colorize ." print something" 0 colorize

    for example.

    is this a good method for my xterm compatibles ascii-terminals ?
    is there a better way to do so ?
    This is a rather subjective question. If it is good enough for you, it is good.

    FWIW sometimes I use

    \ : BOUNDS over + swap ;
    \ : ESC[ 27 emit '[' emit ;

    VARIABLE _VT \ Virtual/ANSI terminal enabled flag
    -1 _VT !

    : __VTSET \ ( n..1 a u -- ) set VT through string (^=ESC[, ~=number)
    base @ >r decimal bounds ?DO i c@
    dup '^' = IF drop esc[ ELSE
    dup '~' = IF drop 0 .r ELSE
    emit THEN THEN
    LOOP r> base ! ;

    : PAGE \ ( -- ) 10.6.1.2005 clear terminal screen
    _vt @ IF s" ^2J^H" __vtset THEN ;

    : AT-XY \ ( col row -- ) 10.6.1.0742 set text cursor coordinates
    _vt @ IF 1+ swap 1+ swap s" ^~;~f" __vtset THEN ;

    : SAVE-XY \ ( -- ) MF save cursor coordinates to memory
    _vt @ IF esc[ 's' emit THEN ;

    : RESTORE-XY \ ( -- ) MF restore cursor coordinates from memory
    _vt @ IF esc[ 'u' emit THEN ;

    : __VTCHAR \ ( n -- ) MF set terminal character attribute
    _vt @ IF s" ^~m" __vtset ELSE drop THEN ;

    \ text attributes
    : VTNORMAL 0 __vtchar ; \ ( -- ) MF
    : VTBRIGHT 1 __vtchar ; \ ( -- ) MF
    : VTUNDERLINE 4 __vtchar ; \ ( -- ) MF
    : VTREVERSE 7 __vtchar ; \ ( -- ) MF
    : VTHIDDEN 8 __vtchar ; \ ( -- ) MF

    \ text colors
    : VTBLACK 30 __vtchar ; \ ( -- ) MF
    : VTRED 31 __vtchar ; \ ( -- ) MF
    : VTGREEN 32 __vtchar ; \ ( -- ) MF
    : VTYELLOW 33 __vtchar ; \ ( -- ) MF
    : VTBLUE 34 __vtchar ; \ ( -- ) MF
    : VTMAGENTA 35 __vtchar ; \ ( -- ) MF
    : VTCYAN 36 __vtchar ; \ ( -- ) MF
    : VTWHITE 37 __vtchar ; \ ( -- ) MF

    \ move cursor
    : VTUP _vt @ IF s" ^~A" __vtset THEN ; \ ( n -- ) MF
    : VTDOWN _vt @ IF s" ^~B" __vtset THEN ; \ ( n -- ) MF
    : VTRIGHT _vt @ IF s" ^~C" __vtset THEN ; \ ( n -- ) MF
    : VTLEFT _vt @ IF s" ^~D" __vtset THEN ; \ ( n -- ) MF

    \ test
    CR vtcyan vtbright ." !! BELLA ITALIA !!" vtreverse
    CR vtgreen 18 spaces
    CR vtwhite 18 spaces
    CR vtred 18 spaces
    CR vtwhite vtnormal

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Brian Fox@21:1/5 to gobli...@gmail.com on Wed May 3 17:30:44 2023
    On Wednesday, May 3, 2023 at 2:48:33 PM UTC-4, gobli...@gmail.com wrote:
    hello,

    I'm using gnu-forth currently as interpreter,
    I use escape sequences for coloration of strings in terminal with a

    decimal
    27 constant ESC \ to manage escape sequences used as colorations
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \ ASCII TERMINAL

    so I can use it as

    31 colorize ." print something" 0 colorize

    for example.

    is this a good method for my xterm compatibles ascii-terminals ?
    is there a better way to do so ?

    regards.

    I play with a retro machine where memory is tight so I broke out text
    and colour.

    I created a kind of "markup" for the VT100 commands
    These might provide some ideas for you.

    https://github.com/bfox9900/CAMEL99-ITC/blob/master/LIB.ITC/VT100.FTH https://github.com/bfox9900/CAMEL99-ITC/blob/master/LIB.ITC/VT100COLR.FTH

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to Brian Fox on Thu May 4 13:01:40 2023
    On 4/05/2023 10:30 am, Brian Fox wrote:

    I created a kind of "markup" for the VT100 commands
    These might provide some ideas for you.

    https://github.com/bfox9900/CAMEL99-ITC/blob/master/LIB.ITC/VT100.FTH https://github.com/bfox9900/CAMEL99-ITC/blob/master/LIB.ITC/VT100COLR.FTH

    Aren't the XY coords for VT100/ANSI 1-based rather than 0-based?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to Lorem Ipsum on Thu May 4 08:17:47 2023
    Lorem Ipsum <gnuarm.deletethisbit@gmail.com> writes:
    On Wednesday, May 3, 2023 at 2:48:33=E2=80=AFPM UTC-4, gobli...@gmail.com w= >rote:
    decimal=20
    27 constant ESC \ to manage escape sequences used as colorations=20
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \ A= ...
    Maybe I'm missing something, or don't understand BASE. I thought it is the=
    address where the number conversion base is stored. You save it on the re=
    turn stack, then restore it after printing the number, but I don't see wher= >e you change it. So why save and restore if you are not going to change it= >?=20

    Yes, the BASE saving and restoring is unnecessary here.

    Also, at the very beginning, you change the value stored at BASE to 10 with=
    the word decimal. Is that intentional? the word decimal is not going to =
    have any impact if BASE is changed elsewhere in the code, before colorize i= >s called.=20

    What am I missing?=20

    DECIMAL ensures that the "27" is converted using decimal base.

    An alternative (Forth-2012) way to ensure this is to write

    #27 constant ESC

    instead.

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Anton Ertl@21:1/5 to gobli...@gmail.com on Thu May 4 08:22:35 2023
    "gobli...@gmail.com" <goblinrieur@gmail.com> writes:
    hello,

    I'm using gnu-forth currently as interpreter,
    I use escape sequences for coloration of strings in terminal with a

    decimal
    27 constant ESC \ to manage escape sequences used as colorations
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \ ASCII TERMINAL

    so I can use it as

    31 colorize ." print something" 0 colorize

    for example.

    is this a good method for my xterm compatibles ascii-terminals ?
    is there a better way to do so ?

    Looking at COLORIZE again, I expect that you want to put DECIMAL (I
    think) before the <#. You can make your program safer in the face of exceptions by wrapping the base-change and restoration with CATCH;
    Gforth provides BASE-EXECUTE <https://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Number-Conversion.html>
    that does all that for you.

    Gforth also provides #ESC, and in Forth-2012 you can write

    s\" \e[" type

    instead of

    ESC EMIT ." ["

    Development Gforth has various words that use escape sequences to get
    coloured output on terminals/consoles that understand these escape
    sequences. You can find some documentation (which could be improved)
    at <https://gforth.org/manual/Terminal-output.html> and source code at <http://git.savannah.gnu.org/cgit/gforth.git/tree/ansi.fs>

    - anton
    --
    M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
    comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
    New standard: https://forth-standard.org/
    EuroForth 2022: https://euro.theforth.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From none) (albert@21:1/5 to gobli...@gmail.com on Thu May 4 13:49:13 2023
    In article <7cf7bfb9-91d4-4778-b452-f2fe06e6a092n@googlegroups.com>, gobli...@gmail.com <goblinrieur@gmail.com> wrote:
    hello,

    I'm using gnu-forth currently as interpreter,
    I use escape sequences for coloration of strings in terminal with a

    decimal
    27 constant ESC \ to manage escape sequences used as colorations
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \

    You are well advised to insert DECIMAL here.
    : COLORIZE ESC EMIT ." [" base @ >R
    DECIMAL
    0 <# #S #> type R> base ! ." m" ; \

    Suppose you have define colors like so:
    35 CONSTANT pink

    Further suppose you are in HEX. Suddenly
    pink COLORIZE
    behaves strangely.

    Actually the color "number" is not in hex or decimal, it is
    just a string. (Ask any VT220 ).
    Using the FORMAT package of lina , it would go
    : pink "%e[35" .FORMAT ;
    irrespective of base.

    ASCII TERMINAL

    so I can use it as

    31 colorize ." print something" 0 colorize

    for example.

    is this a good method for my xterm compatibles ascii-terminals ?
    is there a better way to do so ?

    If dictionary space permits, I would burn the "numbers" into
    the words that change color.


    regards.

    Groetjes Albert
    --
    Don't praise the day before the evening. One swallow doesn't make spring.
    You must not say "hey" before you have crossed the bridge. Don't sell the
    hide of the bear until you shot it. Better one bird in the hand than ten in
    the air. First gain is a cat spinning. - the Wise from Antrim -

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From S Jack@21:1/5 to dxforth on Thu May 4 05:24:10 2023
    On Wednesday, May 3, 2023 at 10:03:30 PM UTC-5, dxforth wrote:
    On 4/05/2023 10:30 am, Brian Fox wrote:
    Aren't the XY coords for VT100/ANSI 1-based rather than 0-based?

    Terminal escape sequences are 1-based; TPUT (Linux) is
    zero-base. I went with the 1-based. No problem in Forth.
    Used much in Bash scripts; just don't mix with TPUT.
    --
    me

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Brian Fox@21:1/5 to dxforth on Thu May 4 07:02:26 2023
    On Wednesday, May 3, 2023 at 11:03:30 PM UTC-4, dxforth wrote:

    Aren't the XY coords for VT100/ANSI 1-based rather than 0-based?

    You have caught my amateur work again Ed. Thanks.
    (Can you tell nobody else uses my system?) :-)

    I failed to update the repository with current files.

    https://github.com/bfox9900/CAMEL99-ITC/blob/master/LIB.ITC/VT100.FTH https://github.com/bfox9900/CAMEL99-ITC/blob/master/LIB.ITC/VT100%2B.FTH https://github.com/bfox9900/CAMEL99-ITC/blob/master/LIB.ITC/VT100COLOR.FTH

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lorem Ipsum@21:1/5 to Anton Ertl on Thu May 4 06:33:08 2023
    On Thursday, May 4, 2023 at 4:21:32 AM UTC-4, Anton Ertl wrote:
    Lorem Ipsum <gnuarm.del...@gmail.com> writes:
    On Wednesday, May 3, 2023 at 2:48:33=E2=80=AFPM UTC-4, gobli...@gmail.com w=
    rote:
    decimal=20
    27 constant ESC \ to manage escape sequences used as colorations=20
    : COLORIZE ESC EMIT ." [" base @ >R 0 <# #S #> type R> base ! ." m" ; \ A=
    ...
    Maybe I'm missing something, or don't understand BASE. I thought it is the=
    address where the number conversion base is stored. You save it on the re=
    turn stack, then restore it after printing the number, but I don't see wher=
    e you change it. So why save and restore if you are not going to change it= >?=20

    Yes, the BASE saving and restoring is unnecessary here.

    I think "unnecessary" is an understatement.


    Also, at the very beginning, you change the value stored at BASE to 10 with=
    the word decimal. Is that intentional? the word decimal is not going to =
    have any impact if BASE is changed elsewhere in the code, before colorize i=
    s called.=20

    What am I missing?=20

    DECIMAL ensures that the "27" is converted using decimal base.

    An alternative (Forth-2012) way to ensure this is to write

    #27 constant ESC

    instead.

    I guess my concern was that the person writing the code thought the use of decimal would have impact on the words defined at run time. Correct me if I'm wrong, but the numeric conversion used, does depend on the value in BASE, no? But that value could
    be anything at run time. This is why BASE is saved and restored, no? Or am I missing something about numeric conversion?


    Rick C.

    + Get 1,000 miles of free Supercharging
    + Tesla referral code - https://ts.la/richard11209

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)