Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redraw screen in terminal

Tags:

linux

terminal

How do some programs edit whats being displayed on the terminal (to pick a random example, the program 'sl')? I'm thinking of the Linux terminal here, it may happen in other OS's too, I don't know. I've always thought once some text was displayed, it stayed there. How do you change it without redrawing the entire screen?

like image 207
Jarek Avatar asked Sep 16 '08 22:09

Jarek


People also ask

How do I refresh my screen in vi?

The screen of Vim can be refreshed or redrawn by pressing Ctrl+l . If that does not work, use the command :redraw .

How to Reload Linux terminal?

To reboot Linux using the command line: To reboot the Linux system from a terminal session, sign in or “su”/”sudo” to the “root” account. Then type “ sudo reboot ” to reboot the box.


4 Answers

Depending on the terminal you send control seuqences. Common sequences are for example esc[;H to send the cursor to a specific position (e.g. on Ansi, Xterm, Linux, VT100). However, this will vary with the type or terminal the user has ... curses (in conjunction with the terminfo files) will wrap that information for you.

like image 116
Yamodax Avatar answered Oct 06 '22 02:10

Yamodax


try this shellscript

#!/bin/bash
i=1
while [ true ]
    do
            echo -e -n "\r $i"
            i=$((i+1))
    done

the -n options prevents the newline ... and the \r does the carriage return ... you write again and again into the same line - no scroling or what so ever

like image 37
mana Avatar answered Oct 06 '22 02:10

mana


Many applications make use of the curses library, or some language binding to it.

For rewriting on a single line, such as updating progress information, the special character "carriage return", often specified by the escape sequence "\r", can return the cursor to the start of the current line allowing subsequent output to overwrite what was previously written there.

like image 39
Mike Tunnicliffe Avatar answered Oct 06 '22 04:10

Mike Tunnicliffe


If you terminate a line sent to the terminal with a carriage return ('\r') instead of a linefeed ('\n'), it will move the cursor to the beginning of the current line, allowing the program to print more text over top of what it printed before. I use this occasionally for progress messages for long tasks.

If you ever need to do more terminal editing than that, use ncurses or a variant thereof.

like image 22
deemer Avatar answered Oct 06 '22 02:10

deemer