The Intellectual Wilderness There is nothing more useless than doing efficiently that which should not be done at all.

2017.09.21 12:06

Erlang: Silly way to see if your shell supports VT100 commands

Filed under: Computing — Tags: , , , , , — zxq9 @ 12:06

There are a few cases where it can be useful to use VT100 terminal commands in shell interaction scripts to draw frames, progressbars, menu lines, position the cursor, clear the screen, colorize text, etc.

I actually have a small library of utilities like this I might eventually release, but its a pretty niche need.

Anyway, within that niche need, here is a really silly way to see if the terminal you run your shell in supports VT100 commands. (If you’re on Linux using pretty much any prepackaged terminal then your terminal supports VT100 commands, but that is not always so true on Windows, depending on how you are accessing your shell.) Paste the following into your shell:

Z =
  fun() ->
    {ok, S} = gen_tcp:connect("towel.blinkenlights.nl", 23, []),
    ok = gen_tcp:send(S, "\r\n"),
    Q =
      fun R() ->
        receive
          {tcp, S, B} ->
            ok = io:format("~ts", [B]),
            R();
          {tcp_closed, S} ->
            done
          after 60000 ->
            ok = gen_tcp:close(S),
            timeout
        end
      end,
    Q()
  end.

And then do Z().

(I remember seeing this first years ago and had forgotten it was even a thing! Sysop excuses is still live at port 666 as well, btw…)

2 Comments »

  1. Hello Sir,

    First of all, very good article, could you possibly share that lib to use the VT100 terminal? that would be very useful for a small experiment I am in the making. Thank you very much!

    Regards,
    Nicolas Maman

    Comment by Nicolas Maman — 2021.07.28 20:10 @ 20:10

  2. Hi, Nicolas.

    There is no special library being used here, the terminal that the Erlang shell itself is running within is doing the VT100 emulation already. The data coming in for the experiment above is simply coming in over a raw telnet connection and the terminal we are using the Erlang shell in is interpreting the terminal codes automatically.
    (Try connecting to telnet towel.blinkenlights.nl in a terminal and see — it works on Windows as well, though you have to enable telnet as a feature through a somewhat arcane procedure involving knowing where the magic click boxes are because Microsoft hates its users).

    Almost all Linux/OSX/BSD/etc and Windows terminals provide at a very minimum VT100 emulation (meaning that if your print any VT control sequences to the terminal, the terminal will automatically know how to interpret them), and in fact almost all of the modern Linux terminal emulators like Konsole, Gnome Terminal, XTerm, etc. provide up to VT500 emulation (though not many claim to support this officially unless they provide an explicit settings option that allows you to declare what mode you want to be in, so be careful if you are writing a terminal program that depends on features beyond VT100).

    Luckily for us there is a large amount of VT series terminal information around. For the most part what you need is the cursor movement and placement commands, backspace working properly, a known tab width (and maybe vtab height), and of course the dimensions of the terminal you are talking to — though discovering what sort of terminal you’re working with over telnet can be a bit of an adventure. VT color codes follow the ASCII/ANSI terminal color codes.
    IBM has a section on it: https://www.ibm.com/docs/en/personal-communications/5.9?topic=reference-vt-emulation
    And this program is pretty cool: https://invisible-island.net/vttest/

    Comment by zxq9 — 2021.07.29 15:00 @ 15:00

RSS feed for comments on this post.

Leave a comment

Powered by WordPress