Question

I want to build an application with a text based UI that is similar to the Linux command 'top', in Ruby. What toolkits and/or techniques can I use to build the UI? In particular I want an area of the console window that is constantly updating, and the ability to press keys to manipulate the display.

Was it helpful?

Solution

Ncurses is great for console apps and you can find bindings for it for lots of languages (or just use shell scripting). There's even a cursed gtk (http://zemljanka.sourceforge.net/cursed/) though I think work on it stopped quite awhile back.

You didn't mention your platform, but for OS/X there's a great little app called Geektool (http://projects.tynsoe.org/en/geektool/) which allows you to put script output on your desktop. I use a small ruby script to generate my top processes list:

puts %x{uptime}

IO.popen("ps aruxl") { |readme|
    pslist = readme.to_a
    pslist.shift # remove header line
    pslist.each_with_index { |i,index|
      ps = i.split
      psh = { user: ps[0], pid: ps[1], pcpu: ps[2], pmem: ps[3],
              vsz: ps[4], rss: ps[5], tty: ps[6], stat: ps[7],
              time: ps[8], uid: ps[9], ppid: ps[10], cpu: ps[11], pri: ps[12], 
              nice: ps[13], wchan: ps[14], cmd: ps[16..ps.size].join(" ") }
      printf("%-6s %-6s %-6s %s", "PID:", "%CPU:", "%Mem", "Command\n") if index == 0
      printf("%-6d %-6.1f %-6.1f %s\n", 
        psh[:pid].to_i, psh[:pcpu].to_f, psh[:pmem].to_f, psh[:cmd]) if index < 10

    }

}

(This could probably be better, but it was the first ruby script I ever wrote and since it works I've never revisited it to improve it - and it doesn't take input. Anyway, it might help give you some ideas)

OTHER TIPS

For a terminal interface, see http://ncurses-ruby.berlios.de/

there's stfl but i don't know if the ruby bindings are maintained. ncurses is probably your best bet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top