class SuperDiff::Csi::Document

Attributes

indentation_stack[R]
parts[R]

Public Class Methods

new(&block) click to toggle source
# File lib/super_diff/csi/document.rb, line 6
def initialize(&block)
  @parts = []
  @indentation_stack = []

  if block
    evaluate_block(&block)
  end
end

Public Instance Methods

bold(*args, **opts, &block) click to toggle source
# File lib/super_diff/csi/document.rb, line 19
def bold(*args, **opts, &block)
  colorize(BoldSequence.new, *args, **opts, &block)
end
colored(*args, **opts, &block)
Alias for: colorize
colorize(*args, **opts, &block) click to toggle source
# File lib/super_diff/csi/document.rb, line 23
def colorize(*args, **opts, &block)
  contents, colors = args.partition do |arg|
    arg.is_a?(String) || arg.is_a?(self.class)
  end

  if colors[0].is_a?(Symbol)
    if colors[0] == :colorize
      raise ArgumentError, "#colorize can't call itself!"
    else
      public_send(colors[0], *contents, *colors[1..-1], **opts, &block)
    end
  elsif !block && colors.empty? && opts.empty?
    text(*contents)
  elsif block
    colorize_block(colors, opts, &block)
  elsif contents.any?
    colorize_inline(contents, colors, opts)
  else
    raise ArgumentError, "Must have something to colorize!"
  end
end
Also aliased as: colored
each(&block) click to toggle source
# File lib/super_diff/csi/document.rb, line 15
def each(&block)
  parts.each(&block)
end
indent(by:, &block) click to toggle source
# File lib/super_diff/csi/document.rb, line 83
def indent(by:, &block)
  # TODO: This won't work if using `text` manually to add lines
  indentation_stack << (by.is_a?(String) ? by : " " * by)
  evaluate_block(&block)
  indentation_stack.pop
end
line(*contents, indent_by: 0, &block) click to toggle source
# File lib/super_diff/csi/document.rb, line 61
def line(*contents, indent_by: 0, &block)
  indent(by: indent_by) do
    add_part(indentation_stack.join)

    if block
      evaluate_block(&block)
    elsif contents.any?
      text(*contents)
    else
      raise ArgumentError.new(
        "Must have something to add to the document!",
      )
    end
  end

  add_part("\n")
end
method_missing(name, *args, **opts, &block) click to toggle source
Calls superclass method
# File lib/super_diff/csi/document.rb, line 90
def method_missing(name, *args, **opts, &block)
  request = derive_request_from(name)

  if request
    request.resolve(self, args, opts, &block)
  else
    super
  end
end
newline() click to toggle source
# File lib/super_diff/csi/document.rb, line 79
def newline
  add_part("\n")
end
plain(*contents, **, &block)
Alias for: text
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/super_diff/csi/document.rb, line 100
def respond_to_missing?(name, include_private = false)
  request = derive_request_from(name)
  !request.nil? || super
end
text(*contents, **, &block) click to toggle source
# File lib/super_diff/csi/document.rb, line 46
def text(*contents, **, &block)
  if block
    evaluate_block(&block)
  elsif contents.any?
    contents.each do |part|
      add_part(part)
    end
  else
    raise ArgumentError.new(
      "Must have something to add to the document!",
    )
  end
end
Also aliased as: plain
to_s() click to toggle source
# File lib/super_diff/csi/document.rb, line 105
def to_s
  parts.map(&:to_s).join.rstrip
end

Protected Instance Methods

add_part(part) click to toggle source
# File lib/super_diff/csi/document.rb, line 139
def add_part(part)
  parts.push(part)
end
derive_request_from(name) click to toggle source
# File lib/super_diff/csi/document.rb, line 113
def derive_request_from(name)
  match = name.to_s.match(/\A(.+)_line\Z/)

  if match
    color_name = match[1].to_sym

    if respond_to?(color_name)
      MethodRequest.new(name: color_name, line: true)
    elsif Csi::Color.exists?(color_name)
      ColorRequest.new(name: color_name, line: true)
    end
  elsif Csi::Color.exists?(name.to_sym)
    ColorRequest.new(name: name.to_sym, line: false)
  else
    nil
  end
end
evaluate_block(&block) click to toggle source
# File lib/super_diff/csi/document.rb, line 131
def evaluate_block(&block)
  if block.arity > 0
    block.call(self)
  else
    instance_eval(&block)
  end
end