Class: TrackChanges

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

Overview

FIXME: Roll this into the actual buffer.

Constant Summary collapse

CURSOR =

The cursor is rendered as an overlay - a cell repainted with this background. The AnsiBackend recognises it and turns it into a real terminal cursor; the X11 backend paints the block.

0xff00ff

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, adapter) ⇒ TrackChanges

Returns a new instance of TrackChanges.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/trackchanges.rb', line 9

def initialize buffer, adapter
  @buffer = buffer
  @adapter = adapter
  @cursor_pos = nil   # where the cursor overlay was last painted
  # When true, #set only mutates the buffer; rendering is deferred to the
  # next #draw_flush, which walks the buffer's damage (generation) instead
  # of drawing eagerly per cell. Default off (the proven eager path) while
  # the damage-driven path is validated against it; see test_damage.rb.
  @defer = false
  @last_flush_gen = 0
  @rows = 24      # overwritten by on_resize before use
  # When true, ALL rendering is suppressed (the buffer still mutates):
  # used to jump-scroll a flood of output by interpreting many chunks and
  # then doing ONE full redraw of the final screen, skipping the
  # intermediate frames the user would never see. The model (incl.
  # scrollback) stays correct; only the framebuffer is batched.
  @suspend = false
  clear
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



29
30
31
# File 'lib/trackchanges.rb', line 29

def buffer
  @buffer
end

#deferObject

Returns the value of attribute defer.



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

def defer
  @defer
end

#suspendObject

Returns the value of attribute suspend.



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

def suspend
  @suspend
end

Instance Method Details

#blinkyObject



50
51
52
# File 'lib/trackchanges.rb', line 50

def blinky        = @buffer.blinky
# Backend-facing queries the interpreter routes through the buffer rather
# than reaching the adapter directly (so Term talks only to its buffer).

#clearObject



36
37
38
39
40
41
42
43
# File 'lib/trackchanges.rb', line 36

def clear
  # Flush any batched text first: otherwise pending draws are emitted to
  # the screen AFTER the clear and survive it (stale content; the buffer
  # is already correct, so only the incremental render diverges).
  draw_flush
  @buffer.clear
  @adapter.clear unless suppressed?
end

#clear_cursorObject



160
161
162
163
164
165
# File 'lib/trackchanges.rb', line 160

def clear_cursor
  return if suppressed?
  return unless @cursor_pos
  redraw(*@cursor_pos)
  @cursor_pos = nil
end

#clear_line(*args) ⇒ Object



92
93
94
95
96
# File 'lib/trackchanges.rb', line 92

def clear_line(*args)
  draw_flush
  @buffer.clear_line(*args)
  @adapter.clear_line(*args) unless suppressed?
end

#delete_chars(*args) ⇒ Object



132
# File 'lib/trackchanges.rb', line 132

def delete_chars(*args)  = @buffer.delete_chars(*args)

#delete_lines(y, num, maxy) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/trackchanges.rb', line 78

def delete_lines(y, num, maxy)
  draw_flush
  # Delete repeatedly at the SAME row: each delete shifts the rows below
  # up into y, so deleting at y+i would skip every other line.
  num.times { @buffer.delete_line(y) }
  @adapter.delete_lines(y, num, @buffer.scroll_end||maxy) unless suppressed?
end

#draw_buffered(x, y, cell, force = false) ⇒ Object

This is a hack



246
247
248
249
250
251
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/trackchanges.rb', line 246

