Class: TuiTui::BoxProber

Inherits:
Object
  • Object
show all
Defined in:
lib/tui_tui/box_prober.rb

Overview

Measures how many columns a string of box-drawing glyphs actually occupies on the live terminal, by printing them and asking for the cursor position (DSR).

Constant Summary collapse

CPR =

Cursor Position Report: ESC [ row ; col R -> capture the column.

/\e\[\d+;(\d+)R/
MAX_REPLY_BYTES =
64

Instance Method Summary collapse

Constructor Details

#initialize(glyphs: BoxChrome::PROBE_GLYPHS, timeout: 0.2, wait: nil) ⇒ BoxProber

Returns a new instance of BoxProber.



13
14
15
16
17
# File 'lib/tui_tui/box_prober.rb', line 13

def initialize(glyphs: BoxChrome::PROBE_GLYPHS, timeout: 0.2, wait: nil)
  @glyphs = glyphs
  @timeout = timeout
  @wait = wait || method(:wait_readable)
end

Instance Method Details

#measure_all(input:, output:) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/tui_tui/box_prober.rb', line 19

def measure_all(input:, output:)
  output.write("\r")     # known baseline: column 1
  output.write(@glyphs)  # advances by the sum of glyph widths
  output.write("\e[6n")  # DSR: request cursor position
  output.flush
  col = read_column(input)
  cleanup(output)
  col.nil? ? -1 : col - 1
end