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.



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

def clear
  @operations << :clear
end

#disable_mouse_trackingObject

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



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

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.



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

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

#enter_alt_screenObject

Records an enter-alt-screen operation.



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

def enter_alt_screen
  @operations << :enter_alt_screen
end

#hide_cursorObject

Records a hide-cursor operation.



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

def hide_cursor
  @operations << :hide_cursor
end

#leave_alt_screenObject

Records a leave-alt-screen operation.



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

def leave_alt_screen
  @operations << :leave_alt_screen
end

#mouse_enabled?Boolean

Returns whether mouse tracking is currently enabled.

Returns:

  • (Boolean)


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

def mouse_enabled?
  @mouse_enabled
end

#move_cursor(row, column) ⇒ Object

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



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

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.



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

def show_cursor
  @operations << :show_cursor
end

#sizeObject

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



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

def size
  [@width, @height]
end

#write_frame(frame) ⇒ Object

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



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

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.



46
47
48
49
50
# File 'lib/charming/internal/terminal/memory_backend.rb', line 46

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