Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a "real" interactive terminal program like vim, htop, ... in C/C++ without ncurses

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

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

  2. 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.

like image 792
leemes Avatar asked Dec 12 '11 15:12

leemes


People also ask

Does Htop use ncurses?

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.

Does VI use ncurses?

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.


1 Answers

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:

  • If you will use terminfo, it will give you the correct control sequence for each action your terminal supports.
  • If you won't use terminfo... well, you have to manually code every action in every terminal type you want to support.

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.

like image 144
rodrigo Avatar answered Oct 17 '22 11:10

rodrigo