class Fabricator::Vertical_Peeker

Constants

ASCII_PSEUDOGRAPHICS
DEFAULT_PALETTE
MARKUP2HTML
POSTPROCESSES
UNICODE_PSEUDOGRAPHICS
WINDOWS_HOSTED_P

Public Class Methods

new(port) click to toggle source
Calls superclass method
# File lib/mau/fabricator.rb, line 42
def initialize port
  super()
  @port = port
  if @port.respond_to? :path then
    @filename = @port.path
  elsif @port == $stdin then
    @filename = '(stdin)'
  else
    @filename = '(unknown)'
  end
  @buffer = []
  @line_number = 1 # number of the first line in the buffer
  @eof_seen = false
  return
end

Public Instance Methods

eof?() click to toggle source
# File lib/mau/fabricator.rb, line 80
def eof?
  return peek_line.nil?
end
get_indented_lines_with_skip() click to toggle source
# File lib/mau/fabricator.rb, line 25
  def get_indented_lines_with_skip
    indent = nil; lines = []
    while peek_line =~ /^\s+/ or
        (peek_line == '' and
         !lines.empty? and
         peek_line(1) =~ /^\s+/) do
      # If the line ahead is not indented but we passed the
      # test, then [[get_line]] will return [[""]] and [[$&]]
      # is the __following__ line's indentation.
      indent = $&.length if indent.nil? or $&.length < indent
      lines.push get_line
    end
    return nil if lines.empty?
    lines.each{|l| l[0 ... indent] = ''}
    return OpenStruct.new(lines: lines, indent: indent)
  end

  def initialize port
    super()
    @port = port
    if @port.respond_to? :path then
      @filename = @port.path
    elsif @port == $stdin then
      @filename = '(stdin)'
    else
      @filename = '(unknown)'
    end
    @buffer = []
    @line_number = 1 # number of the first line in the buffer
    @eof_seen = false
    return
  end

  def peek_line ahead = 0
    raise 'invalid argument' unless ahead >= 0
    until @buffer.length > ahead or @eof_seen do
      line = @port.gets
      if line then
        line.rstrip!
        @buffer.push line
      else
        @eof_seen = true
      end
    end
    return @buffer[ahead] # nil if past eof
  end

  def get_line
    # ensure that if a line is available, it's in [[@buffer]]
    peek_line

    @line_number += 1 unless @buffer.empty?
    return @buffer.shift
  end

  def eof?
    return peek_line.nil?
  end

  def lineno_ahead
    return @line_number + (@line_consumed ? 1 : 0)
  end

  def location_ahead
    return OpenStruct.new(
      filename: @filename, line: lineno_ahead)
  end
end
get_line() click to toggle source
# File lib/mau/fabricator.rb, line 72
def get_line
  # ensure that if a line is available, it's in [[@buffer]]
  peek_line

  @line_number += 1 unless @buffer.empty?
  return @buffer.shift
end
lineno_ahead() click to toggle source
# File lib/mau/fabricator.rb, line 84
def lineno_ahead
  return @line_number + (@line_consumed ? 1 : 0)
end
location_ahead() click to toggle source
# File lib/mau/fabricator.rb, line 88
def location_ahead
  return OpenStruct.new(
    filename: @filename, line: lineno_ahead)
end
peek_line(ahead = 0) click to toggle source
# File lib/mau/fabricator.rb, line 58
def peek_line ahead = 0
  raise 'invalid argument' unless ahead >= 0
  until @buffer.length > ahead or @eof_seen do
    line = @port.gets
    if line then
      line.rstrip!
      @buffer.push line
    else
      @eof_seen = true
    end
  end
  return @buffer[ahead] # nil if past eof
end