Kinx Library-Escape Sequence

Kinx Library-Escape Sequence

Introduction

** "Looks like JavaScript, brain (contents) is Ruby, (stability is AC / DC)" ** Scripting language Kinx ). The library is the life of the language. So how to use the library.

This time it's an escape sequence. You may think it's a little messy, but since we have made it possible to use VT100 escape sequences on Windows as well, it is possible (partly) to share this part with Windows / Linux.

There is no talk of using curses in the future. However, since it is large-scale and highly functional, I wondered if it would be useful to use escape sequences easily.

What is an escape sequence?

The control terminal is dull, isn't it? Escape sequences allow you to color, page, and provide a bit of nice looking stuff right now.

How is it realized?

First, Lexar has made it possible to use \ e as an alternative to \ x1b. It is convenient to write short.

Linux terminals are supposed to be able to use VT100 escape sequences from the beginning. Windows used the following.

This completes support for Windows escape sequences. What a wonderful library! MIT license. But I fixed some.

Is it really underlined right after posting this article? I found out how to do it. .. .. So I just fixed it and committed. Underline, I supported you! (In the latest source)

The Windows command prompt is fairly restrictive and can only support 16 colors. Therefore, the above 38 and 48 are fairly crafted so that the colors are close to each other. However, I'm a little wondering if I had to work so hard. And I don't feel like it's working so well. whatever.

Let's take a look at some samples.

sample

Sample 1

First of all, a color sample. This is the range supported by ansicolor-w32.c. However, only the underline is ~~ unsupported ~~ Although it does not appear in the result below, it is implemented by myself and supported by the latest source. The command prompt is on the left. The yellow border on the right is the result on ubuntu running on WSL.

By the way, the original of this source is here. I'm rewriting this for Kinx.

System.print("  --   ");
for (b in 40..47) {
  System.print("\e[%{b}m    %{b}   \e[0m ");
}
System.print("\n");
for (c in [ 30, 31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97 ]) {
  s = (""+c);
  System.print("\e[%{c}m %{c}   \e[0m ");
  for (b in 40..47) {
    s = "%{c};%{b}";
    System.print("\e[%{s}m %{s}   \e[0m ");
  }
  System.print("\n");
  for (a in [ 1, 4 ]) {
    s = "%{c};%{a}";
    System.print("\e[%{s}m %{s} \e[0m ");
    for (b in 40..47) {
      s = "%{c};%{b};%{a}";
      System.print("\e[%{s}m %{s} \e[0m ");
    }
    System.print("\n");
  }
}

I think it can be used perfectly within this range. There is no underline ~~ but ~~ I added it in the latest source.

sample5.png

Here is the part that I additionally implemented.

Sample 2

Text color with 256 colors specified.

for (i = 0; i < 16; i++) {
    for (j = 0; j < 16; j++) {
        const v = i*16+j;
        System.print("\e[38;5;%dm%02X\e[0m " % v % v);
    }
    System.print("\n");
}

It looks like this. There are only 16 colors, so it looks like this. I haven't set the colors individually, but calculated from the RGB information and converted to 16 colors, so I feel that the color change is not smooth, but it's like this ...

sample1.png

Sample 3

Next, the background color with 256 colors specified.

for (i = 0; i < 16; i++) {
    for (j = 0; j < 16; j++) {
        const v = (i<<4) | j;
        System.print(" \e[48;5;%dm%02X" % v % v);
    }
    System.print("\e[0m\n");
}

Output result. It looks a little dark.

sample2.png

Sample 4

Character color specified by RGB.

for (i = 0; i < 16; i++) {
    for (j = 0; j < 32; j++) {
        System.print("\e[38;2;%d;%d;255mX" % (i<<4) % (j<<3));
    }
    System.print("\e[0m\n");
}

Suspicion increases from this area. But like that ...

sample3.png

Sample 5

Background color with RGB specification.

for (i = 0; i < 16; i++) {
    for (j = 0; j < 32; j++) {
        System.print("\e[48;2;%d;%d;255m " % (i<<4) % (j<<3));
    }
    System.print("\e[0m\n");
}

This makes the difference noticeable.

sample4.png

Sample 6

Going back to the basics, you can even put out a text-based progress bar. The original story is "Use of escape sequences" in here.

System.print("0%       50%       100%\n");
System.print("+---------+---------+\n");
for (i = 0; i <= 100; i++) {
    for (j = 0; j < i / 5 + 1; j++) {
        System.print("#");
    }
    System.print("\n");
    System.print("%3d%%\n" % i);
    System.sleep(100);
    System.print("\e[2A");
}
System.print("\n\nfinish!\n");

sample7.png

in conclusion

I don't think escape sequences are that important at this time, but I personally like them because they are useful for making small commands look good. I wonder if there are people who are so happy that ** can be used on Windows **.

See you next time.

Recommended Posts

Kinx Library-Escape Sequence