No, I don't want to use ncurses, because I want to learn how the terminal works and have fun programming it on my own. :) It doesn't have to be portable, it has to work on linux xterm-based terminal emulators only.
What I want to do is programming an interactive terminal application like htop and vim are. What I mean is not the output of characters which look like boxes or setting colors, this is trivial; also to make the content fit to the window size. What I need is
how to get mouse interactions like clicking on a character and scrolling the mouse wheel (when the mouse is at a specific character) to implement scrolling [EDIT: in a terminal emulator of course], and
how to completely save and restore the output of the parent process and seperate my printing from its output, so after leaving my application nothing but the command I entered in the shell should be there, like when running htop and quitting it again: nothing is visible from this application anymore.
I really don't want to use ncurses. But of course, if you know which part of ncurses is responsible for these tasks, you're welcome to tell me where in the source code I can find it, so I will study it.
htop is written in the C programming language using the ncurses library. Its name is derived from the original author's first name, as a nod to pinfo, an info-replacement program that does the same.
Many full screen terminal programs including the popular vi editor are written by using ncurses. In the spirit of text-only C, I chose ncurses as my Theremin program's interface.
In order to manipulate the terminal you have to use control sequences. Unfortunately, those codes depend on the particular terminal you are using. That's why terminfo
(previously termcap
) exists in the first place.
You don't say if you want to use terminfo or not. So:
As you want this for learning purposes, I'll elaborate in the second.
You can discover the terminal type you are using from the environment variable $TERM
. In linux the most usual are xterm
for terminal emulators (XTerm, gnome-terminal, konsole), and linux
for virtual terminals (those when X is not running).
You can discover the control sequences easily with command tput
. But as tput
prints them on the console, they will apply immediately, so if you want to really see them, use:
$ TERM=xterm tput clear | hd 00000000 1b 5b 48 1b 5b 32 4a |.[H.[2J| $ TERM=linux tput clear | hd 00000000 1b 5b 48 1b 5b 4a |.[H.[J|
That is, to clear the screen in a xterm
you have to output ESC [ H ESC [ 2J
in an xterm but ESC [ H ESC [ J
in a linux terminal.
About the particular commands you ask about, you should read carefully man 5 terminfo
. There is a lot of information there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With