class Fabricator::Vertical_Peeker::Text_Wrapper

Public Class Methods

new(port = $stdout, width: 80, pseudographics: UNICODE_PSEUDOGRAPHICS, palette: DEFAULT_PALETTE) click to toggle source
Calls superclass method
# File lib/mau/fabricator.rb, line 757
def initialize port = $stdout,
    width: 80,
    pseudographics: UNICODE_PSEUDOGRAPHICS,
    palette: DEFAULT_PALETTE
  super()
  @port = port
  @width = width
  @pseudographics = pseudographics
  @palette = palette
  @hangindent = 0
  @curpos = 0
  @curspace = nil
  @curword = OpenStruct.new(
    prepared_output: '',
    width: 0)
  @curmode = @palette.null
  return
end

Public Instance Methods

add_node(node) click to toggle source
# File lib/mau/fabricator.rb, line 815
def add_node node
  case node.type
  when :plain then
    add_plain node.data
  when :space then
    add_space node.data || ' '
  when :nbsp then
    add_plain ' '
  when :monospace, :bold, :italic, :underscore then
    styled node.type do
      add_nodes node.content
    end
  when :mention_chunk then
    add_pseudographics :before_chunk_name
    add_nodes(
        Fabricator.parse_markup(node.name,
            Fabricator::MF::LINK))
    add_pseudographics :after_chunk_name
  when :link then
    if node.implicit_face then
      styled :link do
        add_plain '<'
        add_nodes node.content
        add_plain '>'
      end
    else
      add_plain '<'
      add_nodes node.content
      unless node.implicit_face then
        add_space ' '
        styled :link do
          add_plain node.target
        end
      end
      add_plain '>'
    end
  else
    # Uh-oh, a bug: the parser generated a node of a type
    # unknown to the weaver.
    raise 'invalid node type'
  end
  return
end
add_nodes(nodes) click to toggle source
# File lib/mau/fabricator.rb, line 859
def add_nodes nodes
  nodes.each do |node|
    add_node node
  end
  return
end
add_plain(data) click to toggle source
# File lib/mau/fabricator.rb, line 776
def add_plain data
  if @curspace and @curpos + data.length > @width then
    # the space becomes a linebreak
    @port.puts @palette.null
    @port.print ' ' * @hangindent + @curmode
    @curspace = nil
    @curpos = @hangindent + @curword.width
  end
  @curword.prepared_output << data
  @curpos += data.length
  return
end
add_pseudographics(name) click to toggle source
# File lib/mau/fabricator.rb, line 898
def add_pseudographics name
  seq = @pseudographics[name]
  raise 'unknown pseudographics item' unless seq
  add_plain seq
  return
end
add_space(data = ' ') click to toggle source
# File lib/mau/fabricator.rb, line 789
def add_space data = ' '
  @port.print @curspace.prepared_output if @curspace
  @port.print @curword.prepared_output
  @curspace = OpenStruct.new(
    prepared_output: data,
    width: data.length)
  @curword = OpenStruct.new(
    prepared_output: '',
    width: 0)
  @curpos += data.length
  return
end
hang() { || ... } click to toggle source
# File lib/mau/fabricator.rb, line 866
def hang
  # convert the preceding whitespace, if any, into 'hard'
  # space not subject to future wrapping
  if @curspace then
    @port.print @curspace.prepared_output
    @curspace = nil
  end
  prev_hangindent = @hangindent
  begin
    @hangindent = @curpos
    yield
  ensure
    @hangindent = prev_hangindent
  end
  return
end
linebreak() click to toggle source
# File lib/mau/fabricator.rb, line 802
def linebreak
  @port.print @curspace.prepared_output if @curspace
  @port.print @curword.prepared_output
  @port.puts @palette.null
  @port.print ' ' * @hangindent + @curmode
  @curspace = nil
  @curword = OpenStruct.new(
    prepared_output: '',
    width: 0)
  @curpos = @hangindent
  return
end
styled(sequence_name) { || ... } click to toggle source
# File lib/mau/fabricator.rb, line 883
def styled sequence_name
  sequence = @palette[sequence_name]
  raise 'unknown palette entry' unless sequence
  prev_mode = @curmode
  begin
    @curmode = sequence
    @curword.prepared_output << sequence
    yield
  ensure
    @curmode = prev_mode
    @curword.prepared_output << prev_mode
  end
  return
end