59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/easy_dialog.rb', line 59
def ask
@result = Array.new
width = @choises.max_by {|s| s.length}.length + 1
num = 80 / width
highlight = 0
Curses.noecho
Curses.init_screen
Curses.start_color
Curses.stdscr.keypad(true)
Curses.init_pair 1, Curses::COLOR_BLACK, Curses::COLOR_RED
Curses.init_pair 2, Curses::COLOR_BLACK, Curses::COLOR_YELLOW
loop do
Curses.setpos 0, 0
Curses.attroff(Curses::A_COLOR)
Curses.addstr(@question)
@choises.each_with_index do |c,i|
Curses.setpos i/num + 1, i%num*width
if i == highlight
Curses.attrset(Curses.color_pair(1))
elsif @result.any? {|r| r.equal? c}
Curses.attrset(Curses.color_pair(2))
else
Curses.attroff(Curses::A_COLOR)
end
Curses.addstr(c)
end
Curses.refresh
case hoge = Curses.getch
when Curses::Key::RIGHT
highlight = highlight + 1
when Curses::Key::LEFT
highlight = highlight - 1
when Curses::Key::UP
highlight = highlight - num
when Curses::Key::DOWN
highlight = highlight + num
when 27
Curses.close_screen
return @result
when 10
if @result.any? {|r| r.equal? @choises[highlight]}
@result.delete_if {|r| r.equal? @choises[highlight]}
else
@result<<@choises[highlight]
end
end
if highlight >= @choises.length
highlight = @choises.length - 1
end
highlight = 0 if highlight < 0
end
end
|