Class: TUITD::State
- Inherits:
-
Object
- Object
- TUITD::State
- Defined in:
- lib/tui_td/state.rb
Overview
Represents the parsed state of a terminal screen. Provides high-level query methods for AI consumption.
Instance Attribute Summary collapse
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#grid ⇒ Object
readonly
Returns the value of attribute grid.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #background_at(row, col) ⇒ Object
-
#find_text(pattern) ⇒ Object
Search for text across the entire terminal.
-
#foreground_at(row, col) ⇒ Object
Get the color at a specific cell.
-
#initialize(data) ⇒ State
constructor
A new instance of State.
-
#plain_text ⇒ Object
Get plain text of the entire terminal (no ANSI).
- #style_at(row, col) ⇒ Object
-
#text_at(row, col, length = @cols - col) ⇒ Object
Get text at a specific position.
- #to_ai_json ⇒ Object
Constructor Details
#initialize(data) ⇒ State
Returns a new instance of State.
9 10 11 12 13 14 |
# File 'lib/tui_td/state.rb', line 9 def initialize(data) @rows = data[:size][:rows] @cols = data[:size][:cols] @grid = data[:rows] @cursor = data[:cursor] end |
Instance Attribute Details
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
7 8 9 |
# File 'lib/tui_td/state.rb', line 7 def cols @cols end |
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
7 8 9 |
# File 'lib/tui_td/state.rb', line 7 def cursor @cursor end |
#grid ⇒ Object (readonly)
Returns the value of attribute grid.
7 8 9 |
# File 'lib/tui_td/state.rb', line 7 def grid @grid end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
7 8 9 |
# File 'lib/tui_td/state.rb', line 7 def rows @rows end |
Instance Method Details
#background_at(row, col) ⇒ Object
47 48 49 50 |
# File 'lib/tui_td/state.rb', line 47 def background_at(row, col) return nil if row >= @rows || col >= @cols @grid[row][col][:bg] end |
#find_text(pattern) ⇒ Object
Search for text across the entire terminal
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tui_td/state.rb', line 28 def find_text(pattern) results = [] @grid.each_with_index do |row, ri| text = row.map { |c| c[:char] }.join pos = 0 while (match = text.index(pattern, pos)) results << { row: ri, col: match, text: pattern, full_line: text } pos = match + 1 end end results end |
#foreground_at(row, col) ⇒ Object
Get the color at a specific cell
42 43 44 45 |
# File 'lib/tui_td/state.rb', line 42 def foreground_at(row, col) return nil if row >= @rows || col >= @cols @grid[row][col][:fg] end |
#plain_text ⇒ Object
Get plain text of the entire terminal (no ANSI)
17 18 19 |
# File 'lib/tui_td/state.rb', line 17 def plain_text @grid.map { |row| row.map { |c| c[:char] }.join.rstrip }.join("\n") end |
#style_at(row, col) ⇒ Object
52 53 54 55 56 |
# File 'lib/tui_td/state.rb', line 52 def style_at(row, col) return nil if row >= @rows || col >= @cols cell = @grid[row][col] { bold: cell[:bold], italic: cell[:italic], underline: cell[:underline] } end |
#text_at(row, col, length = @cols - col) ⇒ Object
Get text at a specific position
22 23 24 25 |
# File 'lib/tui_td/state.rb', line 22 def text_at(row, col, length = @cols - col) return "" if row >= @rows || col >= @cols @grid[row][col, length].map { |c| c[:char] }.join end |
#to_ai_json ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tui_td/state.rb', line 58 def to_ai_json h = extract_highlights cursor_info = @cursor.is_a?(Hash) ? @cursor : {} r = cursor_info[:row] || cursor_info["row"] || 0 c = cursor_info[:col] || cursor_info["col"] || 0 styled_count = h.count { |hl| hl[:bold] || hl[:italic] || hl[:underline] || hl[:fg] || hl[:bg] } summary = +"Cursor at [#{r},#{c}]. " summary << "#{styled_count} styled row#{styled_count == 1 ? '' : 's'}" fgs = h.flat_map { |hl| hl[:fg] }.compact.uniq bgs = h.flat_map { |hl| hl[:bg] }.compact.uniq summary << ", colors: fg=#{fgs.sort.join(',')}" unless fgs.empty? summary << ", bg=#{bgs.sort.join(',')}" unless bgs.empty? summary << "." { size: { rows: @rows, cols: @cols }, cursor: cursor_info, text: plain_text, highlights: h, summary: summary, } end |