Module: CLI::UI::StdoutRouter
- Extended by:
- T::Sig
- Defined in:
- lib/cli/ui/stdout_router.rb
Defined Under Namespace
Constant Summary collapse
- WRITE_WITHOUT_CLI_UI =
:write_without_cli_ui
- NotEnabled =
Class.new(StandardError)
Class Attribute Summary collapse
-
.duplicate_output_to ⇒ Object
Returns the value of attribute duplicate_output_to.
Class Method Summary collapse
- .assert_enabled! ⇒ Object
- .current_id ⇒ Object
- .disable ⇒ Object
- .enable ⇒ Object
- .enabled?(stream = $stdout) ⇒ Boolean
- .ensure_activated ⇒ Object
- .with_enabled(&block) ⇒ Object
- .with_id(on_streams:, &block) ⇒ Object
Methods included from T::Sig
Class Attribute Details
.duplicate_output_to ⇒ Object
Returns the value of attribute duplicate_output_to.
180 181 182 |
# File 'lib/cli/ui/stdout_router.rb', line 180 def duplicate_output_to @duplicate_output_to end |
Class Method Details
.assert_enabled! ⇒ Object
205 206 207 |
# File 'lib/cli/ui/stdout_router.rb', line 205 def assert_enabled! raise NotEnabled unless enabled? end |
.current_id ⇒ Object
200 201 202 |
# File 'lib/cli/ui/stdout_router.rb', line 200 def current_id Thread.current[:cliui_output_id] end |
.disable ⇒ Object
238 239 240 241 242 243 244 |
# File 'lib/cli/ui/stdout_router.rb', line 238 def disable return false unless enabled?($stdout) && enabled?($stderr) deactivate($stdout) deactivate($stderr) true end |
.enable ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/cli/ui/stdout_router.rb', line 224 def enable return false if enabled?($stdout) || enabled?($stderr) activate($stdout, :stdout) activate($stderr, :stderr) true end |
.enabled?(stream = $stdout) ⇒ Boolean
233 234 235 |
# File 'lib/cli/ui/stdout_router.rb', line 233 def enabled?(stream = $stdout) stream.respond_to?(WRITE_WITHOUT_CLI_UI) end |
.ensure_activated ⇒ Object
219 220 221 |
# File 'lib/cli/ui/stdout_router.rb', line 219 def ensure_activated enable unless enabled? end |
.with_enabled(&block) ⇒ Object
210 211 212 213 214 215 |
# File 'lib/cli/ui/stdout_router.rb', line 210 def with_enabled(&block) enable yield ensure disable end |
.with_id(on_streams:, &block) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/cli/ui/stdout_router.rb', line 187 def with_id(on_streams:, &block) require 'securerandom' id = format('%05d', rand(10**5)) Thread.current[:cliui_output_id] = { id: id, streams: on_streams.map { |stream| T.cast(stream, IOLike) }, } yield(id) ensure Thread.current[:cliui_output_id] = nil end |