What is the terminal's graphic API?

Are you looking for something like ncurses?


What you're asking about isn't really a graphics API, it's just terminal control characters.

There's a lot of history behind it, but terminals back in the day were Teletype machines. Basically a typewriter with track-fed paper connected to the computer with a serial connection. Typing a character would send that binary value to the computer (as well as type it on the page). The computer would print characters back as the output of whatever you requested.

Special characters were used for controlling the terminal (which is where the control key comes from, it was for producing those characters). For example, ^H or ^? would be a backspace, ^M is a carriage return (moves the cursor to the beginning of the line) and ^J is a line feed (move the page up one line). Some control codes have an escape sequence in C (which is shared by almost all programming languages) to generate the control characters. The controls listed previously would be \b, \r, \n respectively.

The terminal emulator you use today is just what that sounds like, it's software that is pretending to be an old teletype machine. In a way, it's almost as if Unix still thinks you're using a typewriter to communicate with it.

In the case of curl, it writes an entire line then sends \r (carriage return) taking the cursor to the beginning of the line then writes another line. Since a line feed isn't sent it keeps writing over the same line on the screen.

Here's something you can try to demonstrate this:

echo -n  "First" ; sleep 5 ; echo -en "\rSecond\n"

You will see the word "First" printed (but no new line is sent). 5 seconds later it will be replaced with the word "Second" and a new line is sent. You can repeat this pattern indefinitely. Try adding "Third" yourself, you'll see something that you probably didn't expect ;-)

For more information about these and other control characters see the ascii(7) manual.