def draw_buffered(x,y,cell, force=false)
  @last_x ||= -255
  @last_y ||= -255
  @buf ||= ["",PALETTE_BASIC[7], PALETTE_BASIC[0],0]
  cell ||= [" "]

  #p [:buffered, x, y, cell, @bufx, @bufy, @buf, force]
  if @buf[0] && @buf[0].length > 160
    flush_buf
  elsif @last_y != y || @last_x + 1 != x
    flush_buf
  elsif (@buf[1] != cell[1]) or (@buf[2] != cell[2]) or (@buf[3] != cell[3])
    flush_buf
  else
  end

  # FIXME: It is possible this is called from multiple threads.
  # Uh oh. That *will* be trouble. Either changes must be serialized -
  # they certainly must be for the backend screen buffer - or
  # this must be made thread local.
  #
  @buf[0] ||= ""

  # This is to get better performance out of applications that
  # carelessly prints far more than they ought to.
  # *cough* my editor *cough*
  if force
    match = false
  else
    # Skip the draw if the buffer already holds this exact cell, or if
    # we're writing a default-background space over an unset cell.
    # Compared against the buffer's columnar storage directly, so no cell
    # Array is reconstructed per character.
    # FIXME: Make this more deliberate about *background* attributes.
    match = (cell[0] == 32 && cell[2] == BG && @buffer.unset?(x, y)) ||
            @buffer.cell_eq?(x, y, cell[0], cell[1], cell[2], cell[3])
  end

  # FIXME: The #to_s here is a workaround for thread sync issues.
  if @buf[0].to_s.empty?
    if match
      return
    else
      #p [:diff, x,y, cell, bcell]
    end
  elsif match
    # This heuristic could probably be better:
    # * Keep a count, and trigger on the *number of matches*
    #   instead of on the number of characters. This to e.g. prevent a
    #   single coinciding character from splitting up the rendering into
    #   8-char chunks
    #p [:match_non_empty, @buf[0].length]
    if @buf[0]&.length.to_i > 8
      # If flushing here, chop the buffer down to the point of the first
      # match.
      flush_buf
      return
    end
  end

  c = cell[0]

  @buf[1] ||= cell[1]
  @buf[2] ||= cell[2]
  @buf[3] ||= cell[3]
  @buf[0] ||= ""
  @buf[0] << (c || "")
  @bufx ||= x
  @bufy ||= y
  @last_x = x
  @last_y = y
end

#draw_cursor(x, y, visible) ⇒ Object

Render the cursor overlay at (x,y) if visible, after restoring the cell under its previous position. A no-op while scrolled back, so the live cursor doesn’t paint over the frozen history view.



152
153
154
155
156
157
158
# File 'lib/trackchanges.rb', line 152

def draw_cursor(x, y, visible)
  return if suppressed?
  clear_cursor
  return unless visible
  redraw_with(x, y, bg: CURSOR)
  @cursor_pos = [x, y]
end

#draw_flushObject

Public flush point. In the default (eager) mode draws already happened on #set, so this just emits the pending run. In damage-driven (defer) mode #set only mutates, so a flush first walks the buffer’s damage and draws the changed cells (run-batched) before emitting. Either way it then emits the run buffer, which also carries force-redraws (cursor, ICH/DCH, blink, selection).



209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/trackchanges.rb', line 209

def draw_flush
  return if @suspend   # jump-scrolling: defer all rendering to the redraw
  if @defer && !@adapter.scrollback_mode
    @buffer.each_damaged(@last_flush_gen) do |x, y, ch, fg, bg, flags|
      s = (@scratch ||= [])
      s[0], s[1], s[2], s[3] = ch, fg, bg, flags
      draw_buffered(x, y, s, true)
    end
    @last_flush_gen = @buffer.generation
  end
  flush_buf
end

#each_character(scrollback_offset = 0, &block) ⇒ Object



55
56
57
# File 'lib/trackchanges.rb', line 55

def each_character(scrollback_offset = 0, &block)
  @buffer.each_character(scrollback_offset, &block)
end

#each_character_between(*args, &block) ⇒ Object



137
# File 'lib/trackchanges.rb', line 137

def each_character_between(*args, &block) = @buffer.each_character_between(*args, &block)

#flush_bufObject

Emit the batched run and reset the batch. Internal: draw_buffered calls this on a run break, so it must NOT re-enter the damage walk above.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/trackchanges.rb', line 224

