Sha256: 97c2591e356e72b6ce866f7a4b3e4991579015a8356190cf47006889ec738b8e

Contents?: true

Size: 1.85 KB

Versions: 22

Compression:

Stored size: 1.85 KB

Contents

# represents a mono-spaced text document with a given width and expandable height.
class TextDoc

  attr_reader :width, :height, :lines

  def logger
    RAILS_DEFAULT_LOGGER
  end

  def initialize(aWidth=80,aHeight=66)
    @width = aWidth
    @height = aHeight
  
    @lines = Array.new(@height)
    line_str = ' '*@width
    @lines.collect!{|line| line_str.clone }
  end

  def replace_string(aString,aCol,aSubString)
    return aString if aSubString==nil || aSubString==''

    aSubString = aSubString.to_s
    start_col = aCol < 0 ? 0 : aCol
    end_col = aCol+aSubString.length-1
    end_col = @width-1 if end_col >= @width
    source_len = end_col-start_col+1
    return aString if source_len <= 0 || end_col < 0 || start_col >= @width
    aString += ' '*((end_col+1) - aString.length) if aString.length < end_col+1
    aString[start_col,source_len] = aSubString[start_col-aCol,end_col-start_col+1]
    return aString
  end

  def replace(aCol,aLine,aString)
    return if (aLine < 0) || (aLine>=@lines.length)
    replace_string(@lines[aLine],aCol,aString)
  end

  def replace_block(aCol,aLine,aLines)
    aLines = aLines.split(/\n/) if aLines.is_a?(String)
    aLines = aLines.lines if aLines.is_a?(TextDoc)
  
    aLines.each_index do |iSource|
      replace(aCol,aLine+iSource,aLines[iSource])
    end
  end

  def add_block(aLines,aCol=0)
    aLines = aLines.split(/\n/) if aLines.is_a?(String)
    aLines = aLines.lines if aLines.is_a?(TextDoc)
    aLines.each_index do |iSource|
      @lines << ' '*@width
      replace(aCol,@lines.length-1,aLines[iSource])
    end
  end

  def add_line(aLine=nil,aCol=0)
    @lines << ' '*@width and return if !aLine
    @lines << ' '*@width
    replace(aCol,@lines.length-1,aLine)
  end

  def centre_bar(aChar = '-', indent = 6)
    (' '*indent) + aChar*(@width-(indent*2)) + (' '*indent)
  end

  def to_s
    return @lines.join("\n")
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
buzzcore-0.6.6 lib/buzzcore/text_doc.rb
buzzware-buzzcore-0.2.2 lib/buzzcore/text_doc.rb
buzzware-buzzcore-0.2.3 lib/buzzcore/text_doc.rb
buzzcore-0.6.4 lib/buzzcore/text_doc.rb
buzzcore-0.6.3 lib/buzzcore/text_doc.rb
buzzcore-0.6.2 lib/buzzcore/text_doc.rb
buzzcore-0.6.1 lib/buzzcore/text_doc.rb
buzzcore-0.5.1 lib/buzzcore/text_doc.rb
buzzcore-0.5.0 lib/buzzcore/text_doc.rb
buzzcore-0.4.3 lib/buzzcore/text_doc.rb
buzzcore-0.4.2 lib/buzzcore/text_doc.rb
buzzcore-0.4.1 lib/buzzcore/text_doc.rb
buzzcore-0.4.0 lib/buzzcore/text_doc.rb
buzzcore-0.3.5 lib/buzzcore/text_doc.rb
buzzcore-0.3.4 lib/buzzcore/text_doc.rb
buzzcore-0.3.3 lib/buzzcore/text_doc.rb
buzzcore-0.3.2 lib/buzzcore/text_doc.rb
buzzcore-0.3.1 lib/buzzcore/text_doc.rb
buzzcore-0.3.0 lib/buzzcore/text_doc.rb
buzzcore-0.2.7 lib/buzzcore/text_doc.rb