Class: Shellfie::TerminalScreen

Inherits:
Object
  • Object
show all
Defined in:
lib/shellfie/terminal_screen.rb

Constant Summary collapse

CSI =
/\A\e\[([0-9;:?]*)([ -\/]*)?([@-~])\z/
STRING_CONTROL =
/\e(?:\]|P|_|\^).*?(?:\a|\e\\)/m
TOKENS =
/#{STRING_CONTROL}|\e\[[0-9;:?]*[ -\/]*[@-~]|\e[78DEM]|[\r\n\b\t\a]|\X/m
MAX_PENDING_CONTROL_BYTES =
1_048_576
CONTINUATION =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns: 80, rows: 24, tab_width: 8, graphics_policy: "ignore") ⇒ TerminalScreen

Returns a new instance of TerminalScreen.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/shellfie/terminal_screen.rb', line 16

def initialize(columns: 80, rows: 24, tab_width: 8, graphics_policy: "ignore")
  @columns = columns
  @rows = rows
  @tab_width = tab_width
  @graphics_policy = graphics_policy
  @cells = Array.new(rows) { Array.new(columns) }
  @row = 0
  @column = 0
  @saved_cursor = [0, 0]
  @scroll_top = 0
  @scroll_bottom = rows - 1
  @primary_state = nil
  @pending_control = +""
  @discarding_control = nil
  @discard_control_tail = +""
  @wrap_pending = false
  @ansi_parser = AnsiParser.new
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



14
15
16
# File 'lib/shellfie/terminal_screen.rb', line 14

def column
  @column
end

#columnsObject (readonly)

Returns the value of attribute columns.



14
15
16
# File 'lib/shellfie/terminal_screen.rb', line 14

def columns
  @columns
end

#rowObject (readonly)

Returns the value of attribute row.



14
15
16
# File 'lib/shellfie/terminal_screen.rb', line 14

def row
  @row
end

#rowsObject (readonly)

Returns the value of attribute rows.



14
15
16
# File 'lib/shellfie/terminal_screen.rb', line 14

def rows
  @rows
end

Instance Method Details

#feed(text) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shellfie/terminal_screen.rb', line 35

def feed(text)
  input = text.to_s.dup.force_encoding(Encoding::UTF_8).scrub
  input = discard_control_prefix(input)
  return self if input.empty? && @discarding_control

  input = @pending_control << input
  if @graphics_policy == "error" && input.match?(AnsiNormalizer::GRAPHICS_CONTROL_PREFIX)
    raise ValidationError, "Terminal graphics are not supported; use window.graphics_policy: ignore to discard them"
  end
  input, @pending_control = split_incomplete_control(input)
  input.scan(TOKENS).each { |token| process(token) }
  self
end

#linesObject



49
50
51
# File 'lib/shellfie/terminal_screen.rb', line 49

def lines
  visible_rows.map { |cells| cells.reject { |cell| cell.equal?(CONTINUATION) }.map { |cell| cell&.text || " " }.join.rstrip }
end

#render_linesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shellfie/terminal_screen.rb', line 53

def render_lines
  visible_rows.map do |cells|
    visible = cells.reject { |cell| cell.equal?(CONTINUATION) }
    last = visible.rindex { |cell| cell && cell.text != " " }
    next "" unless last

    visible[0..last].chunk { |cell| style_key(cell) }.map do |_style, group|
      cells_in_style = group.to_a
      text = cells_in_style.map { |cell| cell&.text || " " }.join
      styled_cell(cells_in_style.first, text)
    end.join
  end
end

#to_sObject



67
68
69
# File 'lib/shellfie/terminal_screen.rb', line 67

def to_s
  lines.join("\n")
end