Gnu watch: justify in the lower left corner of the terminal

I want to use the watch command in a mysql query every N seconds, but I would like to get the results in the lower left corner of the terminal, and not in the upper left corner:

watch -n 120 "mysql_query" | column -t" 

Shows my results as follows:

 -------------------------- |xxxxxxxxxxx | |xxxxxxxxxxx | |xxxxxxxxxxx | | | | | -------------------------- 

While I would like them to have this:

 -------------------------- | | | | |xxxxxxxxxxx | |xxxxxxxxxxx | |xxxxxxxxxxx | -------------------------- 

Sentence?

+4
source share
2 answers

I do not see a direct way to do this, but I managed to get it to work using the following approach. I have not fully tested this, so I cannot guarantee that this will work in all situations.

Using this script:

 #!/bin/bash TERM_HEIGHT=`tput lines` # determine terminal height WATCH_BANNER_HEIGHT=2 # account for the lines taken up by the header of "watch" let VIS_LINES="TERM_HEIGHT - WATCH_BANNER_HEIGHT" # height of visible area (yes " " | head -n $VIS_LINES; cat | head -n $VIS_LINES) | tail -n $VIS_LINES 

Post the output process of your command, since it is called watch , for example. (assuming the script was saved as align_bottom , made an executable, and saved somewhere in your $PATH ):

 watch -n 120 "mysql_query | column -t | align_bottom" 

What the script does:

  • Determine the height (number of lines) of the terminal
  • Calculate the visible output area of watch
  • Print blank lines for input output (press exit)
  • Read the output from stdin and crop it so that we only display the top output if it goes off the screen. If you want to see the bottom of the output, simply remove the head command after cat .
  • tail output of steps (3) and (4), so that excess filling is removed, and the final output is tightly included in watch

I have to admit that this sounds a bit hacky, but hopefully it brings you closer to what you are trying to achieve.


Update:

It should also be possible to implement this as a function instead, so that it can be conveniently located in .bashrc .

 function align_bottom() { (( VIS = $(tput lines) - 2 )) # height of visible area (yes " " | head -n $VIS; cat | head -n $VIS) | tail -n $VIS } typeset -fx align_bottom # !! make it callable from subshell 

Usage will be the same:

 watch -n 120 "mysql_query | column -t | align_bottom" 

Note that watch executes the given command using sh -c , so, as Dennis noted in the comments, on systems that do not associate /bin/sh with /bin/bash , the above function approach will not work.

You can make it work with usign:

 watch -n 120 "mysql_query | column -t | bash -c align_bottom" 

but for portability and usability, it just uses a shell script.

+3
source

I don’t know if watch can do this, but I would use another tool to have multiple terminals and change the size at which watch works according to my needs.

A few such tools that may be helpful are:

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1392032/


All Articles