Class: TTY::Command::Window::Block
- Inherits:
-
Object
- Object
- TTY::Command::Window::Block
- Defined in:
- lib/tty/command/window/block.rb
Overview
One live window in the terminal: an emulator plus title-bar state.
Blocks are registered with a Coordinator, which stacks and paints them. All emulator access is synchronized through the block's mutex because the runner thread feeds data while the render thread paints.
Constant Summary collapse
- SUCCESS_GLYPH =
"✔"- FAILURE_GLYPH =
"✖"- FOCUSED_MARKER =
"▸ "- UNFOCUSED_MARKER =
"▹ "
Instance Attribute Summary collapse
-
#focused ⇒ Object
Returns the value of attribute focused.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#on_exit ⇒ Object
readonly
Returns the value of attribute on_exit.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #collapsed? ⇒ Boolean
- #done? ⇒ Boolean
-
#dump_on_finalize? ⇒ Boolean
Whether finalization should replace this block with its full history.
-
#dump_text(pastel) ⇒ Object
Plain title line plus history, used when dumping on failure.
- #failure? ⇒ Boolean
-
#feed(data) ⇒ Object
Feed raw child output into the emulator.
-
#finish(success, runtime) ⇒ Object
Mark the block finished.
-
#full_text ⇒ Object
Full plain-text history (scrollback + visible screen).
-
#height ⇒ Object
Rendered height in terminal lines given the current state.
-
#initialize(emulator:, title:, lines:, on_exit:, interactive: false) ⇒ Block
constructor
A new instance of Block.
- #interactive? ⇒ Boolean
-
#render(width:, pastel:, frame:) ⇒ Array<String>
Render the block to an array of ANSI strings,
heightelements. -
#resize(width) ⇒ Object
Resize the emulator to a new terminal width.
- #running? ⇒ Boolean
- #success? ⇒ Boolean
Constructor Details
#initialize(emulator:, title:, lines:, on_exit:, interactive: false) ⇒ Block
Returns a new instance of Block.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tty/command/window/block.rb', line 27 def initialize(emulator:, title:, lines:, on_exit:, interactive: false) @emulator = emulator @title_enabled = title != false @title_text = title.is_a?(String) ? title : "" @lines = lines @on_exit = on_exit @interactive = interactive @status = :running @started_at = Block.clock @runtime = nil @focused = false @mutex = Mutex.new end |
Instance Attribute Details
#focused ⇒ Object
Returns the value of attribute focused.
20 21 22 |
# File 'lib/tty/command/window/block.rb', line 20 def focused @focused end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
19 20 21 |
# File 'lib/tty/command/window/block.rb', line 19 def lines @lines end |
#on_exit ⇒ Object (readonly)
Returns the value of attribute on_exit.
19 20 21 |
# File 'lib/tty/command/window/block.rb', line 19 def on_exit @on_exit end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
19 20 21 |
# File 'lib/tty/command/window/block.rb', line 19 def status @status end |
Class Method Details
.clock ⇒ Object
41 42 43 |
# File 'lib/tty/command/window/block.rb', line 41 def self.clock Process.clock_gettime(Process::CLOCK_MONOTONIC) end |
Instance Method Details
#collapsed? ⇒ Boolean
79 80 81 |
# File 'lib/tty/command/window/block.rb', line 79 def collapsed? done? && @on_exit == :collapse end |
#done? ⇒ Boolean
63 64 65 |
# File 'lib/tty/command/window/block.rb', line 63 def done? !running? end |
#dump_on_finalize? ⇒ Boolean
Whether finalization should replace this block with its full history.
84 85 86 |
# File 'lib/tty/command/window/block.rb', line 84 def dump_on_finalize? failure? && @on_exit == :dump_on_failure end |
#dump_text(pastel) ⇒ Object
Plain title line plus history, used when dumping on failure.
116 117 118 |
# File 'lib/tty/command/window/block.rb', line 116 def dump_text(pastel) "#{status_glyph(pastel)} #{@title_text} #{pastel.dim("• #{elapsed_text}")}\n#{full_text}" end |
#failure? ⇒ Boolean
71 72 73 |
# File 'lib/tty/command/window/block.rb', line 71 def failure? @status == :failure end |
#feed(data) ⇒ Object
Feed raw child output into the emulator.
46 47 48 |
# File 'lib/tty/command/window/block.rb', line 46 def feed(data) @mutex.synchronize { @emulator.feed(data) } end |
#finish(success, runtime) ⇒ Object
Mark the block finished.
54 55 56 57 |
# File 'lib/tty/command/window/block.rb', line 54 def finish(success, runtime) @runtime = runtime @status = success ? :success : :failure end |
#full_text ⇒ Object
Full plain-text history (scrollback + visible screen).
111 112 113 |
# File 'lib/tty/command/window/block.rb', line 111 def full_text @mutex.synchronize { @emulator.full_text } end |
#height ⇒ Object
Rendered height in terminal lines given the current state.
89 90 91 92 93 |
# File 'lib/tty/command/window/block.rb', line 89 def height return 1 if collapsed? (@title_enabled ? 1 : 0) + @lines end |
#interactive? ⇒ Boolean
75 76 77 |
# File 'lib/tty/command/window/block.rb', line 75 def interactive? @interactive end |
#render(width:, pastel:, frame:) ⇒ Array<String>
Render the block to an array of ANSI strings, height elements.
101 102 103 104 105 106 107 108 |
# File 'lib/tty/command/window/block.rb', line 101 def render(width:, pastel:, frame:) @mutex.synchronize do out = [] out << title_line(width, pastel, frame) if @title_enabled || collapsed? out.concat(@emulator.render_lines) unless collapsed? out end end |
#resize(width) ⇒ Object
Resize the emulator to a new terminal width. PTY resize is owned by ChildSession.
122 123 124 |
# File 'lib/tty/command/window/block.rb', line 122 def resize(width) @mutex.synchronize { @emulator.resize(cols: width) } end |
#running? ⇒ Boolean
59 60 61 |
# File 'lib/tty/command/window/block.rb', line 59 def running? @status == :running end |
#success? ⇒ Boolean
67 68 69 |
# File 'lib/tty/command/window/block.rb', line 67 def success? @status == :success end |