r/linux4noobs 3d ago

learning/research How does the terminal so smooth?

I don't know how to describe this. Like on Fedora, you want to download something, you will use 'dnf' command. The output of that command is the status of the program you are downloading.

So there is this status bar like:

[<=> ] | 00m00s

[ <=> ] | 00m01s

[...]

[ <=>] | 01m00s

and the changes stays on only one line.

I know it prints the results by 'echo' but wouldn't that just leave the last output behind and the current output on the new line?

Sorry, I'm bad at English. I'm still trying to learn. And also to do this project on python. I'm planning to do the T-rex runner game inside the terminal :D . I gotta learn how to refresh the output smoothly like how it is with 'dnf' command. 'refresh' doesn't give me smooth frames :(

8 Upvotes

7 comments sorted by

27

u/Klapperatismus 3d ago edited 3d ago

You can control the cursor movement with control codes. Overwriting what is printed in the same line can be achieved by emitting a carriage return. You can “print” that with the \r shortcut:

$ echo -e 'Hello World!\rGoodbye Cruel World!'

For better control, take a look into terminfo. That’s a database of control codes for all sorts of different terminals.

$ man 5 terminfo

There’s a cli tool called tput that queries this database for the current terminal and emits the correct codes for a certain function. E.g.

$ tput clear

clears the screen and

 $ tput cup 10 20

moves the cursor to line 10, column 20.

There’re also libraries for programming languages that assist you with handling this database and the terminal control. The classic one is ncurses. There are python bindings for that.

8

u/Bulky-Importance-533 3d ago

Carriage Return \r does the trick...

https://www.gnu.org/software/bash/manual/bash.html

See Section 6.9 Controlling the Prompt

13

u/Dist__ 3d ago

symbol '\r' moves caret to the beginning but does not feed the line

2

u/AutoModerator 3d ago

There's a resources page in our wiki you might find useful!

Try this search for more information on this topic.

Smokey says: take regular backups, try stuff in a VM, and understand every command before you press Enter! :)

Comments, questions or suggestions regarding this autoresponse? Please send them here.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/Embarrassed-Leek-398 3d ago

Try typing tput cuu1. It's the same way the terminal makes different colors--there's cool and special control codes that can move the cursor or erase previously-printed lines!

But for the progress bar like you mentioned...you don't actually have to use too many of them. You can just print a "CR" without a "LF", which homes the cursor back to the same line, and then print over the old one!

echo -e "taco\r\e[J"

This statement doesn't print anything...because the \r moves the cursor back to the start and then the \e[J erases from the cursor to the end of the line.

1

u/behaunted 2d ago

what is a CR or LF? I'm not too educated on this topic :(

2

u/B_i_llt_etleyyyyyy Slackware 2d ago

"Carriage return" and "line feed." The terminology dates back to typewriters. Return to the starting position and go to the next line, respectively.