如何用 Ruby 构建类似 top 的 UI

发布于 2024-07-21 12:39:41 字数 116 浏览 8 评论 0原文

我想构建一个具有基于文本的 UI 的应用程序,类似于 Ruby 中的 Linux 命令“top”。 我可以使用哪些工具包和/或技术来构建 UI? 特别是,我想要控制台窗口的一个区域不断更新,并且能够按键来操作显示。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糖 2024-07-28 12:39:41

Ncurses 非常适合控制台应用程序,您可以找到多种语言的绑定(或者仅使用 shell 脚本)。 甚至还有一个被诅咒的 gtk (http://zemljanka.sourceforge.net/cursed/)我想这方面的工作已经停止了一段时间了。

你没有提到你的平台,但对于 OS/X 有一个很棒的小应用程序,名为 Geektool (http: //projects.tynsoe.org/en/geektool/),它允许您将脚本输出放在桌面上。 我使用一个小的 ruby​​ 脚本来生成我的顶级进程列表:(

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

    }

}

这可能会更好,但这是我编写的第一个 ruby​​ 脚本,因为它有效,所以我从未重新访问它来改进它 - 并且不需要无论如何,它可能会帮助你提供一些想法)

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)

┈┾☆殇 2024-07-28 12:39:41

有关终端界面,请参阅 http://ncurses-ruby.berlios.de/

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

胡渣熟男 2024-07-28 12:39:41

stfl 但我不知道 ruby​​ 绑定是否得到维护。 ncurses 可能是您最好的选择。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文