Class: TextDoc
- Inherits:
-
Object
- Object
- TextDoc
- Defined in:
- lib/buzzcore/text_doc.rb
Overview
represents a mono-spaced text document with a given width and expandable height.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #add_block(aLines, aCol = 0) ⇒ Object
- #add_line(aLine = nil, aCol = 0) ⇒ Object
- #centre_bar(aChar = '-', indent = 6) ⇒ Object
-
#initialize(aWidth = 80, aHeight = 66) ⇒ TextDoc
constructor
A new instance of TextDoc.
- #logger ⇒ Object
- #replace(aCol, aLine, aString) ⇒ Object
- #replace_block(aCol, aLine, aLines) ⇒ Object
- #replace_string(aString, aCol, aSubString) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(aWidth = 80, aHeight = 66) ⇒ TextDoc
Returns a new instance of TextDoc.
10 11 12 13 14 15 16 17 |
# File 'lib/buzzcore/text_doc.rb', line 10 def initialize(aWidth=80,aHeight=66) @width = aWidth @height = aHeight @lines = Array.new(@height) line_str = ' '*@width @lines.collect!{|line| line_str.clone } end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
4 5 6 |
# File 'lib/buzzcore/text_doc.rb', line 4 def height @height end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
4 5 6 |
# File 'lib/buzzcore/text_doc.rb', line 4 def lines @lines end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
4 5 6 |
# File 'lib/buzzcore/text_doc.rb', line 4 def width @width end |
Instance Method Details
#add_block(aLines, aCol = 0) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/buzzcore/text_doc.rb', line 47 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 |
#add_line(aLine = nil, aCol = 0) ⇒ Object
56 57 58 59 60 |
# File 'lib/buzzcore/text_doc.rb', line 56 def add_line(aLine=nil,aCol=0) @lines << ' '*@width and return if !aLine @lines << ' '*@width replace(aCol,@lines.length-1,aLine) end |
#centre_bar(aChar = '-', indent = 6) ⇒ Object
62 63 64 |
# File 'lib/buzzcore/text_doc.rb', line 62 def (aChar = '-', indent = 6) (' '*indent) + aChar*(@width-(indent*2)) + (' '*indent) end |
#logger ⇒ Object
6 7 8 |
# File 'lib/buzzcore/text_doc.rb', line 6 def logger RAILS_DEFAULT_LOGGER end |
#replace(aCol, aLine, aString) ⇒ Object
33 34 35 36 |
# File 'lib/buzzcore/text_doc.rb', line 33 def replace(aCol,aLine,aString) return if (aLine < 0) || (aLine>=@lines.length) replace_string(@lines[aLine],aCol,aString) end |
#replace_block(aCol, aLine, aLines) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/buzzcore/text_doc.rb', line 38 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 |
#replace_string(aString, aCol, aSubString) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/buzzcore/text_doc.rb', line 19 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 |
#to_s ⇒ Object
66 67 68 |
# File 'lib/buzzcore/text_doc.rb', line 66 def to_s return @lines.join("\n") end |