Class: TuiTui::Screen

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

Overview

Terminal-facing screen owner: session lifecycle, event stream, and rendering.

Constant Summary collapse

DEFAULT_SIZE =
Size.new(rows: 24, cols: 80)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(console, input, output, depth, mouse: true) ⇒ Screen

Returns a new instance of Screen.



36
37
38
39
40
41
42
43
44
45
# File 'lib/tui_tui/screen.rb', line 36

def initialize(console, input, output, depth, mouse: true)
  @output = output
  @compositor = CanvasCompositor.new(depth: depth)
  @term_size = TerminalSize.new(console, default: DEFAULT_SIZE)
  @events = EventStream.new(input: input, size: @term_size)
  @session = TerminalSession.new(console: console, output: output, events: @events, mouse: mouse)
  @previous = nil
  # the cursor position last written (the session starts it hidden)
  @cursor = nil
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



47
48
49
# File 'lib/tui_tui/screen.rb', line 47

def events
  @events
end

Class Method Details

.mouse_defaultObject



32
33
34
# File 'lib/tui_tui/screen.rb', line 32

def self.mouse_default
  !%w[0 off false].include?(ENV["TUITUI_MOUSE"])
end

.run(input: $stdin, output: $stdout, depth: ColorDepth.detect, mouse: mouse_default) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tui_tui/screen.rb', line 18

def self.run(input: $stdin, output: $stdout, depth: ColorDepth.detect, mouse: mouse_default)
  console = IO.console
  # Let callers provide a non-interactive fallback for piped output.
  return yield(nil) if console.nil? || !output.tty?

  screen = new(console, input, output, depth, mouse: mouse)
  screen.start
  begin
    yield screen
  ensure
    screen.close
  end
end

Instance Method Details

#closeObject



76
# File 'lib/tui_tui/screen.rb', line 76

def close = @session.close

#copy(text) ⇒ Object



71
72
73
74
# File 'lib/tui_tui/screen.rb', line 71

def copy(text)
  @output.write(Ansi.clipboard(text))
  @output.flush
end

#invalidateObject



67
68
69
# File 'lib/tui_tui/screen.rb', line 67

def invalidate
  @previous = nil
end

#render(canvas) ⇒ Object

Render ‘canvas`: the compositor computes the (full or per-row diff) escape string, then the cursor is repositioned (or hidden). The cursor directive is appended only on a full repaint or when the cursor actually moved, so an idle identical re-render still writes nothing.



57
58
59
60
61
62
63
64
65
# File 'lib/tui_tui/screen.rb', line 57

def render(canvas)
  full = @previous.nil? || !@previous.same_size?(canvas)
  out = @compositor.render(@previous, canvas)
  out += cursor_directive(canvas) if full || canvas.cursor != @cursor
  @output.write(out)
  @output.flush
  @previous = canvas
  @cursor = canvas.cursor
end

#sizeObject



51
# File 'lib/tui_tui/screen.rb', line 51

def size = @term_size.size

#startObject



49
# File 'lib/tui_tui/screen.rb', line 49

def start = @session.start