Module: TTY::Command::Window
- Defined in:
- lib/tty/command/window.rb,
lib/tty/command/window/ansi.rb,
lib/tty/command/window/block.rb,
lib/tty/command/window/runner.rb,
lib/tty/command/window/spinner.rb,
lib/tty/command/window/version.rb,
lib/tty/command/window/emulator.rb,
lib/tty/command/window/coordinator.rb,
lib/tty/command/window/integration.rb,
lib/tty/command/window/input_router.rb,
lib/tty/command/window/trap_manager.rb,
lib/tty/command/window/child_session.rb,
lib/tty/command/window/window_options.rb
Overview
Namespace for the windowed-run extension.
Requiring this file adds #run_windowed and
#run_windowed! to TTY::Command.
Defined Under Namespace
Modules: ANSI, Integration Classes: Block, ChildSession, Coordinator, Emulator, InputRouter, Runner, Spinner, TrapManager, Unavailable, WindowOptions
Constant Summary collapse
- DEFAULT_LINES =
5- DEFAULT_SCROLLBACK =
10_000- DEFAULT_CAPTURE_MAX_BYTES =
10 * 1024 * 1024
- OPTION_KEYS =
Option keys consumed by run_windowed and stripped before any delegation to plain tty-command.
%i[ lines title on_exit scrollback output_log interactive capture capture_max_bytes output window width on_unavailable tty ].freeze
- ON_EXIT_MODES =
%i[freeze dump_on_failure collapse].freeze
- CAPTURE_MODES =
%i[raw stripped screen].freeze
- ON_UNAVAILABLE_MODES =
%i[fallback raise].freeze
- VERSION =
"0.1.0"
Class Attribute Summary collapse
-
.assume_tty ⇒ Boolean
Treat non-TTY outputs as terminals.
Class Method Summary collapse
-
.apply_tty_alias!(window_options) ⇒ Object
private
Translate the legacy
tty:key intowindow:with a one-time deprecation warning. -
.normalize_options(window_options, cmd) ⇒ WindowOptions
Validate and default the window options.
-
.pty_available? ⇒ Boolean
True when the PTY library is usable.
-
.register_at_exit ⇒ Object
Install the at_exit cursor-restore hook once.
-
.renderable?(output) ⇒ Boolean
Whether a windowed run can render onto the given IO.
-
.split_options(args) ⇒ Array(Hash, Array)
Extract window-specific keys from a trailing options hash.
-
.strip_ansi(text) ⇒ String
Strip ANSI escape sequences (CSI, OSC, DCS, simple escapes) from a string.
-
.validate_on_unavailable!(window_options) ⇒ Object
private
Fail fast on typos / unsupported values for
on_unavailable:(see follow-up review P0-4). -
.windows? ⇒ Boolean
True when running on Windows (no PTY support).
-
.with_assume_tty(value = true) ⇒ Object
private
Set Window.assume_tty for the duration of the block and restore the previous value on exit — including on exception.
Class Attribute Details
.assume_tty ⇒ Boolean
Treat non-TTY outputs as terminals.
54 55 56 |
# File 'lib/tty/command/window.rb', line 54 def assume_tty @assume_tty end |
Class Method Details
.apply_tty_alias!(window_options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Translate the legacy tty: key into window: with a one-time
deprecation warning. Preserves compatibility with pre-rename
callers for one minor version.
140 141 142 143 144 145 146 147 148 |
# File 'lib/tty/command/window/integration.rb', line 140 def apply_tty_alias!() return unless .key?(:tty) value = .delete(:tty) return if .key?(:window) warn_tty_deprecation [:window] = value end |
.normalize_options(window_options, cmd) ⇒ WindowOptions
Validate and default the window options.
169 170 171 |
# File 'lib/tty/command/window/integration.rb', line 169 def (, cmd) WindowOptions.build(, cmd) end |
.pty_available? ⇒ Boolean
Returns true when the PTY library is usable.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tty/command/window.rb', line 76 def pty_available? return @pty_available unless @pty_available.nil? @pty_available = begin require "pty" true rescue LoadError false end end |
.register_at_exit ⇒ Object
Install the at_exit cursor-restore hook once.
174 175 176 177 178 179 |
# File 'lib/tty/command/window/integration.rb', line 174 def register_at_exit return if @at_exit_registered @at_exit_registered = true at_exit { Coordinator.restore_all } end |
.renderable?(output) ⇒ Boolean
Whether a windowed run can render onto the given IO.
92 93 94 95 96 |
# File 'lib/tty/command/window.rb', line 92 def renderable?(output) return false if windows? || !pty_available? assume_tty || (output.respond_to?(:tty?) && output.tty?) end |
.split_options(args) ⇒ Array(Hash, Array)
Extract window-specific keys from a trailing options hash.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/tty/command/window/integration.rb', line 123 def (args) return [{}, args] unless args.last.respond_to?(:to_hash) = args.last.to_hash = .slice(*OPTION_KEYS) remaining = .except(*OPTION_KEYS) plain_args = args[0..-2] plain_args << remaining unless remaining.empty? [, plain_args] end |
.strip_ansi(text) ⇒ String
Strip ANSI escape sequences (CSI, OSC, DCS, simple escapes) from a string.
102 103 104 |
# File 'lib/tty/command/window.rb', line 102 def strip_ansi(text) ANSI.strip(text) end |
.validate_on_unavailable!(window_options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Fail fast on typos / unsupported values for on_unavailable:
(see follow-up review P0-4).
154 155 156 157 158 159 160 161 162 |
# File 'lib/tty/command/window/integration.rb', line 154 def validate_on_unavailable!() return unless .key?(:on_unavailable) mode = [:on_unavailable] return if ON_UNAVAILABLE_MODES.include?(mode) raise ArgumentError, "on_unavailable must be one of #{ON_UNAVAILABLE_MODES.join(', ')}" end |
.windows? ⇒ Boolean
Returns true when running on Windows (no PTY support).
71 72 73 |
# File 'lib/tty/command/window.rb', line 71 def windows? !!(RbConfig::CONFIG["host_os"] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/) end |
.with_assume_tty(value = true) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set assume_tty for the duration of the block and restore the previous value on exit — including on exception.
62 63 64 65 66 67 68 |
# File 'lib/tty/command/window.rb', line 62 def with_assume_tty(value = true) # rubocop:disable Style/OptionalBooleanParameter previous = assume_tty self.assume_tty = value yield ensure self.assume_tty = previous end |