Module: CLI::UI::StdoutRouter
- 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
TODO: remove this.
- .with_enabled ⇒ Object
- .with_id(on_streams:) ⇒ Object
Class Attribute Details
.duplicate_output_to ⇒ Object
Returns the value of attribute duplicate_output_to.
8 9 10 |
# File 'lib/cli/ui/stdout_router.rb', line 8 def duplicate_output_to @duplicate_output_to end |
Class Method Details
.assert_enabled! ⇒ Object
178 179 180 |
# File 'lib/cli/ui/stdout_router.rb', line 178 def assert_enabled! raise NotEnabled unless enabled? end |
.current_id ⇒ Object
174 175 176 |
# File 'lib/cli/ui/stdout_router.rb', line 174 def current_id Thread.current[:cliui_output_id] end |
.disable ⇒ Object
205 206 207 208 209 210 |
# File 'lib/cli/ui/stdout_router.rb', line 205 def disable return false unless enabled?($stdout) && enabled?($stderr) deactivate($stdout) deactivate($stderr) true end |
.enable ⇒ Object
194 195 196 197 198 199 |
# File 'lib/cli/ui/stdout_router.rb', line 194 def enable return false if enabled?($stdout) || enabled?($stderr) activate($stdout, :stdout) activate($stderr, :stderr) true end |
.enabled?(stream = $stdout) ⇒ Boolean
201 202 203 |
# File 'lib/cli/ui/stdout_router.rb', line 201 def enabled?(stream = $stdout) stream.respond_to?(WRITE_WITHOUT_CLI_UI) end |
.ensure_activated ⇒ Object
TODO: remove this
190 191 192 |
# File 'lib/cli/ui/stdout_router.rb', line 190 def ensure_activated enable unless enabled? end |
.with_enabled ⇒ Object
182 183 184 185 186 187 |
# File 'lib/cli/ui/stdout_router.rb', line 182 def with_enabled enable yield ensure disable end |
.with_id(on_streams:) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/cli/ui/stdout_router.rb', line 154 def with_id(on_streams:) unless on_streams.is_a?(Array) && on_streams.all? { |s| s.respond_to?(:write) } raise ArgumentError, <<~EOF on_streams must be an array of objects that respond to `write` These do not respond to write #{on_streams.reject { |s| s.respond_to?(:write) }.map.with_index { |s| s.class.to_s }.join("\n")} EOF end require 'securerandom' id = format('%05d', rand(10**5)) Thread.current[:cliui_output_id] = { id: id, streams: on_streams, } yield(id) ensure Thread.current[:cliui_output_id] = nil end |