Class: TrackChanges
- Inherits:
-
Object
- Object
- TrackChanges
- 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
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#defer ⇒ Object
Returns the value of attribute defer.
-
#suspend ⇒ Object
Returns the value of attribute suspend.
Instance Method Summary collapse
- #blinky ⇒ Object
- #clear ⇒ Object
- #clear_cursor ⇒ Object
- #clear_line(*args) ⇒ Object
- #delete_chars(*args) ⇒ Object
- #delete_lines(y, num, maxy) ⇒ Object
-
#draw_buffered(x, y, cell, force = false) ⇒ Object
This is a hack.
-
#draw_cursor(x, y, visible) ⇒ Object
Render the cursor overlay at (x,y) if
visible, after restoring the cell under its previous position. -
#draw_flush ⇒ Object
Public flush point.
- #each_character(scrollback_offset = 0, &block) ⇒ Object
- #each_character_between(*args, &block) ⇒ Object
-
#flush_buf ⇒ Object
Emit the batched run and reset the batch.
- #get(x, y) ⇒ Object
-
#glyph_span(x, buffer_y) ⇒ Object
The screen-column span [x0,x1] of the glyph occupying (x, buffer_y).
-
#initialize(buffer, adapter) ⇒ TrackChanges
constructor
A new instance of TrackChanges.
-
#insert(*args) ⇒ Object
Explicit delegations to the underlying buffer, replacing a catch-all method_missing so the buffer's surface through TrackChanges is knowable.
- #insert_lines(y, num, maxy) ⇒ Object
-
#lineattrs(y) ⇒ Object
Methods that does not alter the buffer.
- #on_resize(w, h) ⇒ Object
- #redraw(x, y) ⇒ Object
- #redraw_all(scrollback_offset = 0) ⇒ Object
- #redraw_blink ⇒ Object
-
#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.
-
#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).
- #redraw_with(x, y, fg: nil, bg: nil) ⇒ Object
- #scroll_end ⇒ Object
- #scroll_end=(v) ⇒ Object
- #scroll_start ⇒ Object
- #scroll_start=(v) ⇒ Object
-
#scroll_up ⇒ Object
Mutation.
-
#scrollback_mode ⇒ Object
Backend-facing queries the interpreter routes through the buffer rather than reaching the adapter directly (so Term talks only to its buffer).
- #scrollback_size ⇒ Object
- #set(x, y, c, fg, bg, mode) ⇒ Object
- #set_columns(cols) ⇒ Object
- #set_lineattrs(*args) ⇒ Object
-
#suppressed? ⇒ Boolean
Rendering is off either because we're viewing scrollback history or because output is being jump-scrolled.
Constructor Details
#initialize(buffer, adapter) ⇒ TrackChanges
Returns a new instance of TrackChanges.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/trackchanges.rb', line 11 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
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
31 32 33 |
# File 'lib/trackchanges.rb', line 31 def buffer @buffer end |
#defer ⇒ Object
Returns the value of attribute defer.
32 33 34 |
# File 'lib/trackchanges.rb', line 32 def defer @defer end |
#suspend ⇒ Object
Returns the value of attribute suspend.
32 33 34 |
# File 'lib/trackchanges.rb', line 32 def suspend @suspend end |
Instance Method Details
#blinky ⇒ Object
52 53 54 |
# File 'lib/trackchanges.rb', line 52 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). |
#clear ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/trackchanges.rb', line 38 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_cursor ⇒ Object
187 188 189 190 191 192 |
# File 'lib/trackchanges.rb', line 187 def clear_cursor return if suppressed? return unless @cursor_pos redraw(*@cursor_pos) @cursor_pos = nil end |
#clear_line(*args) ⇒ Object
98 99 100 101 102 |
# File 'lib/trackchanges.rb', line 98 def clear_line(*args) draw_flush @buffer.clear_line(*args) @adapter.clear_line(*args) unless suppressed? end |
#delete_chars(*args) ⇒ Object
138 |
# File 'lib/trackchanges.rb', line 138 def delete_chars(*args) = @buffer.delete_chars(*args) |
#delete_lines(y, num, maxy) ⇒ Object
84 85 86 87 88 89 90 |
# File 'lib/trackchanges.rb', line 84 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
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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/trackchanges.rb', line 277 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.
179 180 181 182 183 184 185 |
# File 'lib/trackchanges.rb', line 179 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_flush ⇒ Object
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).
240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/trackchanges.rb', line 240 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
57 58 59 |
# File 'lib/trackchanges.rb', line 57 def each_character(scrollback_offset = 0, &block) @buffer.each_character(scrollback_offset, &block) end |
#each_character_between(*args, &block) ⇒ Object
143 |
# File 'lib/trackchanges.rb', line 143 def each_character_between(*args, &block) = @buffer.each_character_between(*args, &block) |
#flush_buf ⇒ Object
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.
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/trackchanges.rb', line 255 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
49 |
# File 'lib/trackchanges.rb', line 49 def get(x,y) = @buffer.get(x,y) |
#glyph_span(x, buffer_y) ⇒ Object
The screen-column span [x0,x1] of the glyph occupying (x, buffer_y). A double-width glyph is stored as [head, WIDE_SPACER tail]; redrawing either cell on its own leaves the other half stale (cursor move, blink, a single-cell damage update), so every redraw path expands to the whole glyph. cell is an integer codepoint (or nil for an unset cell).
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/trackchanges.rb', line 158 def glyph_span(x, buffer_y) cell = @buffer.get(x, buffer_y) cp = cell && cell[0] if cp == CharWidth::WIDE_SPACER && x > 0 && (h = @buffer.get(x - 1, buffer_y)) && h[0] && CharWidth.width(h[0]) == 2 [x - 1, x] elsif cp && CharWidth.width(cp) == 2 [x, x + 1] else [x, x] end end |
#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.)
137 |
# File 'lib/trackchanges.rb', line 137 def insert(*args) = @buffer.insert(*args) |
#insert_lines(y, num, maxy) ⇒ Object
92 93 94 95 96 |
# File 'lib/trackchanges.rb', line 92 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
48 |
# File 'lib/trackchanges.rb', line 48 def lineattrs(y) = @buffer.lineattrs(y) |
#on_resize(w, h) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/trackchanges.rb', line 122 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
171 172 173 174 |
# File 'lib/trackchanges.rb', line 171 def redraw(x,y) x0, x1 = glyph_span(x, y) (x0..x1).each { |xx| draw_buffered(xx, y, @buffer.get(xx, y), true) } end |
#redraw_all(scrollback_offset = 0) ⇒ Object
194 195 196 197 198 199 200 201 |
# File 'lib/trackchanges.rb', line 194 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 |
#redraw_blink ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/trackchanges.rb', line 145 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).
217 218 219 220 221 222 223 |
# File 'lib/trackchanges.rb', line 217 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).
228 229 230 231 232 |
# File 'lib/trackchanges.rb', line 228 def redraw_display(screen_x, screen_y, scrollback_offset = 0) buffer_y = screen_y - scrollback_offset x0, x1 = glyph_span(screen_x, buffer_y) (x0..x1).each { |xx| draw_buffered(xx, screen_y, @buffer.get(xx, buffer_y), true) } end |
#redraw_with(x, y, fg: nil, bg: nil) ⇒ Object
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/trackchanges.rb', line 203 def redraw_with(x,y, fg: nil, bg: nil) x0, x1 = glyph_span(x, y) (x0..x1).each do |xx| cell = Array(@buffer.get(xx, y)).dup cell[0] ||= " " cell[1] = fg if fg cell[2] = bg if bg draw_buffered(xx, y, cell, true) end end |
#scroll_end ⇒ Object
51 |
# File 'lib/trackchanges.rb', line 51 def scroll_end = @buffer.scroll_end |
#scroll_end=(v) ⇒ Object
141 |
# File 'lib/trackchanges.rb', line 141 def scroll_end=(v); @buffer.scroll_end = v; end |
#scroll_start ⇒ Object
50 |
# File 'lib/trackchanges.rb', line 50 def scroll_start = @buffer.scroll_start |
#scroll_start=(v) ⇒ Object
140 |
# File 'lib/trackchanges.rb', line 140 def scroll_start=(v); @buffer.scroll_start = v; end |
#scroll_up ⇒ Object
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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/trackchanges.rb', line 67 def scroll_up draw_flush start = @buffer.scroll_start.to_i bottom = @buffer.scroll_end || (@rows - 1) @buffer.scroll_up # The content moved up in the buffer; keep any active text selection # pinned to it. Done before the @suspend bail-out so the selection stays # aligned even through jump-scrolled (unrendered) frames. @adapter.scroll_selection(start, bottom) return if @suspend if @adapter.scrollback_mode @adapter.scrollback_anchor else @adapter.scroll_up(start, bottom) end end |
#scrollback_mode ⇒ Object
Backend-facing queries the interpreter routes through the buffer rather than reaching the adapter directly (so Term talks only to its buffer).
55 |
# File 'lib/trackchanges.rb', line 55 def scrollback_mode = @adapter.scrollback_mode |
#scrollback_size ⇒ Object
142 |
# File 'lib/trackchanges.rb', line 142 def scrollback_size = @buffer.scrollback_size |
#set(x, y, c, fg, bg, mode) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/trackchanges.rb', line 104 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
56 |
# File 'lib/trackchanges.rb', line 56 def set_columns(cols) = @adapter.set_columns(cols) |
#set_lineattrs(*args) ⇒ Object
139 |
# File 'lib/trackchanges.rb', line 139 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.
36 |
# File 'lib/trackchanges.rb', line 36 def suppressed? = @suspend || @adapter.scrollback_mode |