3

I do a lot of bash scripts for various needs. Recently I started to feel the urge of implement various indicators on top of them.

Would be cool during an automated unattended installation script know which operation is being carried.

Also.. could be nice have a status bar displaying a percentage of the actual progress.

There are in Linux (preferibly Debian) some libs and commands like my mockup ones for manipulate the terminal output ?

(following commands are fake mockups ones just for make reader understand)

txtoverlay -k head -c azure "MyString on top of all the commands"

or

txtovelay -k tail -c green -a right "[ Completition: 57 % ]"

or

txtovelay -k canvas -c azure -b darkblue -l 2 -t 5 -w 68 -h 50

To generate something like the following graphical mockups?

enter image description here

enter image description here

enter image description here

Or even some more complex overlays..

Basically the concept could be the same in HTML with some DIVs over the main webpage with position: fixed

<div id="MyDiv1" style="position:fixed; color: #00ffff; top: 0px; left: 0px; padding: 10px"></div>

<div id="MyDiv2" style="position:fixed; color: #00ff00; bottom: 0px; right: 0px; padding: 10px; text-align: right"></div>

and time by time during the script various commands like:

document.getElementById("MyDiv1").innerHTML = "Step 5: Installing NET-TOOLS package in progress..<br>-------------------------"

document.getElementById("MyDiv2").innerHTML = "[Completition: 57 % ]"
4
  • Not sure this is possible on the shell level. When your shell executes a command, it passes control to it, it can't jump back and forth to write the status bars. I think this would have to be implemented on the terminal emulator level. You could sort of do this with tmux, I suppose. Make three windows in tmux, shrink the top and bottom one, and run a command that generates the status/progress bars in them. Not really as good as what you're asking, and any window resizing will wreck it. Aug 19, 2017 at 23:19
  • @apricotboy Applications can jump back and forth, as long as they can assume that cursor save, move, and restore escape sequences work on the given terminal. I don't think any application actually uses this, though. screen or tmux does seem to be the way to go here
    – Fox
    Aug 20, 2017 at 0:49
  • True, it probably is possible, my comment's phrasing is off. I was trying to say how it's not really used in any applications. Aug 20, 2017 at 7:13
  • 1
    Currently I cannot make this an answer: You can run your script in screen (with hardstatus firstline) and use its status line. You can change the status line from the running script with screen -X hardstatus string "foo bar" Aug 20, 2017 at 12:17

2 Answers 2

6

Yes, you can do those things. Focusing just on the question of how does one place colored text in specific positions, one direct though somewhat low-level route is to use the tput utility. tput has numerous commands that, with the help of the terminfo database, manipulate the terminal screen. For example tput cup 23 4 will move the cursor to row 23, column 4 of your terminal. A few other examples:

tput ed  # clear to end of screen

tput setaf 2  # set foreground color to bright green

tput cubl  # move cursor left one space

tput rev  # turn on reverse video mode

tput sc  # save the cursor position
tput rc  # restore the cursor position

You may also find use for the stty utility. For example if you want to determine the dimensions of the current screen you can do stty size.

I have previously built a rough 'GUI' for some utility of mine that split the screen into two sections. The top section was a header of fixed height. The bottom section contained (scrolling) command output. I did this using Bash scripting and tput + stty only. I figured out a lot of it just by trial-and-error but there are some nice resources online such as http://linuxcommand.org/lc3_adv_tput.php

See man tput and man 5 terminfo. For the latter you'll want to scroll down to the Predefined Capabilities section in particular.

There may be higher level abstractions for terminfo-based screen manipulation but if you have relatively simple requirements tput is a good option. (I believe tput is part of the ncurses package mentioned in another answer here.)

Edit: I should add, since it sounds like you want some of these features on all your screens, that you can accomplish this by writing a shell script that utilizes tput as described above and point environment variable PROMPT_COMMAND at that script so it gets invoked each time your prompt is refreshed. If you want more frequent refreshing then you'd have to get some process to run in the background while still being attached to your screen. That's more than I'll try to bite off in this answer.

1
  • For sure that thing requires lot of trial and error but thanks for the hint, it will be a good start for researching ;) Aug 20, 2017 at 10:50
2

As far as I understand you're looking for a library like ncurses or python's urwid.

Not the answer you're looking for? Browse other questions tagged or ask your own question.