MUD/Telnet terminal emulation

jwatte's picture

Now and then, someone will say "hey, I want to write a MUD (or some other text-based program), and I want the text to be in color -- how do I do this?"

The answer actually requires a little bit of understanding of pre-Internet computer arcana, so here we go:

There is no one standard for telnet-type connections to display graphics or color.

Back in the days, when text-based terminals connected with RS-232 or similar serial interfaces was all there was, each terminal type had its own special 'escape sequences' that would do things like move the cursor, change the output color, etc. These typically start with the escape (27) character, followed by one or more other characters to specify the command, hence the name.

Because DEC (Digital Equipment Corporation) made such popular timeshare computers (the PDP line, and then the VAX line), their terminals were also very popular. Especially popular were the VT-52, VT-100 and VT-220 lines, which ended up being emulated by a lot of other terminals. Thus, if you drive the terminal as if it is one of those, then it's likely that the terminal emulation program used will know how to interpret those sequences.

Because there were so many different terminals, each of which had a different escape code to do the same operation (clear the screen, move the cursor, etc), libraries started developing to abstract this away. A library would come with a database of terminals, and the user would set his/her terminal type in the "TERM" environment variable. The library would then load the database entry for the particular terminal, and when the program said "clear the screen," the library would look it up in the database, and send the appropriate command. The two most popular such libraries are "termcap" (fairly simple to use) and "terminfo" (more modern, also a bit harder to use/configure).

After a while, a US standard was also adopted, known as the "ANSI" terminal standard. This is the most common for uses such as GUI terminal windows, DOS prompts, etc. The ANSI standard looks a bit like VT-52, too.

Because some terminals can do more than other terminals, applications started emulating certain features (like "insert line" or "clear to end of line" or whatever) by sending more commands to terminals that didn't support them. This, too, spawned new libaries for "windowing" or "menuing" on terminals using termcap or terminfo underneath. Popular libraries include the old, AT&T-copyrighted "curses" library, and the newer, open source "ncurses" library.

Thus, I would recommend using the ANSI codes, and to tell your users to set their emulation to ANSI, if their terminal/window doesn't already do this.