如何在使用Curses

时间:2017-08-06 20:18:18

标签: ruby ncurses curses

我想学习Ruby,所以我想我会尝试同时学习和诅咒。我正在创建一个导航菜单,允许用户选择将运行系统命令的选项。

我已经达到创建导航菜单的程度,我可以使用箭头键循环选项。

现在我尝试读取Enter键作为输入,并在按下Enter键时运行系统命令。例如:

    input = menu.getch
    if input == ENTER

in

  position = 3 if position < 0
  position = 0 if position > 3
  draw_menu(menu, position)
  if position == 0
    draw_info menu, 'You selected option 0'
    input = menu.getch
    if input == ENTER
      menu.clear
      menu.refresh
      puts (system 'ls')

    end

当按下Enter键时,系统命令可以正常工作(种类),但是我遇到了一个问题,如果选择或突出显示该选项,系统命令也会运行。如果按下Enter键,我只想让它工作。

如果我将其更改为

    if input == 'k'

系统命令仅在按下Enter键时运行。突出显示或选中时不会运行。这是我希望它工作的方式。

关于如何让Enter键与&#39; k&#39;相同的任何想法。键?

这是我的代码。

require 'curses'
include Curses

# Top Lie
SCREEN_WIDTH       = 90
HEADER_HEIGHT      = 4
HEADER_WIDTH       = SCREEN_WIDTH

# Bottom Line
SCREEN_WIDTH2      = 90
HEADER_HEIGHT2      = 1
HEADER_WIDTH2       = SCREEN_WIDTH2

Curses.init_screen
Curses.curs_set(0)  # Invisible cursor

Curses.start_color

Curses.noecho # echo or noecho to display user input
Curses.nonl
Curses.raw
Curses.stdscr.nodelay = 1

Curses.init_pair(1, Curses::COLOR_WHITE, Curses::COLOR_BLUE)
Curses.init_pair(2, Curses::COLOR_WHITE, Curses::COLOR_BLUE)

begin

  # Top Line
  header_window = Curses::Window.new(HEADER_HEIGHT, HEADER_WIDTH, 0, 0)   # (height, width, top, left)
  header_window.color_set(1)
  header_window << "Curses example".center(HEADER_WIDTH)
  header_window.refresh

  # Bottom Line
  header2_window = Curses::Window.new(HEADER_HEIGHT, HEADER_WIDTH, 23, 0)
  header2_window.color_set(2)
  header2_window << "Curses example".center(HEADER_WIDTH)
  header2_window.refresh

  # Building a static window

    def draw_menu(menu, active_index=nil)
      ["This is option 0.", "This is option 1.", "This is option 2.", "This is option 3."].each_with_index do |element, index|
      # "w" for word array
      # It's a shortcut for arrays
        menu.setpos(index + 1, 1)
        menu.attrset(index == active_index ? A_STANDOUT : A_NORMAL)
        menu.addstr("#{index} - %-10s" % element)   # %-Xs makes sure array words line up evenly if you place index after element
                                                    # you can change 17 to another number
      end
      menu.setpos(5, 1)
    end

    def draw_info(menu, text)
      menu.setpos(6, 1)  # sets the position of move up and down
                         # for example, menu.setpos(1, 10) moves to another
                         # location
      menu.attrset(A_NORMAL)
      menu.addstr text
    end

    position = 0

    menu = Window.new(20, 70, 2, 2)  # (height, width, top, left)
    menu.keypad = true  # enable keypad which allows arrow keys
    #menu.box('|', '-')
    draw_menu(menu, position)
    while ch = menu.getch
      stdscr.keypad = true
      case ch
      when KEY_UP, 'w'
        #draw_info menu, 'move up'
        position -= 1
      when KEY_DOWN, '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)
      if position == 0
        draw_info menu, 'You selected option 0'
        input = menu.getch
        if input == 'k'  # I want this to be ENTER
            menu.clear
            menu.refresh
            puts (system 'ls')  # This does not work well.  I need to fix it.

        end
      elsif position == 1
        draw_info menu, 'You selected option 1'
      elsif position == 2
        draw_info menu, 'You selected option 2'
      else position == 3
        draw_info menu, 'You selected option 3'
      end       
    end

rescue => ex
  Curses.close_screen
end

1 个答案:

答案 0 :(得分:1)

可能还有其他问题,但您可能遇到的问题是键盘上的 Enter 键通常不是curses(ncurses)调用KEY_ENTER的键。相反,它通常被分配到数字键盘上的 Enter (并且当启用键盘模式时它会发送转义序列) 。这在 proper way of catching control+key in ncurses 中进行了讨论。

另外,使用

进行初始化
Curses.raw

防止curses在任何情况下都返回KEY_ENTER。也就是说,curses不会识别分配给该代码的转义序列。您可能想尝试

Curses.cbreak
Curses.stdscr.keypad = 1

(除非某些内容未显示KEY_UPKEY_DOWN有效)将有助于其他KEY_xxx符号,以及您期望&#34的位置;输入&#34; ,接受换行符 \n 字符。这更有可能达到你的期望。

进一步阅读: