Question: 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](http://linux.die.net/man/1/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.

Answer: 
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>
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](http://www.gap-system.org/Packages/browse.html).