Class: Charming::Internal::Terminal::MemoryBackend

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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`.



21
22
23
24
25
26
27
28
# File 'lib/charming/internal/terminal/memory_backend.rb', line 21

def initialize(events: [], width: 80, height: 24)
  @events = events.dup
  @width = width
  @height = height
  @frames = []
  @operations = []
  @mouse_enabled = false
end

Instance Attribute Details

#framesObject (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

#operationsObject (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

#clearObject

Records a clear-screen operation.



79
80
81
# File 'lib/charming/internal/terminal/memory_backend.rb', line 79

def clear
  @operations << :clear
end

#disable_mouse_trackingObject

Marks the backend as having mouse tracking disabled and records the operation.



100
101
102
103
# File 'lib/charming/internal/terminal/memory_backend.rb', line 100

def disable_mouse_tracking
  @mouse_enabled = false
  @operations << :disable_mouse_tracking
end

#enable_mouse_trackingObject

Marks the backend as having mouse tracking enabled and records the operation.



94
95
96
97
# File 'lib/charming/internal/terminal/memory_backend.rb', line 94

def enable_mouse_tracking
  @mouse_enabled = true
  @operations << :enable_mouse_tracking
end

#enter_alt_screenObject

Records an enter-alt-screen operation.



59
60
61
# File 'lib/charming/internal/terminal/memory_backend.rb', line 59

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.

Returns:

  • (Boolean)


38
39
40
# File 'lib/charming/internal/terminal/memory_backend.rb', line 38

def exhausted?
  @events.empty?
end

#hide_cursorObject

Records a hide-cursor operation.



74
75
76
# File 'lib/charming/internal/terminal/memory_backend.rb', line 74

def hide_cursor
  @operations << :hide_cursor
end

#leave_alt_screenObject

Records a leave-alt-screen operation.



64
65
66
# File 'lib/charming/internal/terminal/memory_backend.rb', line 64

def leave_alt_screen
  @operations << :leave_alt_screen
end

#mouse_enabled?Boolean

Returns whether mouse tracking is currently enabled.

Returns:

  • (Boolean)


106
107
108
# File 'lib/charming/internal/terminal/memory_backend.rb', line 106

def mouse_enabled?
  @mouse_enabled
end

#move_cursor(row, column) ⇒ Object

Records a move-cursor operation at the given (row, column) (1-based).



84
85
86
# File 'lib/charming/internal/terminal/memory_backend.rb', line 84

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.



31
32
33
34
# File 'lib/charming/internal/terminal/memory_backend.rb', line 31

def read_event(timeout: nil)
  @operations << [:read_event, timeout]
  @events.shift
end

#show_cursorObject

Records a show-cursor operation.



69
70
71
# File 'lib/charming/internal/terminal/memory_backend.rb', line 69

def show_cursor
  @operations << :show_cursor
end

#sizeObject

Returns the configured terminal dimensions as [width, height].



89
90
91
# File 'lib/charming/internal/terminal/memory_backend.rb', line 89

def size
  [@width, @height]
end

#write_frame(frame) ⇒ Object

Stores frame as the current frame and appends it to ‘frames`.



43
44
45
46
47
# File 'lib/charming/internal/terminal/memory_backend.rb', line 43

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.



52
53
54
55
56
# File 'lib/charming/internal/terminal/memory_backend.rb', line 52

def write_lines(line_changes, frame: nil)
  @current_frame = frame || apply_line_changes(line_changes)
  @frames << @current_frame
  @operations << [:write_lines, line_changes]
end