Class: Thaum::InputReader

Inherits:
Object
  • Object
show all
Defined in:
lib/thaum/input_reader.rb

Overview

Reads raw bytes from an input stream in a background thread and pushes KeyEvents onto a queue.

Constant Summary collapse

ESCAPE_TIMEOUT =

seconds to wait after a bare e before treating it as Escape

0.05
MAX_ESCAPE_EXTENDS =

cap extend reads so malformed input can’t hang the reader

4
ESC =
"\e"
CSI_INTRO =

‘[’ — Control Sequence Introducer

0x5b
SS3_INTRO =

‘O’ — Single Shift 3

0x4f
SGR_MOUSE_MARKER =

‘<’ — SGR mouse introducer

0x3c
CSI_FINAL_RANGE =

any byte in this range terminates a CSI

0x40..0x7e
SGR_MOUSE_FINAL =

‘M’ / ‘m’ terminate an SGR mouse sequence

[0x4d, 0x6d].freeze

Instance Method Summary collapse

Constructor Details

#initialize(input:, queue:, parser: EscapeParser.new) ⇒ InputReader

Returns a new instance of InputReader.



16
17
18
19
20
21
# File 'lib/thaum/input_reader.rb', line 16

def initialize(input:, queue:, parser: EscapeParser.new)
  @input  = input
  @queue  = queue
  @parser = parser
  @thread = nil
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/thaum/input_reader.rb', line 38

def alive?
  @thread&.alive? || false
end

#startObject



23
24
25
# File 'lib/thaum/input_reader.rb', line 23

def start
  @thread = Thread.new { run }
end

#stopObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/thaum/input_reader.rb', line 27

def stop
  return unless @thread

  # Give the thread a moment to finish on its own (e.g. input already closed).
  # If it's still blocked in readpartial, interrupt it so stop doesn't wait out
  # the full join timeout (~1s) on every quit.
  @thread.kill unless @thread.join(0.1)
  @thread.join(1)
  @thread = nil
end