Community discussions

MikroTik App
 
Cicer22
just joined
Topic Author
Posts: 1
Joined: Wed Sep 02, 2020 4:26 pm

How to add color to output

Wed Sep 02, 2020 4:35 pm

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.
 
jarda
Forum Guru
Forum Guru
Posts: 7756
Joined: Mon Oct 22, 2012 4:46 pm

Re: How to add color to output

Wed Sep 02, 2020 10:03 pm

No.
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: How to add color to output

Thu Sep 03, 2020 1:13 am

 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to add color to output

Mon Dec 06, 2021 7:41 pm


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.


Image


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...<text>"... I ain't that crazy, but I can squint and see a mini UNIX cursors library theoretically possible.
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: How to add color to output

Mon Dec 06, 2021 8:13 pm

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.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to add color to output

Mon Dec 06, 2021 9:45 pm

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

Image

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" }
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: How to add color to output

Wed Dec 08, 2021 12:08 pm

Ah, did not know this exists...

But you can use escape sequences as well.
:put ("\1B[91mhello \1B[93mworld \1B[96m!!!")
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: How to add color to output

Wed Dec 08, 2021 12:13 pm

But you can use escape sequences as well.
Nice catch.
This only works in telnet/ssh. Not in Winbox termial.
/terminal style does work in all terminal.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to add color to output

Wed Dec 08, 2021 2:00 pm

/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}
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to add color to output

Wed Dec 08, 2021 2:49 pm

Ah, did not know this exists...

But you can use escape sequences as well.
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:

Image

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]
}
}
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to add color to output

Mon Feb 20, 2023 5:03 am

But you can use escape sequences as well.
:put ("\1B[91mhello \1B[93mworld \1B[96m!!!")
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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to add color to output

Mon Feb 20, 2023 10:16 am

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!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to add color to output

Mon Feb 20, 2023 10:56 am

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.

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")
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to add color to output

Mon Feb 20, 2023 4:09 pm

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 ;). But I guess "c" is pretty obvious code for it...
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to add color to output

Tue Jul 18, 2023 7:18 pm

FWIW, I wrote a wrapper function called "CHALK" that builds various ANSI escape codes for colorizing output. See viewtopic.php?t=197847

Image
 
User avatar
vecernik87
Forum Veteran
Forum Veteran
Posts: 882
Joined: Fri Nov 10, 2017 8:19 am

Re: How to add color to output

Fri Sep 01, 2023 10:02 am

Excelent. Now we got coloring in the terminal. How long until someone ports the DOOM into ROS CLI?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: How to add color to output

Sat Sep 02, 2023 6:13 pm

Ehm...
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How to add color to output

Sat Sep 02, 2023 6:23 pm

Excelent. Now we got coloring in the terminal. How long until someone ports the DOOM into ROS CLI?
A more reasonable start be adding ANSI colors to https://en.wikipedia.org/wiki/Colossal_Cave_Adventure ... see viewtopic.php?t=199250

Who is online

Users browsing this forum: Kuitz, m4rk3J and 18 guests