Full screen script, curses-style, updating table output (a la top) on unixen

I am looking for some Linux packages / APIs that can display output in columns similar to top .

For example, continue to clear and rewrite the output in full screen at a given interval (I think watch probably does it quite well, but I hope some APIs wrap it on top).

Easy to sort by columns. In particular, if I sort by column A, then the next time I re-type everything, it remembers sorting by column A every time the output is updated.

And of course, ideally, it can handle keyboard input for me.

In general, I am looking for packages or APIs that can help me organize my output in such a way that " top " organizes it.

Just for clarity: what I'm showing may not be completely related to system statistics. I just love how top organizes content. For example, My content may be output (and it is constantly changing, so it needs to be cleaned and rewritten):

Col1 Col2 Time
12 4 13
13 5 19
14 5 15

I can press the "A" key and then sort by time. If I press key B, it is sorted on Col1. If I press the key, say C, it is sorted by Col2, etc. Etc.

And, of course, this output content can be completely in memory, organized in any data structures.

+6
source share
3 answers

I found the curses library in Python handy for this kind of requirement. It still does not support column management, but provides a decent solution with a reasonable language, good document and a controlled learning curve.

If anyone has a better offer, I would be happy to choose it as the best answer.

http://docs.python.org/howto/curses.html

+2
source

If you want to do this in a shell, watching + printf will be a quick and dirty place to run - watch to restart the script every few seconds, printf to format it like this:

 printf '%-20s %-20s\n' \ header1 header2 \ line1data1 line1data2 \ line2data1 line2data2 ... 

... with data apparently built and filed from an array. Just looping around in your script and emitting appropriate control codes to clear every line when you are going to overwrite it (and move to the beginning of the window at the beginning) is another sensible approach.

However, the right tool here is a layer built on top of curses. A number of higher-level text widgets already exist, but I don't know anything with direct table support.

If you wanted to write C, one such wrapper around the GAP.Browse curses >.

+2
source

There is a CDK . I tried, and everything is fine, a bit like GTK. But printf solution is much cleaner.

If you don't mind scripting languages, you can try rbcurse , which has more features. However, he has very poor documentation.

0
source

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


All Articles