Module: CLI::UI::StdoutRouter

Defined in:
lib/cli/ui/stdout_router.rb

Defined Under Namespace

Classes: Capture, Writer

Constant Summary collapse

WRITE_WITHOUT_CLI_UI =
:write_without_cli_ui
NotEnabled =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.duplicate_output_toObject

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

Raises:



173
174
175
# File 'lib/cli/ui/stdout_router.rb', line 173

def assert_enabled!
  raise NotEnabled unless enabled?
end

.current_idObject



169
170
171
# File 'lib/cli/ui/stdout_router.rb', line 169

def current_id
  Thread.current[:cliui_output_id]
end

.disableObject



200
201
202
203
204
205
# File 'lib/cli/ui/stdout_router.rb', line 200

def disable
  return false unless enabled?($stdout) && enabled?($stderr)
  deactivate($stdout)
  deactivate($stderr)
  true
end

.enableObject



189
190
191
192
193
194
# File 'lib/cli/ui/stdout_router.rb', line 189

def enable
  return false if enabled?($stdout) || enabled?($stderr)
  activate($stdout, :stdout)
  activate($stderr, :stderr)
  true
end

.enabled?(stream = $stdout) ⇒ Boolean



196
197
198
# File 'lib/cli/ui/stdout_router.rb', line 196

def enabled?(stream = $stdout)
  stream.respond_to?(WRITE_WITHOUT_CLI_UI)
end

.ensure_activatedObject

TODO: remove this



185
186
187
# File 'lib/cli/ui/stdout_router.rb', line 185

def ensure_activated
  enable unless enabled?
end

.with_enabledObject



177
178
179
180
181
182
# File 'lib/cli/ui/stdout_router.rb', line 177

def with_enabled
  enable
  yield
ensure
  disable
end

.with_id(on_streams:) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cli/ui/stdout_router.rb', line 149

def with_id(on_streams:)
  unless on_streams.is_a?(Array) && on_streams.all? { |s| s.respond_to?(:write) }
    raise ArgumentError, "    on_streams must be an array of objects that respond to `write`\n    These do not respond to write\n    \#{on_streams.reject { |s| s.respond_to?(:write) }.map.with_index { |s| s.class.to_s }.join(\"\\n\")}\n    EOF\n  end\n\n  require 'securerandom'\n  id = format(\"%05d\", rand(10**5))\n  Thread.current[:cliui_output_id] = {\n    id: id,\n    streams: on_streams\n  }\n  yield(id)\nensure\n  Thread.current[:cliui_output_id] = nil\nend\n"