Class: Charming::Internal::Terminal::MemoryBackend
- Inherits:
-
Object
- Object
- Charming::Internal::Terminal::MemoryBackend
- Includes:
- Adapter
- Defined in:
- lib/charming/internal/terminal/memory_backend.rb
Overview
MemoryBackend is an in-memory implementation of the terminal Adapter used by
RSpec specs. It serves events from a fixed events: list and records every
output operation in frames (rendered output) and operations (every method
call with its arguments), so tests can assert against observed output.
Instance Attribute Summary collapse
-
#escapes ⇒ Object
readonly
The array of out-of-band escape sequences written via
write_escape. -
#frames ⇒ Object
readonly
The array of rendered frame strings (one per
write_frameorwrite_linescall). -
#operations ⇒ Object
readonly
The array of recorded operation tuples: [:method_name, *args].
Instance Method Summary collapse
-
#clear ⇒ Object
Records a clear-screen operation.
-
#disable_mouse_tracking ⇒ Object
Marks the backend as having mouse tracking disabled and records the operation.
-
#enable_mouse_tracking(motion: :drag) ⇒ Object
Marks the backend as having mouse tracking enabled and records the operation with the requested motion mode (:drag or :all).
-
#enter_alt_screen ⇒ Object
Records an enter-alt-screen operation.
-
#exhausted? ⇒ Boolean
True when every pre-seeded event has been consumed.
-
#hide_cursor ⇒ Object
Records a hide-cursor operation.
-
#initialize(events: [], width: 80, height: 24) ⇒ MemoryBackend
constructor
events is the queue of pre-seeded events to return from
read_event. -
#input_pending? ⇒ Boolean
True when a pre-seeded event is still queued (mirrors a TTY with bytes ready).
-
#leave_alt_screen ⇒ Object
Records a leave-alt-screen operation.
-
#mouse_enabled? ⇒ Boolean
Returns whether mouse tracking is currently enabled.
-
#move_cursor(row, column) ⇒ Object
Records a move-cursor operation at the given (row, column) (1-based).
-
#read_event(timeout: nil) ⇒ Object
Pops the next pre-seeded event from the queue.
-
#show_cursor ⇒ Object
Records a show-cursor operation.
-
#size ⇒ Object
Returns the configured terminal dimensions as [width, height].
-
#write_escape(sequence) ⇒ Object
Records an out-of-band escape sequence, appending it to
escapesand recording the operation (so specs can assert both its content and its ordering relative to frames). -
#write_frame(frame) ⇒ Object
Stores frame as the current frame and appends it to
frames. -
#write_lines(line_changes, frame: nil) ⇒ Object
Applies the [row, line] line_changes to the current frame, then stores and records the result.
Constructor Details
#initialize(events: [], width: 80, height: 24) ⇒ MemoryBackend
events is the queue of pre-seeded events to return from read_event.
width/height set the initial terminal dimensions reported by size.
24 25 26 27 28 29 30 31 32 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 24 def initialize(events: [], width: 80, height: 24) @events = events.dup @width = width @height = height @frames = [] @operations = [] @escapes = [] @mouse_enabled = false end |
Instance Attribute Details
#escapes ⇒ Object (readonly)
The array of out-of-band escape sequences written via write_escape.
20 21 22 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 20 def escapes @escapes end |
#frames ⇒ Object (readonly)
The array of rendered frame strings (one per write_frame or write_lines call).
14 15 16 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 14 def frames @frames end |
#operations ⇒ Object (readonly)
The array of recorded operation tuples: [:method_name, *args].
17 18 19 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 17 def operations @operations end |
Instance Method Details
#clear ⇒ Object
Records a clear-screen operation.
96 97 98 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 96 def clear @operations << :clear end |
#disable_mouse_tracking ⇒ Object
Marks the backend as having mouse tracking disabled and records the operation.
118 119 120 121 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 118 def disable_mouse_tracking @mouse_enabled = false @operations << :disable_mouse_tracking end |
#enable_mouse_tracking(motion: :drag) ⇒ Object
Marks the backend as having mouse tracking enabled and records the operation with the requested motion mode (:drag or :all).
112 113 114 115 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 112 def enable_mouse_tracking(motion: :drag) @mouse_enabled = true @operations << [:enable_mouse_tracking, motion] end |
#enter_alt_screen ⇒ Object
Records an enter-alt-screen operation.
76 77 78 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 76 def enter_alt_screen @operations << :enter_alt_screen end |
#exhausted? ⇒ Boolean
True when every pre-seeded event has been consumed. The Runtime stops its loop on an exhausted backend so tests can't hang waiting for input that never comes.
42 43 44 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 42 def exhausted? @events.empty? end |
#hide_cursor ⇒ Object
Records a hide-cursor operation.
91 92 93 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 91 def hide_cursor @operations << :hide_cursor end |
#input_pending? ⇒ Boolean
True when a pre-seeded event is still queued (mirrors a TTY with bytes ready). Lets the runtime's held-key coalescing drain seeded repeats in specs.
48 49 50 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 48 def input_pending? !@events.empty? end |
#leave_alt_screen ⇒ Object
Records a leave-alt-screen operation.
81 82 83 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 81 def leave_alt_screen @operations << :leave_alt_screen end |
#mouse_enabled? ⇒ Boolean
Returns whether mouse tracking is currently enabled.
124 125 126 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 124 def mouse_enabled? @mouse_enabled end |
#move_cursor(row, column) ⇒ Object
Records a move-cursor operation at the given (row, column) (1-based).
101 102 103 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 101 def move_cursor(row, column) @operations << [:move_cursor, row, column] end |
#read_event(timeout: nil) ⇒ Object
Pops the next pre-seeded event from the queue. Returns nil when the queue is empty.
35 36 37 38 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 35 def read_event(timeout: nil) @operations << [:read_event, timeout] @events.shift end |
#show_cursor ⇒ Object
Records a show-cursor operation.
86 87 88 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 86 def show_cursor @operations << :show_cursor end |
#size ⇒ Object
Returns the configured terminal dimensions as [width, height].
106 107 108 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 106 def size [@width, @height] end |
#write_escape(sequence) ⇒ Object
Records an out-of-band escape sequence, appending it to escapes and recording the
operation (so specs can assert both its content and its ordering relative to frames).
70 71 72 73 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 70 def write_escape(sequence) @escapes << sequence @operations << [:write_escape, sequence] end |
#write_frame(frame) ⇒ Object
Stores frame as the current frame and appends it to frames.
53 54 55 56 57 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 53 def write_frame(frame) @current_frame = frame @frames << frame @operations << [:write_frame, frame] end |
#write_lines(line_changes, frame: nil) ⇒ Object
Applies the [row, line] line_changes to the current frame, then stores and records the result. The full frame is taken from the optional frame: argument (when provided) or built by overlaying the changes on the previous frame.
62 63 64 65 66 |
# File 'lib/charming/internal/terminal/memory_backend.rb', line 62 def write_lines(line_changes, frame: nil) @current_frame = frame || apply_line_changes(line_changes) @frames << @current_frame @operations << [:write_lines, line_changes] end |