class TestBench::Output::Writer

Constants

Error

Attributes

byte_offset[W]
device[W]
indentation_depth[W]
mode[W]
styling[RW]
styling_enabled[W]

Public Class Methods

assure_styling_setting(styling_setting) click to toggle source
# File lib/test_bench/output/writer.rb, line 147
def self.assure_styling_setting(styling_setting)
  unless styling_settings.include?(styling_setting)
    raise Error, "Invalid output styling #{styling_setting.inspect} (Valid values: #{styling_settings.map(&:inspect).join(', ')})"
  end
end
build(device=nil, styling: nil) click to toggle source
# File lib/test_bench/output/writer.rb, line 47
def self.build(device=nil, styling: nil)
  instance = new
  instance.configure(device: device, styling: styling)
  instance
end
configure(receiver, writer: nil, styling: nil, device: nil, attr_name: nil) click to toggle source
# File lib/test_bench/output/writer.rb, line 53
def self.configure(receiver, writer: nil, styling: nil, device: nil, attr_name: nil)
  attr_name ||= :writer

  if writer.nil?
    instance = build(device, styling: styling)
  else
    instance = writer
  end

  receiver.public_send(:"#{attr_name}=", instance)

  instance
end
default_styling_setting() click to toggle source
# File lib/test_bench/output/writer.rb, line 161
def self.default_styling_setting
  styling_settings.fetch(0)
end
styling?(device, styling_setting=nil) click to toggle source
# File lib/test_bench/output/writer.rb, line 132
def self.styling?(device, styling_setting=nil)
  styling_setting ||= Defaults.styling

  assure_styling_setting(styling_setting)

  case styling_setting
  when :detect
    device.tty?
  when :on
    true
  when :off
    false
  end
end
styling_settings() click to toggle source
# File lib/test_bench/output/writer.rb, line 153
def self.styling_settings
  [
    :detect,
    :on,
    :off
  ]
end

Public Instance Methods

byte_offset() click to toggle source
# File lib/test_bench/output/writer.rb, line 26
def byte_offset
  @byte_offset ||= 0
end
configure(styling: nil, device: nil) click to toggle source
# File lib/test_bench/output/writer.rb, line 36
def configure(styling: nil, device: nil)
  device ||= Defaults.device

  unless styling.nil?
    self.class.assure_styling_setting(styling)
  end

  self.device = device
  self.styling = styling
end
current?(byte_offset) click to toggle source
# File lib/test_bench/output/writer.rb, line 120
def current?(byte_offset)
  byte_offset >= self.byte_offset
end
decrease_indentation() click to toggle source
# File lib/test_bench/output/writer.rb, line 128
def decrease_indentation
  self.indentation_depth -= 1
end
device() click to toggle source
# File lib/test_bench/output/writer.rb, line 6
def device
  @device ||= StringIO.new
end
escape_code(id) click to toggle source
# File lib/test_bench/output/writer.rb, line 79
def escape_code(id)
  escape_code = SGR.escape_code(id)

  if mode == Mode.text
    return self unless styling?

    self.mode = Mode.escape_sequence

    write("\e[")
  else
    write(';')
  end

  write(escape_code)

  self
end
increase_indentation() click to toggle source
# File lib/test_bench/output/writer.rb, line 124
def increase_indentation
  self.indentation_depth += 1
end
indent() click to toggle source
# File lib/test_bench/output/writer.rb, line 101
def indent
  indentation = '  ' * indentation_depth

  text(indentation)
end
indentation_depth() click to toggle source
# File lib/test_bench/output/writer.rb, line 31
def indentation_depth
  @indentation_depth ||= 0
end
mode() click to toggle source
# File lib/test_bench/output/writer.rb, line 21
def mode
  @mode ||= Mode.text
end
newline() click to toggle source
# File lib/test_bench/output/writer.rb, line 107
def newline
  sync
  device.puts('')
  self.byte_offset += 1
  self
end
styling?()
Alias for: styling_enabled
styling_enabled() click to toggle source
# File lib/test_bench/output/writer.rb, line 13
def styling_enabled
  instance_variable_defined?(:@styling_enabled) ?
    @styling_enabled :
    @styling_enabled = self.class.styling?(device, styling)
end
Also aliased as: styling?
sync() click to toggle source
# File lib/test_bench/output/writer.rb, line 97
def sync
  text('')
end
text(text) click to toggle source
# File lib/test_bench/output/writer.rb, line 67
def text(text)
  if mode == Mode.escape_sequence
    self.mode = Mode.text

    write('m')
  end

  write(text)

  self
end
write(data) click to toggle source
# File lib/test_bench/output/writer.rb, line 114
def write(data)
  bytes_written = device.write(data)

  self.byte_offset += bytes_written
end