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

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 thoughts on “Erlang: Silly way to see if your shell supports VT100 commands

  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

    1. 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/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.