lib/textbringer/controller.rb in textbringer-0.2.1 vs lib/textbringer/controller.rb in textbringer-0.2.2

- old
+ new

@@ -34,18 +34,26 @@ @current_prefix_arg = nil @echo_immediately = false @recording_keyboard_macro = nil @last_keyboard_macro = nil @executing_keyboard_macro = nil + @next_tick_queue = [] + @next_tick_queue_mutex = Mutex.new + @next_tick_input, @next_tick_output = IO.pipe end + def close + @next_tick_input.close + @next_tick_output.close + end + def command_loop(tag) catch(tag) do loop do begin echo_input - c = read_char + c = read_event break if c.nil? Window.echo_area.clear_message @last_key = c @key_sequence << @last_key cmd = key_binding(@key_sequence) @@ -90,26 +98,60 @@ end end end def wait_input(msecs) + # TODO: Check @next_tick_queue if executing_keyboard_macro? return @executing_keyboard_macro.first end Window.current.wait_input(msecs) end - def read_char - read_char_with_keyboard_macro(:read_char) + def next_tick(&block) + @next_tick_queue_mutex.synchronize do + @next_tick_queue.push(block) + end + @next_tick_output.write("\n") end - def read_char_nonblock - read_char_with_keyboard_macro(:read_char_nonblock) + def read_event + event = read_event_nonblock + if event + return event + end + loop do + if Window.echo_area.active? + wait_files = [STDIN] + else + wait_files = [STDIN, @next_tick_input] + end + files, = IO.select(wait_files, [], [], 1) + # KEY_RESIZE may be returned even if STDIN is not included in files. + event = read_event_nonblock + if event + return event + end + if !Window.echo_area.active? && files&.include?(@next_tick_input) + c = @next_tick_input.read_nonblock(1, exception: false) + if !c.nil? && c != :wait_readable + block = @next_tick_queue_mutex.synchronize { + @next_tick_queue.shift + } + block.call + Window.redisplay + end + end + end end + def read_event_nonblock + read_event_with_keyboard_macro(:read_event_nonblock) + end + def received_keyboard_quit? - while key = read_char_nonblock + while key = read_event_nonblock if GLOBAL_MAP.lookup([key]) == :keyboard_quit return true end end false @@ -206,22 +248,22 @@ GLOBAL_MAP.lookup(key_sequence) end private - def read_char_with_keyboard_macro(read_char_method) + def read_event_with_keyboard_macro(read_event_method) if !executing_keyboard_macro? - c = call_read_char_method(read_char_method) + c = call_read_event_method(read_event_method) if @recording_keyboard_macro @recording_keyboard_macro.push(c) end c else @executing_keyboard_macro.shift end end - def call_read_char_method(read_char_method) - Window.current.send(read_char_method) + def call_read_event_method(read_event_method) + Window.current.send(read_event_method) end end end