Class: Muxr::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/muxr/terminal.rb

Overview

A minimal VT100/ANSI terminal emulator. It maintains a fixed grid of cells plus a cursor and parser state. Bytes fed from a PTY are interpreted into mutations of the grid which the Renderer then composites into the final frame. The emulator implements enough of the protocol to host typical interactive shells (bash, zsh) and line-oriented programs.

Defined Under Namespace

Classes: Cell

Constant Summary collapse

BOLD =
1
UNDERLINE =
2
REVERSE =
4
SCROLLBACK_MAX =
5000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows: 24, cols: 80) ⇒ Terminal

Returns a new instance of Terminal.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/muxr/terminal.rb', line 32

def initialize(rows: 24, cols: 80)
  @rows = rows
  @cols = cols
  @buffer = Array.new(rows) { Array.new(cols) { blank_cell } }
  @cursor_row = 0
  @cursor_col = 0
  @saved_cursor = [0, 0]
  @fg = nil
  @bg = nil
  @attrs = 0
  @autowrap_pending = false
  @scroll_top = 0
  @scroll_bottom = rows - 1
  @parser_state = :ground
  @parser_params = +""
  @feed_remainder = +"".b
  @dirty = true
  @scrollback = []
  @view_offset = 0
  @selection_anchor = nil
  @selection_cursor = nil
  @selection_mode = :linear
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



30
31
32
# File 'lib/muxr/terminal.rb', line 30

def cols
  @cols
end

#cursor_colObject (readonly)

Returns the value of attribute cursor_col.



30
31
32
# File 'lib/muxr/terminal.rb', line 30

def cursor_col
  @cursor_col
end

#cursor_rowObject (readonly)

Returns the value of attribute cursor_row.



30
31
32
# File 'lib/muxr/terminal.rb', line 30

def cursor_row
  @cursor_row
end

#rowsObject (readonly)

Returns the value of attribute rows.



30
31
32
# File 'lib/muxr/terminal.rb', line 30

def rows
  @rows
end

#selection_modeObject (readonly)

Returns the value of attribute selection_mode.



56
57
58
# File 'lib/muxr/terminal.rb', line 56

def selection_mode
  @selection_mode
end

#view_offsetObject (readonly)

Returns the value of attribute view_offset.



30
31
32
# File 'lib/muxr/terminal.rb', line 30

def view_offset
  @view_offset
end

Instance Method Details

#anchor_selection!(mode: :linear) ⇒ Object

