Class: TextDoc

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/buzzcore/text_doc.rb', line 4

def height
  @height
end

#linesObject (readonly)

Returns the value of attribute lines.



4
5
6
# File 'lib/buzzcore/text_doc.rb', line 4

def lines
  @lines
end

#widthObject (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 centre_bar(aChar = '-', indent = 6)
  (' '*indent) + aChar*(@width-(indent*2)) + (' '*indent)
end

#loggerObject



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_sObject



66
67
68
# File 'lib/buzzcore/text_doc.rb', line 66

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