class CLI::UI::StdoutRouter::Writer

Public Class Methods

new(stream, name) click to toggle source
# File lib/cli/ui/stdout_router.rb, line 12
def initialize(stream, name)
  @stream = stream
  @name = name
end

Public Instance Methods

write(*args) click to toggle source
# File lib/cli/ui/stdout_router.rb, line 17
def write(*args)
  args = args.map do |str|
    if auto_frame_inset?
      str = str.dup # unfreeze
      str = str.force_encoding(Encoding::UTF_8)
      apply_line_prefix(str, CLI::UI::Frame.prefix)
    else
      @pending_newline = false
      str
    end
  end

  # hook return of false suppresses output.
  if (hook = Thread.current[:cliui_output_hook])
    return if hook.call(args.map(&:to_s).join, @name) == false
  end

  @stream.write_without_cli_ui(*prepend_id(@stream, args))
  if (dup = StdoutRouter.duplicate_output_to)
    dup.write(*prepend_id(dup, args))
  end
end

Private Instance Methods

apply_line_prefix(str, prefix) click to toggle source
# File lib/cli/ui/stdout_router.rb, line 60
def apply_line_prefix(str, prefix)
  return '' if str.empty?
  prefixed = +''
  str.force_encoding(Encoding::UTF_8).lines.each do |line|
    if @pending_newline
      prefixed << line
      @pending_newline = false
    else
      prefixed << prefix << line
    end
  end
  @pending_newline = !str.end_with?("\n")
  prefixed
end
auto_frame_inset?() click to toggle source
# File lib/cli/ui/stdout_router.rb, line 56
def auto_frame_inset?
  !Thread.current[:no_cliui_frame_inset]
end
prepend_id(stream, args) click to toggle source
# File lib/cli/ui/stdout_router.rb, line 42
def prepend_id(stream, args)
  return args unless prepend_id_for_stream(stream)
  args.map do |a|
    next a if a.chomp.empty? # allow new lines to be new lines
    "[#{Thread.current[:cliui_output_id][:id]}] #{a}"
  end
end
prepend_id_for_stream(stream) click to toggle source
# File lib/cli/ui/stdout_router.rb, line 50
def prepend_id_for_stream(stream)
  return false unless Thread.current[:cliui_output_id]
  return true if Thread.current[:cliui_output_id][:streams].include?(stream)
  false
end