Module: CLI::UI::StdoutRouter

Extended by:
T::Sig
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

Methods included from T::Sig

sig

Class Attribute Details

.duplicate_output_toObject

Returns the value of attribute duplicate_output_to.



325
326
327
# File 'lib/cli/ui/stdout_router.rb', line 325

def duplicate_output_to
  @duplicate_output_to
end

Class Method Details

.assert_enabled!Object

Raises:



350
351
352
# File 'lib/cli/ui/stdout_router.rb', line 350

def assert_enabled!
  raise NotEnabled unless enabled?
end

.current_idObject



345
346
347
# File 'lib/cli/ui/stdout_router.rb', line 345

def current_id
  Thread.current[:cliui_output_id]
end

.disableObject



383
384
385
386
387
388
389
# File 'lib/cli/ui/stdout_router.rb', line 383

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

  deactivate($stdout)
  deactivate($stderr)
  true
end

.enableObject



369
370
371
372
373
374
375
# File 'lib/cli/ui/stdout_router.rb', line 369

def enable
  return false if enabled?($stdout) || enabled?($stderr)

  activate($stdout, :stdout)
  activate($stderr, :stderr)
  true
end

.enabled?(stream = $stdout) ⇒ Boolean

Returns:

  • (Boolean)


378
379
380
# File 'lib/cli/ui/stdout_router.rb', line 378

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

.ensure_activatedObject



364
365
366
# File 'lib/cli/ui/stdout_router.rb', line 364

def ensure_activated
  enable unless enabled?
end

.with_enabled(&block) ⇒ Object



355
356
357
358
359
360
# File 'lib/cli/ui/stdout_router.rb', line 355

def with_enabled(&block)
  enable
  yield
ensure
  disable
end

.with_id(on_streams:, &block) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
# File 'lib/cli/ui/stdout_router.rb', line 332

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