Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scripting full-screen, curses-style, updating tabular output (a la top) on unixen

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

For example, keep cleaning and rewriting the output to a full screen at a given interval (I guess watch probably does this good enough. But I am hoping to have some APIs that wrap on top of it).

Sort by columns easily. Particularly if I sort by column A, then when next time I re-print everything, it remembers to sort by that column A every time the output is refreshed.

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

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

Just to clarity: What I display might be completely unrelated to the system statistics. I just like the way top organizes the content. For example, My output content might be (and it's constantly changing, which is why it needs to be cleaned and rewritten):

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

I can hit a key say "A" then it sorts by Time. If I hit a key B then it sorts by Col1. If I hit a key say C then it sorts by Col2, etc, etc.

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

like image 979
CuriousMind Avatar asked Apr 20 '12 18:04

CuriousMind


People also ask

What is the curses module in python?

The curses library supplies a terminal-independent screen-painting and keyboard-handling facility for text-based terminals; such terminals include VT100s, the Linux console, and the simulated terminal provided by various programs.

What is curses module?

The curses module provides an interface to the curses library, the de-facto standard for portable advanced terminal handling. While curses is most widely used in the Unix environment, versions are available for Windows, DOS, and possibly other systems as well.

Does curses come with Python?

The curses package comes with the Python standard library. In Linux and Mac, the curses dependencies should already be installed so there is no extra steps needed. On Windows, you need to install one special Python package, windows-curses available on PyPI to add support.


2 Answers

If you wanted to do this in shell, watch + printf would be a quick and dirty place to start -- watch to rerun a script every few seconds, printf to do the formatting, roughly as so:

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

...with the data presumably being constructed in and fed from an array. Simply looping within your script and emitting the appropriate control codes to clear each line as you're about to overwrite it (and moving to the top of the window at the beginning) is another reasonable approach.

That said, the right tool here will be a layer built on top of curses. There are a number of higher-level textual widget sets out there already -- but I don't know of anything with direct support for tables.

If you were willing to write C, one such wrapper around curses is GAP.Browse.

like image 136
Charles Duffy Avatar answered Oct 10 '22 19:10

Charles Duffy


I found the curses library in Python handy for this sort of requirement. It still doesn't support column management, but it provides a decent solution with a sane language, a nice document and a manageable learning curve.

If anyone else has better suggestion, I would be happy to pick his as the best answer.

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

like image 30
CuriousMind Avatar answered Oct 10 '22 18:10

CuriousMind