Hacker News new | past | comments | ask | show | jobs | submit login

Can anyone explain how to do a full-screen UI using only VT100 codes? If I had to write a small editor, I'd probably reach for something like Termbox for display.



First, switch it to alternative screen mode... printf("\033?1047h"); then clear and move and such with other similar prints. See the list here : http://invisible-island.net/xterm/ctlseqs/ctlseqs.html

It really is pretty simple. The big benefit of ncurses is if you want to target terminals other than xterm compatible ones, and they are so rare nowadays that you can really just ignore them....

Input is quite a pain though (however, ncurses sometimes get it wrong too), but the same thing applies, you just need to switch to raw mode with tcsetattr and then have some kind of select() loop or something to watch for escape sequences then translate them into interesting key presses.

Mouse events are the same btw: you write out a sequence to turn them on, then the terminal sends them as sequences to stdin.

I wrote a terminal library myself for the D programming language (and a terminal emulator too!), it is kinda ugly code but it isn't hard.


Thanks for the info, very useful info; I was most interested to learn that ncurses provides compatibility for terminals that are rare.


I wonder if these codes work in cmd.exe now that Windows 10 TH2 supports (some of) the features.


I haven't checked but I kinda doubt it... and I kinda hope not - the windows console API is so much nicer than the unix terminal system it would be a pity to make it an ugly hybrid.

Of course, you can run a terminal emulator inside the windows console and handle those sequences too! If I had to guess, I'd say that's what Microsoft would have implemented.


You could look at the code to see how it's done, but basically, you send a special sequence of characters that update the screen.

The VT100 (and a lot of terminal emulators these days) use what are called ANSI escape sequences. For instance, to move the cursor, the CUP (CUrsor Position) sequence is used, which is "<ESC> [ <row> ; <col> H" where <ESC> is the ESCAPE ASCII character, <row> is the row number (as ASCII, so for row 12, it's the string "12"), <col> is the column number ("40" for column 40) and the rest are the literal characters. So, in hex, it's 1B 5B 31 32 3B 34 30 48 (<ESC>[12;40H).

There are more sequences (move the cursor up, down, left, right, clear various portions of the screen, change colors, etc).

Conversely, the terminal will send ANSI escape sequences representing certain keys, such as "<ESC>[A" for the up arrow key, "<ESC>[D" for the left arrow key and such. It's then just a matter of recognizing these sequences and doing the appropriate thing ("I received <ESC>[A so the user is moving the cursor up. Update my internal structure to represent that, and send to the terminal <ESC>[A so it moves the cursor on the screen up").




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: