What is the terminal's graphic API?

128

Solution 1

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.

Solution 2

Are you looking for something like ncurses?

Solution 3

That question is actually several questions, and "control characters" addresses only a small part of it, e.g., the progress-bar for curl. More generally, these are common features of terminals (and terminal emulators).

Most of these common features are standardized in ECMA-48: Control Functions for Coded Character Sets. However, other features are not. They are implementation-defined.

ECMA-48 refers to control functions. That includes control characters and control sequences (often referred to as escape sequences, ANSI sequences, etc).

Some control characters are used for simple operations, e.g.,

  • move the cursor to the previous column on the same row
  • move the cursor to the first column on the same row
  • move the cursor to the next tab stop on the same row
  • move the cursor to the next row (and scroll the screen if on the last row already)

The progress-bar for curl is built using these simple operations. But control characters can do only so much, no more. Control sequences do more, e.g.,

  • move the cursor to any row/column on the screen
  • move the cursor to any row in the same column on the screen
  • move the cursor to any column in the same row on the screen
  • set tab-stops at any column on the screen
  • move the cursor to the previous line
  • make the screen scroll up or down without moving the cursor

So much for parallels between simple/complex. Control sequences also are used to change the color of text and background, erase text from the screen, show text in reverse-video (or bold, underline, blink).

Programs that draw a reverse-video (or colored) progress-bar use control sequences.

Although control sequences can do more, they can do only specific things. Putting them together to make text-editors, installation screens (and programs that draw colored progress-bars) gets complicated. Some of that is made simpler by using libraries that know about these things. Initially, we had termcap (and a database of a few hundred types of terminal), extended to terminfo (and a database of around a thousand types of terminal).

Even with standardization, there are dozens of terminal descriptions that you could use. So we continue to use libraries for all but the most trivial of these applications. One is ncurses ("new-curses"), another is slang (technically "S-Lang").

Further reading:

Share:
128

Related videos on Youtube

John
Author by

John

Updated on September 18, 2022

Comments

  • John
    John over 1 year

    if i have a file, how should i implement a function so that it can both read single and multiple lines. for example:

    TimC
    Tim Cxe
    USA
    http://www.TimTimTim.com
    TimTim facebook!
    ENDBIO
    Charles
    Dwight
    END
    Mcdon
    Mcdonald 
    Africa
          # website in here is empty, but we still need to consider it
          # bio in here is empty, but we need to include this in the dict
          # bio can be multiple lines
    ENDBIO
    Moon
    King
    END
    etc
    

    I am just wondering if anyone could use some python beginner keywords (like dont use yield,break, continue).

    In my own version, I actually defined 4 functions. 3 of the 4 functions are helper functions.

    and i want a function to return:

    dict = {'TimC':{'name':Tim Cxd, 'location':'USA', 'Web':'http://www.TimTimTim.com', 'bio':'TimTim facebook!','follows': ['Charles','Dwight']}, 'Mcdon':{'name':Mcdonald , 'location':'Africa', 'Web':'', 'bio':'','follows': ['Moon','King']}}
    
  • nosklo
    nosklo over 13 years
    code does not consider that the bio could be more than 1 line up to ENDBIO
  • John La Rooy
    John La Rooy over 13 years
    @noskio, correct, the question is underspecified, but the OP indicates that the bio should be a string, not a list. I chose to refuse the temtation to guess. When the question is updated to show what should happen if there is more than one line of bio, I can update my answer
  • Steven Monday
    Steven Monday almost 12 years
    These days, you'll want the new curses, or ncurses.
  • Boris Ivanov
    Boris Ivanov almost 12 years
    Exactly. Thanks for corrections. I forgot they different.