Drop the anchor at the cursor’s current position. ‘mode` controls the selection shape: :linear (character-by-character, reading order) or :block (rectangular).



127
128
129
130
131
132
# File 'lib/muxr/terminal.rb', line 127

def anchor_selection!(mode: :linear)
  return unless @selection_cursor
  @selection_anchor = @selection_cursor.dup
  @selection_mode = mode
  @dirty = true
end

#cell(r, c) ⇒ Object



58
59
60
# File 'lib/muxr/terminal.rb', line 58

def cell(r, c)
  @buffer[r][c]
end

#clear_anchor!Object

Drop the anchor but keep the cursor so the user can continue navigating (vim’s behavior when pressing v while already in linear visual mode).



136
137
138
139
140
# File 'lib/muxr/terminal.rb', line 136

def clear_anchor!
  return unless @selection_anchor
  @selection_anchor = nil
  @dirty = true
end

#clear_dirty!Object



248
249
250
# File 'lib/muxr/terminal.rb', line 248

def clear_dirty!
  @dirty = false
end

#clear_selectionObject



186
187
188
189
190
191
# File 'lib/muxr/terminal.rb', line 186

def clear_selection
  return unless @selection_anchor
  @selection_anchor = nil
  @selection_cursor = nil
  @dirty = true
end

#dirty?Boolean

Returns:

  • (Boolean)


244
245
246
# File 'lib/muxr/terminal.rb', line 244

def dirty?
  @dirty
end

#extract_selection_textObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/muxr/terminal.rb', line 207

def extract_selection_text
  return "" unless @selection_anchor
  if @selection_mode == :block
    ar, ac = @selection_anchor
    br, bc = @selection_cursor
    min_r, max_r = ar <= br ? [ar, br] : [br, ar]
    min_c, max_c = ac <= bc ? [ac, bc] : [bc, ac]
    lines = []
    (min_r..max_r).each do |tr|
      row = timeline_row(tr)
      if row.nil? || min_c >= row.length
        lines << ""
        next
      end
      last = [max_c, row.length - 1].min
      chars = (min_c..last).map { |c| row[c]&.char || " " }
      lines << chars.join.rstrip
    end
    return lines.join("\n")
  end
  sr, sc, er, ec = ordered_selection
  lines = []
  (sr..er).each do |tr|
    row = timeline_row(tr)
    if row.nil?
      lines << ""
      next
    end
    first = (tr == sr) ? sc : 0
    last = (tr == er) ? ec : row.length - 1
    last = [last, row.length - 1].min
    chars = (first..last).map { |c| row[c]&.char || " " }
    lines << chars.join.rstrip
  end
  lines.join("\n")
end

#feed(data) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/muxr/terminal.rb', line 278

def feed(data)
  bytes = @feed_remainder + data.b
  @feed_remainder = +"".b
  str = bytes.dup.force_encoding(Encoding::UTF_8)
  unless str.valid_encoding?
    # Find the longest valid UTF-8 prefix and stash the remainder for the
    # next feed call so multi-byte characters don't get garbled across PTY
    # read boundaries.
    raw = bytes.bytes
    while raw.any?
      candidate = raw.pack("C*").force_encoding(Encoding::UTF_8)
      break if candidate.valid_encoding?
      @feed_remainder = ([raw.last] + @feed_remainder.bytes).pack("C*").b
      raw.pop
    end
    str = raw.pack("C*").force_encoding(Encoding::UTF_8)
    # Bail out completely if we couldn't decode anything yet.
    return if str.empty?
  end
  str.each_char { |c| process_char(c) }
  @dirty = true
end

#move_selection_cursor_by(dr, dc) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/muxr/terminal.rb', line 148

def move_selection_cursor_by(dr, dc)
  return unless @selection_cursor
  tr, tc = @selection_cursor
  ntr = (tr + dr).clamp(0, timeline_size - 1)
  ntc = (tc + dc).clamp(0, @cols - 1)
  return if ntr == tr && ntc == tc
  @selection_cursor = [ntr, ntc]
  ensure_selection_cursor_visible
  @dirty = true
end

#place_selection_cursor(r, c) ⇒ Object

Place the moving cursor at a viewport position without dropping an anchor — the user is still navigating, not yet selecting.



116
117
118
119
120
121
122
# File 'lib/muxr/terminal.rb', line 116

def place_selection_cursor(r, c)
  tr = timeline_row_for_visible(r).clamp(0, timeline_size - 1)
  tc = c.clamp(0, @cols - 1)
  @selection_cursor = [tr, tc]
  @selection_anchor = nil
  @dirty = true
end

#resize(rows, cols) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/muxr/terminal.rb', line 252

def resize(rows, cols)
  return if rows == @rows && cols == @cols
  new_buf = Array.new(rows) { Array.new(cols) { blank_cell } }
  keep_rows = [rows, @rows].min
  keep_cols = [cols, @cols].min
  src_start = @rows - keep_rows
  keep_rows.times do |i|
    keep_cols.times do |j|
      new_buf[i][j].copy_from(@buffer[src_start + i][j])
    end
  end
  @buffer = new_buf
  @rows = rows
  @cols = cols
  @scroll_top = 0
  @scroll_bottom = rows - 1
  @cursor_row = @cursor_row.clamp(0, rows - 1)
  @cursor_col = @cursor_col.clamp(0, cols - 1)
  @autowrap_pending = false
  # Selection points at timeline rows whose shape can't be remapped
  # meaningfully through a resize, so drop it rather than show a smear.
  @selection_anchor = nil
  @selection_cursor = nil
  @dirty = true
end

#scroll_back(n = 1) ⇒ Object



86
87
88
# File 'lib/muxr/terminal.rb', line 86

def scroll_back(n = 1)
  set_view_offset(@view_offset + n)
end

#scroll_forward(n = 1) ⇒ Object



90
91
92
# File 'lib/muxr/terminal.rb', line 90

def scroll_forward(n = 1)
  set_view_offset(@view_offset - n)
end

#scroll_to_bottomObject



98
99
100
# File 'lib/muxr/terminal.rb', line 98

def scroll_to_bottom
  set_view_offset(0)
end

#scroll_to_topObject



94
95
96
# File 'lib/muxr/terminal.rb', line 94

def scroll_to_top
  set_view_offset(@scrollback.size)
end

#scrollback_sizeObject



78
79
80
# File 'lib/muxr/terminal.rb', line 78

def scrollback_size
  @scrollback.size
end

#scrolled_back?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/muxr/terminal.rb', line 82

def scrolled_back?
  @view_offset > 0
end

#selected_at_visible?(r, c) ⇒ Boolean

Returns:

  • (Boolean)


193
194
195
196
197
# File 'lib/muxr/terminal.rb', line 193

def selected_at_visible?(r, c)
  return false unless @selection_anchor
  tr = timeline_row_for_visible(r)
  inside_selection?(tr, c)
end

#selection_active?Boolean

———- selection ———-

Selection coordinates are in the combined “timeline”:

0..scrollback.size-1                 → @scrollback rows
scrollback.size..scrollback.size+rows-1 → @buffer rows

so the selection stays anchored to the same text as the user pages through history.

Returns:

  • (Boolean)


110
111
112
# File 'lib/muxr/terminal.rb', line 110

def selection_active?
  !@selection_anchor.nil?
end

#selection_cursor_to(tr, tc) ⇒ Object



159
160
161
162
163
164
165
166
# File 'lib/muxr/terminal.rb', line 159

def selection_cursor_to(tr, tc)
  return unless @selection_cursor
  ntr = tr.clamp(0, timeline_size - 1)
  ntc = tc.clamp(0, @cols - 1)
  @selection_cursor = [ntr, ntc]
  ensure_selection_cursor_visible
  @dirty = true
end

#selection_cursor_to_bottomObject



182
183
184
# File 'lib/muxr/terminal.rb', line 182

def selection_cursor_to_bottom
  selection_cursor_to(timeline_size - 1, @cols - 1)
end

#selection_cursor_to_line_endObject



173
174
175
176
# File 'lib/muxr/terminal.rb', line 173

def selection_cursor_to_line_end
  return unless @selection_cursor
  selection_cursor_to(@selection_cursor[0], @cols - 1)
end

#selection_cursor_to_line_startObject



168
169
170
171
# File 'lib/muxr/terminal.rb', line 168

def selection_cursor_to_line_start
  return unless @selection_cursor
  selection_cursor_to(@selection_cursor[0], 0)
end

#selection_cursor_to_topObject



178
179
180
# File 'lib/muxr/terminal.rb', line 178

def selection_cursor_to_top
  selection_cursor_to(0, 0)
end

#selection_cursor_visibleObject



199
200
201
202
203
204
205
# File 'lib/muxr/terminal.rb', line 199

def selection_cursor_visible
  return nil unless @selection_cursor
  tr, tc = @selection_cursor
  vr = tr - (@scrollback.size - @view_offset)
  return nil unless vr.between?(0, @rows - 1)
  [vr, tc]
end

#start_selection_at_visible(r, c, mode: :linear) ⇒ Object

Convenience for tests: place cursor at (r,c) AND anchor immediately.



143
144
145
146
# File 'lib/muxr/terminal.rb', line 143

def start_selection_at_visible(r, c, mode: :linear)
  place_selection_cursor(r, c)
  anchor_selection!(mode: mode)
end

#visible_cell(r, c) ⇒ Object

Returns the Cell that should be visible at (r, c) given the current scrollback view_offset. When view_offset == 0 this is the live grid. When view_offset > 0, rows in the top of the visible area are sourced from @scrollback instead.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/muxr/terminal.rb', line 66

def visible_cell(r, c)
  return @buffer[r][c] if @view_offset.zero?
  idx = @scrollback.size - @view_offset + r
  if idx < @scrollback.size
    row = @scrollback[idx]
    return blank_cell if row.nil? || c >= row.length
    row[c]
  else
    @buffer[idx - @scrollback.size][c]
  end
end