如何逐行打印数组元素并使用 Ruby Curses select 每行?
How to print elements of array line by line and select each individual line with Ruby Curses?
我目前正在 Ruby Curses 中打印第 0-3 行。我还创建了一个包含动物名称的数组。
我的程序目前输出
dog + 0cat + 0bird + 0rat + 0
dog + 1cat + 1bird + 1rat + 1
dog + 2cat + 2bird + 2rat + 2
dog + 3cat + 3bird + 3rat + 3
我希望它输出类似
的内容
dog + 0
cat + 1
bird + 2
rat + 3
有没有办法在不同的行上列出数组的每个元素,并且能够 select 每行?
这是我正在处理的功能
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)
arr = []
arr << "dog"
arr << "cat"
arr << "bird"
arr << "rat"
arr.each do |item|
menu.addstr "#{item} + #{i}"
end
end
end
我试过使用 arr.each 和 arr.each_index 但它给了我相同的输出。
这是完整的程序。
更新
下面的菜单看起来像我想要的那样,但是当按 'w' 或 's' 滚动菜单时,它会同时 select 所有 4 个元素。有没有办法让它一次只能 selected 一个元素?
require "curses"
include Curses
init_screen
start_color
noecho
def draw_menu(menu, active_index=nil)
4.times do |i|
menu.setpos(1, 1)
menu.attrset(i == active_index ? A_STANDOUT : A_NORMAL)
arr = []
arr << "dog"
arr << "cat"
arr << "bird"
arr << "rat"
arr.each_with_index do |element, index|
menu.addstr "#{element} + #{index}\n"
end
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
不确定 4.times
试图做什么,但我认为它是将相同的文本设置 4 次到屏幕上的相同位置。对于这 4 个中的每一个,如果当前的 4 个项目集与 active_index
相同,您会将所有 4 个设置为相同的样式(A_STANDOUT
而不是 A_NORMAL
)。
似乎对我有用并且我认为是有意的是这样的:
def draw_menu(menu, active_index=nil)
%w(dog cat bird rat).each_with_index do |element, index|
menu.setpos(index + 1, 1)
menu.attrset(index == active_index ? A_STANDOUT : A_NORMAL)
menu.addstr("%-4s + #{index}" % element)
end
menu.setpos(5, 1) # set the cursor on the line after the menu items
end
然后在你的 draw_info
中我看不到文本输出的位置,但是如果我将它更改为 setpos(5, 1)
它在菜单后的行中变得可见:
def draw_info(menu, text)
menu.setpos(5, 1)
menu.attrset(A_NORMAL)
menu.addstr text
end
我目前正在 Ruby Curses 中打印第 0-3 行。我还创建了一个包含动物名称的数组。 我的程序目前输出
dog + 0cat + 0bird + 0rat + 0
dog + 1cat + 1bird + 1rat + 1
dog + 2cat + 2bird + 2rat + 2
dog + 3cat + 3bird + 3rat + 3
我希望它输出类似
的内容dog + 0
cat + 1
bird + 2
rat + 3
有没有办法在不同的行上列出数组的每个元素,并且能够 select 每行?
这是我正在处理的功能
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)
arr = []
arr << "dog"
arr << "cat"
arr << "bird"
arr << "rat"
arr.each do |item|
menu.addstr "#{item} + #{i}"
end
end
end
我试过使用 arr.each 和 arr.each_index 但它给了我相同的输出。
这是完整的程序。
更新
下面的菜单看起来像我想要的那样,但是当按 'w' 或 's' 滚动菜单时,它会同时 select 所有 4 个元素。有没有办法让它一次只能 selected 一个元素?
require "curses"
include Curses
init_screen
start_color
noecho
def draw_menu(menu, active_index=nil)
4.times do |i|
menu.setpos(1, 1)
menu.attrset(i == active_index ? A_STANDOUT : A_NORMAL)
arr = []
arr << "dog"
arr << "cat"
arr << "bird"
arr << "rat"
arr.each_with_index do |element, index|
menu.addstr "#{element} + #{index}\n"
end
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
不确定 4.times
试图做什么,但我认为它是将相同的文本设置 4 次到屏幕上的相同位置。对于这 4 个中的每一个,如果当前的 4 个项目集与 active_index
相同,您会将所有 4 个设置为相同的样式(A_STANDOUT
而不是 A_NORMAL
)。
似乎对我有用并且我认为是有意的是这样的:
def draw_menu(menu, active_index=nil)
%w(dog cat bird rat).each_with_index do |element, index|
menu.setpos(index + 1, 1)
menu.attrset(index == active_index ? A_STANDOUT : A_NORMAL)
menu.addstr("%-4s + #{index}" % element)
end
menu.setpos(5, 1) # set the cursor on the line after the menu items
end
然后在你的 draw_info
中我看不到文本输出的位置,但是如果我将它更改为 setpos(5, 1)
它在菜单后的行中变得可见:
def draw_info(menu, text)
menu.setpos(5, 1)
menu.attrset(A_NORMAL)
menu.addstr text
end