Class: TuiTui::Screen
- Inherits:
-
Object
- Object
- TuiTui::Screen
- 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
-
#chrome ⇒ Object
readonly
Returns the value of attribute chrome.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
Class Method Summary collapse
- .mouse_default ⇒ Object
- .run(input: $stdin, output: $stdout, depth: ColorDepth.detect, mouse: mouse_default, box: ENV["TUITUI_BOX"]) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #copy(text) ⇒ Object
-
#initialize(console, input, output, depth, mouse: true, box: nil) ⇒ Screen
constructor
A new instance of Screen.
- #invalidate ⇒ Object
- #mouse ⇒ Object
-
#mouse=(enabled) ⇒ Object
Toggle mouse reporting mid-session (so an app can release the mouse for a native terminal selection while a read-only pane is open).
-
#render(canvas) ⇒ Object
Render ‘canvas`: the compositor computes the (full or per-row diff) escape string, then the cursor is repositioned (or hidden).
- #size ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(console, input, output, depth, mouse: true, box: nil) ⇒ Screen
Returns a new instance of Screen.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tui_tui/screen.rb', line 38 def initialize(console, input, output, depth, mouse: true, box: nil) @input = input @output = output @box_override = box # ASCII until start probes a real TTY; safe for non-TTY/StringIO callers. @chrome = BoxChrome::ASCII @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
#chrome ⇒ Object (readonly)
Returns the value of attribute chrome.
53 54 55 |
# File 'lib/tui_tui/screen.rb', line 53 def chrome @chrome end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
53 54 55 |
# File 'lib/tui_tui/screen.rb', line 53 def events @events end |
Class Method Details
.mouse_default ⇒ Object
34 35 36 |
# File 'lib/tui_tui/screen.rb', line 34 def self.mouse_default !%w[0 off false].include?(ENV["TUITUI_MOUSE"]) end |
.run(input: $stdin, output: $stdout, depth: ColorDepth.detect, mouse: mouse_default, box: ENV["TUITUI_BOX"]) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tui_tui/screen.rb', line 20 def self.run(input: $stdin, output: $stdout, depth: ColorDepth.detect, mouse: mouse_default, box: ENV["TUITUI_BOX"]) 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, box: box) screen.start begin yield screen ensure screen.close end end |
Instance Method Details
#close ⇒ Object
100 |
# File 'lib/tui_tui/screen.rb', line 100 def close = @session.close |
#copy(text) ⇒ Object
95 96 97 98 |
# File 'lib/tui_tui/screen.rb', line 95 def copy(text) @output.write(Ansi.clipboard(text)) @output.flush end |
#invalidate ⇒ Object
91 92 93 |
# File 'lib/tui_tui/screen.rb', line 91 def invalidate @previous = nil end |
#mouse ⇒ Object
61 |
# File 'lib/tui_tui/screen.rb', line 61 def mouse = @session.mouse |
#mouse=(enabled) ⇒ Object
Toggle mouse reporting mid-session (so an app can release the mouse for a native terminal selection while a read-only pane is open).
57 58 59 |
# File 'lib/tui_tui/screen.rb', line 57 def mouse=(enabled) @session.mouse = enabled 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.
81 82 83 84 85 86 87 88 89 |
# File 'lib/tui_tui/screen.rb', line 81 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 |
#size ⇒ Object
75 |
# File 'lib/tui_tui/screen.rb', line 75 def size = @term_size.size |
#start ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tui_tui/screen.rb', line 63 def start @session.start # Probe box-drawing support once, after raw mode + alt screen, before the # first render/next_event so the DSR reply never reaches the key reader. @chrome = BoxChrome.resolve( input: @input, output: @output, term_cols: size.cols, env: {"TUITUI_BOX" => @box_override} ) end |