ruby - 使用 curses 创建菜单

标签 ruby curses

我正在尝试按照本教程进行操作: http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/keys.html

但我恐怕一点也相处不好。

这不是世界上最好的代码,但它说明了我想做的事情:

        require "curses"
        include Curses

        init_screen #initialize first screen
        start_color #
        noecho

    close = false


        t1 = Thread.new{
        four = Window.new(5,60,2,2)
        four.box('|', '-')


            t2 = Thread.new{

                menu = Window.new(7,40,7,2)
                menu.box('|', '-')
                menu.setpos 1,1
                menu.addstr "item_one"
                menu.setpos 2,1

                menu.attrset(A_STANDOUT)

                menu.addstr "item_two"
                menu.setpos 3,1

                menu.attrset(A_NORMAL)

                menu.addstr "item_three"
                menu.setpos 4,1
                menu.addstr "item_four"

                menu.getch
            }
            t2.join

        while close == false
            ch = four.getch
            case ch
                when 'w'
                    four.setpos 2,1
                    four.addstr 'move up'
                    four.refresh
                when 's'
                    four.setpos 2,1
                    four.addstr 'move down'
                    four.refresh
                when 'x'
                    close = true
            end
        end


        }
        t1.join

通过按 W 和 D 键,我想在菜单窗口中上下移动突出显示的菜单项。完全不知道如何移动该突出显示。这意味着在代码中移动属性 setter 。我真的不知道。 Curses 方面的资源不多。

最佳答案

您的示例中不需要线程。

你可以在这里看到如何做到这一点:https://gist.github.com/phoet/6988038

require "curses"
include Curses

init_screen
start_color
noecho

def draw_menu(menu, active_index=nil)
  4.times do |i|
    menu.setpos(i + 1, 1)
    menu.attrset(i == active_index ? A_STANDOUT : A_NORMAL)
    menu.addstr "item_#{i}"
  end
end

def draw_info(menu, text)
  menu.setpos(1, 10)
  menu.attrset(A_NORMAL)
  menu.addstr text
end

position = 0

menu = Window.new(7,40,7,2)
menu.box('|', '-')
draw_menu(menu, position)
while ch = menu.getch
  case ch
  when 'w'
    draw_info menu, 'move up'
    position -= 1
  when 's'
    draw_info menu, 'move down'
    position += 1
  when 'x'
    exit
  end
  position = 3 if position < 0
  position = 0 if position > 3
  draw_menu(menu, position)
end

关于ruby - 使用 curses 创建菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19372766/

相关文章:

python:当底层库记录到标准输出时使用ncurses

c++ - 我可以在没有 tputs 或 putp 的情况下使用 tparm()

ruby-on-rails - 有没有办法在 Ruby on Rails 的控制台中检查命令的性能?

mysql - rake 有问题。 rails

ruby-on-rails - bundle 安装不适用于 capistrano

ruby 诅咒 : how to get pure white

ruby - 如何使用 Ruby 将列标题写入 csv 文件?

ruby - Ruby 中的链接方法,并将 block 注入(inject)链中

控制台界面教程和提示 (pdcurses)

c++ - Valgrind 在使用适当的 free() 和 end() 后显示 ncurses 命令的内存泄漏