How to add color to output

This may be a more complete example… What I didn’t understand was:

/terminal/cuu

The following code will “redraw” a spreadsheet-like grid of random strings (that look like hex). It use “/terminal/inkey timeout=1s” in a loop (instead of “while(true)”…“delay 1s”), this allows the “press any key to exit” part. But could be leveraged to make it a real “event loop” and redraw new data from other commands. Using an array to store the grid be a better approach. But wanted to show how the /terminal operators without someone having to know ROS array sytanx.

Here is GIF of it working:




do {

:local RED     do={/terminal style error}
:local BLUE    do={/terminal style ambiguous}
:local WHITE   do={/terminal style "syntax-old"}
:local blue    do={/terminal style escaped} 
:local cyan    do={/terminal style "varname-local"}  
:local red     do={/terminal style varname} 
:local plain   do={/terminal style "syntax-val" } 
:local yellow  do={/terminal style "syntax-meta"}  
:local strong  do={/terminal style "syntax-noterm"}
:local nostyle do={/terminal style "none"} 

:local rndhex do={ :return [:rndstr length=6 from=abcdef0123456789] }

{                           :put {"  Press "}; 
  $nostyle; /terminal/cuu;     :put        "\t    key to exit (or use Ctrl-C)"; 
  $RED; /terminal/cuu;         :put        "\tany"; 
}

:local keypress 65535;

while (keypress=65535) do={ 
    :local Nrows (9);
    
    for i from=1 to=$Nrows do={:put ""} 
    for i from=1 to=$Nrows do={/terminal cuu} 
    for i from=1 to=$Nrows do={$RED; :put "   A$i         "} 
    for i from=1 to=$Nrows do={/terminal cuu} 
    for i from=1 to=$Nrows do={$cyan; :put "\t $([$rndhex])"} 
    for i from=1 to=$Nrows do={/terminal cuu} 
    for i from=1 to=$Nrows do={$blue; :put "\t\t $([$rndhex])"} 
    for i from=1 to=$Nrows do={/terminal cuu} 
    for i from=1 to=$Nrows do={$yellow; :put "\t\t\t $([$rndhex])"} 
    for i from=1 to=$Nrows do={/terminal cuu} 
    for i from=1 to=$Nrows do={$WHITE; :put "\t\t\t\t   E$i   "} 
    for i from=1 to=$Nrows do={/terminal cuu}
    
    :set keypress [/terminal inkey timeout=1s]
}
}