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