Class: UI::ReadlineWindow
- Inherits:
-
Object
- Object
- UI::ReadlineWindow
- Defined in:
- lib/ektoplayer/ui.rb
Instance Method Summary collapse
- #active? ⇒ Boolean
- #feed(c) ⇒ Object
-
#initialize ⇒ ReadlineWindow
constructor
A new instance of ReadlineWindow.
- #readline(pos, size, prompt: '', add_hist: false, &block) ⇒ Object
- #redraw ⇒ Object
Constructor Details
#initialize ⇒ ReadlineWindow
Returns a new instance of ReadlineWindow.
156 157 158 159 160 161 |
# File 'lib/ektoplayer/ui.rb', line 156 def initialize Readline.input, @readline_in_write = IO.pipe Readline.output = File.open(File::NULL, ?w) @window = ICurses.newwin(0,0,0,0) @thread = nil end |
Instance Method Details
#active? ⇒ Boolean
163 |
# File 'lib/ektoplayer/ui.rb', line 163 def active?; !@thread.nil? end |
#feed(c) ⇒ Object
194 195 196 197 198 199 |
# File 'lib/ektoplayer/ui.rb', line 194 def feed(c) @readline_in_write.putc(c) Thread.pass @thread = nil if c == ?\n.ord redraw end |
#readline(pos, size, prompt: '', add_hist: false, &block) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/ektoplayer/ui.rb', line 177 def readline(pos, size, prompt: '', add_hist: false, &block) @thread ||= Thread.new do @size, @pos, @prompt = size, pos, prompt begin Readline.set_screen_size(size.height, size.width) Readline.delete_text @readline_in_write.read_nonblock(100) rescue nil block.(Readline.readline(prompt, add_hist)) ensure @window.clear @thread = nil UI::Canvas.update_screen(true) end end end |
#redraw ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ektoplayer/ui.rb', line 165 def redraw @window.resize(@size.height, @size.width) @window.mvwin(@pos.y, @pos.x) @window.erase buffer = @prompt + Readline.line_buffer.to_s @window.addstr(buffer[(buffer.size - @size.width).clamp(0, buffer.size)..-1]) @window.move(0, Readline.point + @prompt.size) @window.refresh rescue nil end |