How to add color to output

is there a way to add color to the scripts like bash? For example I can run this in bash and get red output:

echo "\033[0;31mI Am Red?"

But when I try:

:put "\033[0;31mI Am Red?"

it doesn’t change the color.

No.

http://forum.mikrotik.com/t/script-input-from-console-works/120030/5

:terminal style …

Found this a bit ago, but got distracted by “/terminal cuu” yesterday. Don’t know if it helps, the key to using the /terminal “colors” is seemingly putting them inside curly braces with the any “:put” operations. For example,

{ [/terminal style error]; :put "error text"; }

You can string these together inside the same block:

   { :put "\n"; [ /terminal style error ]; :put "error text"; [ /terminal style escaped ]; :put "escaped text"; :put "\n";  }

More tricky is the “cursor up” operation, [/terminal cuu]. This part isn’t so clear, but what I found was use tabs, NOT spaces, allows some limited way of “inlining” colors using [/terminal cuu]:

   { :put "\n"; [ /terminal style error ]; :put "hello "; [ /terminal style syntax-meta ]; [ /terminal cuu ]; :put "\tworld"; [ /terminal style syntax-old ]; [ /terminal cuu ]; :put "\t\t!!!"; :put "\n"; }

Basically when you use “/terminal cuu” (cursor up), it also starts writing at the start of the line. But the “counter-acts” the newline (\n) automatically added by a :put “text” operation. The big trick is using backslash-t “\t” in your :put strings avoids overriding previously like done with a “:put”. Tabs are 8 chars, so you can’t do exact positioning, but close. While a space at the start of a string that being :put will override the line above, the tab doesn’t.





I can imagine a more sophisticated script doing more. For example, if you start with a :put “\n\n…” (e.g. the “row count”), then track the desired text layout using array in memory (in 8 char “cells” to match \t) to redraw using the right number of “/terminal cuu” and right ":put “\t\t\t\t…”… I ain’t that crazy, but I can squint and see a mini UNIX cursors library theoretically possible.

I did not use tab but wrote the line again, again and again on each letter entered, till it was confirmed with an enter, NEXT line. I had even the option to correct earlier entries with the cursor keys.

Didn’t try it with input :wink:… I occasionally use the colorization for output only, like a quick monitor script. The inkey stuff is super cleaver, but I just want some colorized text in the CLI for some stuff.

I was searching if there was already functions to make using the “colors” easier – only found this posting. Thought I’d add some script code as example for colorized output for others, since it’s not clear how to use /terminal based on F1 help (and no docs).

Here is what the all the various “color names” (like “/terminal style ambiguous”, “syntax-meta”…) map on screen with black background:

Mainly sharing the color side of /terminal style since got it working well enough for me. Basically just used some “macro” functions for my limited purposes to streamline using them:

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

which can be used like this:

{ $red; :put "red"; $RED; :put "reversed red" }

Ah, did not know this exists…

But you can use escape sequences as well.

:put ("\1B[91mhello \1B[93mworld \1B[96m!!!")

Nice catch.
This only works in telnet/ssh. Not in Winbox termial.
/terminal style does work in all terminal.

That seems true.

One exception, and it makes sense:
the Mikrotik terminal styles are stripped when you use ssh to exec one command, like:
ssh user@mikrotik “import printcolors.rsc”
The colors work fine if you use SSH to get into the RouterOS CLI shell using /terminal/style. But that logic may explain why @eworm ANSI is strip when in using “:put”, it does some sanitization on the :put strings for SSH.

Basically The /terminal/style is like a “smart write” of the ANSI same codes without a newline from :put – ROS internally has access to UNIX termcap, so they know more about the remote terminal than what’s available from CLI script.

The key to understand is trying the colors in the CLI was the key for me:
If you don’t put the /terminal/style in a { code block } together with a :put…it won’t work. See if you use “/terminal style” commands as separate commands (e.g. hit enter after them)… Mikrotik will reset the colors as part on the INPUT prompt. e.g. Mikrotik output of “admin@MIkroTik >” will clear the style you’d just set. THUS, it seem likes these command “do nothing” from the CLI. But they do, just need a code block in CLI like

admin@MIkroTik > {/terminal style error; :put "red reversed test"; /terminal style none}

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]
}
}

Don’t know if this helps anyone, but just learned a new trick to mimic the UNIX “clear” command.

But there is an ANSI escape code for that, e.g. ANSI escape (“\1B”) and letter “c”, as a routeros “byte string”:

:put "\1Bc"

Prompt stay at bottom, and winbox/etc clears the terminal.

Uuuuuuuuuuu… nice catch!

For simulate that on winbox terminal…:

:put ("\1B[41mhello \1B[43mworld \1B[46m!!!\1B[0m")

For simulate screenshot result on post #4:

:put ("\1B[1;37m\1B[41mhello \1B[1;33m\1B[40mworld \1B[0;30m\1B[42m!!!\1B[0m")

Note: The “random reloading color” example script in post #10 above is broken from ~v7.4 to the current v7.8rc2. There is Mikrotik bug in /terminial/inkey where the timeout= isn’t respected anymore, so the /terminal/inkey will only stop if it gets input. Previously a timeout=5s would mean, well, it either gets a key in 5 secs., or return 0xFFFF as the key (meaning timeout). When that’s fixed, that colorized example of “reload” will work again.

@rextended - it was your “replacement char” in the unicode thread, that got me searching for an ANSI code for clear :wink:. But I guess “c” is pretty obvious code for it…

FWIW, I wrote a wrapper function called “CHALK” that builds various ANSI escape codes for colorizing output. See http://forum.mikrotik.com/t/chalk-function-for-colorizing-text-output-using-ansi-codes/168093/1

Excelent. Now we got coloring in the terminal. How long until someone ports the DOOM into ROS CLI?

Ehm…

A more reasonable start be adding ANSI colors to https://en.wikipedia.org/wiki/Colossal_Cave_Adventure … see CLIGAMES - container with UNIX CLI games & playable with /system/telnet - Containers - MikroTik community forum