How to create top similar interface in Ruby

I want to create an application with a text interface similar to Linux 'top' command in Ruby. What tools and / or techniques can be used to create the user interface? Specifically, I want the console window area to be constantly updated, as well as the ability to press keys to control the display.

0


a source to share


3 answers


Ncurses is great for console applications, and you can find bindings for it for a lot of languages ​​(or just use shell scripts). There's even a damn gtk ( http://zemljanka.sourceforge.net/cursed/ ), although I think work on it stopped a while ago.

You didn't mention your platform, but for OS / X there is a large little application called Geektool ( http://projects.tynsoe.org/en/geektool/ ) that allows you to add script output to the desktop. I am using a little ruby ​​script to create a list of my best processes:



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

    }

}

      

(It might have been better, but this was the first ruby ​​script I have ever written, and since it works, I never revisited it to improve it, and it doesn't accept input. Anyway, it might help give you some ideas)

+1


a source


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



+2


a source


there stfl , but I don't know if ruby ​​bindings are supported. ncurses is probably the best choice.

0


a source







All Articles