def flush_buf
  if @bufx && @buf && @buf[0] && !@buf[0].empty?
    c = @buf[0]
    fg = @buf[1] || PALETTE_BASIC[7]
    bg = @buf[2] || PALETTE_BASIC[0]
    if c == " " && fg == 0 && bg == 0 # Why?
    else
      lineattrs = @buffer.lineattrs(@bufy)
      flags = @buf[3].to_i
      #p [:flush, fg, c]
      @adapter.draw(@bufx, @bufy, c, fg, bg, flags, lineattrs)
    end
  end
  @buf = []
  @bufx = nil
  @bufy = nil
  @last_x = -2
  @last_y = -2
end

#get(x, y) ⇒ Object



47
# File 'lib/trackchanges.rb', line 47

def get(x,y)      = @buffer.get(x,y)

#insert(*args) ⇒ Object

Explicit delegations to the underlying buffer, replacing a catch-all method_missing so the buffer’s surface through TrackChanges is knowable. Each is a model-only mutation whose on-screen redraw the caller drives separately (Term#redraw_line_from_cursor after insert/delete_chars; Term#set_line_attrs after set_lineattrs) or a read-only query - none of them paint, which is why they bypass the adapter. (The scroll_start/scroll_end getters are defined above; only the setters delegate.)



131
# File 'lib/trackchanges.rb', line 131

def insert(*args)        = @buffer.insert(*args)

#insert_lines(y, num, maxy) ⇒ Object



86
87
88
89
90
# File 'lib/trackchanges.rb', line 86

def insert_lines(y, num, maxy)
  draw_flush
  num.times.each {|i| @buffer.insert_line(y+i) }
  @adapter.insert_lines(y, num, @buffer.scroll_end || maxy) unless suppressed?
end

#lineattrs(y) ⇒ Object

Methods that does not alter the buffer



46
# File 'lib/trackchanges.rb', line 46

def lineattrs(y)  = @buffer.lineattrs(y)

#on_resize(w, h) ⇒ Object



116
117
118
119
120
121
# File 'lib/trackchanges.rb', line 116

def on_resize(w,h)
  raise if !h
  @rows = h   # used as the default scroll-region bottom in scroll_up
  # FIXME: Window is currently resized separately.
  @buffer.on_resize(w,h)
end

#redraw(x, y) ⇒ Object



147
# File 'lib/trackchanges.rb', line 147

def redraw(x,y) = draw_buffered(x,y, @buffer.get(x,y), true)

#redraw_all(scrollback_offset = 0) ⇒ Object



167
168
169
170
171
172
173
174
# File 'lib/trackchanges.rb', line 167

def redraw_all(scrollback_offset = 0)
  @buffer.each_character(scrollback_offset) { |*args| draw_buffered(*args, true) }
  # We just force-drew every cell, so nothing is damaged relative to now:
  # advance the watermark before flushing so the damage walk doesn't redraw
  # it all again (and so the next incremental flush only sees new changes).
  @last_flush_gen = @buffer.generation
  draw_flush
end


139
140
141
142
143
144
145
# File 'lib/trackchanges.rb', line 139

def redraw_blink
  return nil if suppressed?
  b = @buffer.blinky
  return nil if b.empty?
  b.each { |x,y| redraw(x,y) }
  draw_flush
end

#redraw_cell_at(screen_x, screen_y, cell, fg: nil, bg: nil) ⇒ Object

Draw an already-resolved cell at a screen position, optionally overriding fg/bg. Used when the cell’s buffer row and its screen row differ (selection highlighting while scrolled back into scrollback).



187
188
189
190
191
192
193
# File 'lib/trackchanges.rb', line 187

def redraw_cell_at(screen_x, screen_y, cell, fg: nil, bg: nil)
  cell = Array(cell).dup
  cell[0] ||= " "
  cell[1] = fg if fg
  cell[2] = bg if bg
  draw_buffered(screen_x, screen_y, cell, true)
end

#redraw_display(screen_x, screen_y, scrollback_offset = 0) ⇒ Object

Repaint whatever is currently displayed at a screen position, given the active scrollback offset (so scrollback rows repaint their scrolled-off content rather than the live buffer’s).



198
199
200
201
# File 'lib/trackchanges.rb', line 198

def redraw_display(screen_x, screen_y, scrollback_offset = 0)
  buffer_y = screen_y - scrollback_offset
  draw_buffered(screen_x, screen_y, @buffer.get(screen_x, buffer_y), true)
end

#redraw_with(x, y, fg: nil, bg: nil) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/trackchanges.rb', line 176

def redraw_with(x,y, fg: nil, bg: nil)
  cell = Array(@buffer.get(x,y)).dup
  cell[0] ||= " "
  cell[1] = fg if fg
  cell[2] = bg if bg
  draw_buffered(x,y, cell, true)
end

#scroll_endObject



49
# File 'lib/trackchanges.rb', line 49

def scroll_end    = @buffer.scroll_end

#scroll_end=(v) ⇒ Object



135
# File 'lib/trackchanges.rb', line 135

def scroll_end=(v);   @buffer.scroll_end = v;   end

#scroll_startObject



48
# File 'lib/trackchanges.rb', line 48

def scroll_start  = @buffer.scroll_start

#scroll_start=(v) ⇒ Object



134
# File 'lib/trackchanges.rb', line 134

def scroll_start=(v); @buffer.scroll_start = v; end

#scroll_upObject

# Mutation

Scroll the region up one line: draw pending damage, scroll the model, then drive the backend - a blit, or (when scrolled back) just anchor the viewport so the frozen history lines stay in place. The blit’s inclusive bottom row is the scroll region’s, or the last screen row when unset.



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

def scroll_up
  draw_flush
  start  = @buffer.scroll_start.to_i
  bottom = @buffer.scroll_end || (@rows - 1)
  @buffer.scroll_up
  return if @suspend
  if @adapter.scrollback_mode
    @adapter.scrollback_anchor
  else
    @adapter.scroll_up(start, bottom)
  end
end

#scrollback_modeObject

Backend-facing queries the interpreter routes through the buffer rather than reaching the adapter directly (so Term talks only to its buffer).



53
# File 'lib/trackchanges.rb', line 53

def scrollback_mode = @adapter.scrollback_mode

#scrollback_sizeObject



136
# File 'lib/trackchanges.rb', line 136

def scrollback_size      = @buffer.scrollback_size

#set(x, y, c, fg, bg, mode) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/trackchanges.rb', line 98

def set(x,y,c,fg,bg,mode)
  # MUST be before the @buffer.set below, as draw_buffered compares
  # against the buffer's *current* content to avoid redundant redraws.
  # Skipped while scrolled back so live output does not paint over the
  # scrolled-back view (the buffer still updates).
  #
  # draw_buffered reads the cell's four fields synchronously and never
  # retains the array, so we reuse a per-instance scratch cell instead of
  # allocating [c,fg,bg,mode] per character. Safe: a single processing
  # thread, with no re-entrancy back into #set.
  unless @defer || @adapter.scrollback_mode
    s = (@scratch ||= [])
    s[0], s[1], s[2], s[3] = c, fg, bg, mode
    draw_buffered(x, y, s)
  end
  @buffer.set(x,y,c,fg,bg,mode)
end

#set_columns(cols) ⇒ Object



54
# File 'lib/trackchanges.rb', line 54

def set_columns(cols) = @adapter.set_columns(cols)

#set_lineattrs(*args) ⇒ Object



133
# File 'lib/trackchanges.rb', line 133

def set_lineattrs(*args) = @buffer.set_lineattrs(*args)

#suppressed?Boolean

Rendering is off either because we’re viewing scrollback history or because output is being jump-scrolled.

Returns:

  • (Boolean)


34
# File 'lib/trackchanges.rb', line 34

def suppressed? = @suspend || @adapter.scrollback_mode