2013-10-15 106 views
2

我想按照這個教程的菜單: 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鍵,我想向上和向下的菜單項移動高亮在菜單窗口中。從字面上不理解我如何移動這個亮點。這將意味着在代碼中移動屬性設置器。我真的不知道。詛咒中沒有太多的資源。

+0

看看https://github.com/piotrmurach/tty-prompt – Nakilon

+0

請縮進代碼... – Felix

回答

4

您的示例中不需要線程。

你可以看到這可怎麼在這裏完成: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 
+0

真的好答案!你認爲你應該把它粘貼在這裏嗎? – Starkers