Class: Yummi::TextBox

Inherits:
Object
  • Object
show all
Defined in:
lib/yummi/text_box.rb

Overview

A box to decorate texts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ TextBox

Initializes a text box using the parameters to define the style

Example:

TextBox::new :align => :center, :border => {:color => :red}, :separator => {:color => :green}


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/yummi/text_box.rb', line 50

def initialize params = {}
  params = OpenStruct::new params
  params.separator ||= {}
  params.border ||= {}
  @content = []
  @style = OpenStruct::new

  @style.width = (params.width or nil)
  @style.align = (params.align or :left)

  @style.separator = {}
  @style.separator[:pattern] = (params.separator[:pattern] or '-')
  @style.separator[:width] = (params.separator[:width] or nil)
  @style.separator[:color] = (params.separator[:color] or "bold.black")
  @style.separator[:align] = (params.separator[:align] or :left)

  @style.border = {}
  @style.border[:color] = (params.border[:color] or "bold.black")
  @style.border[:top] = (params.border[:top] or '-')
  @style.border[:bottom] = (params.border[:bottom] or '-')
  @style.border[:left] = (params.border[:left] or '|')
  @style.border[:right] = (params.border[:right] or '|')
  @style.border[:top_left] = (params.border[:top_left] or '+')
  @style.border[:top_right] = (params.border[:top_right] or '+')
  @style.border[:bottom_left] = (params.border[:bottom_left] or '+')
  @style.border[:bottom_right] = (params.border[:bottom_right] or '+')
end

Instance Attribute Details

#contentObject

The box content



30
31
32
# File 'lib/yummi/text_box.rb', line 30

def content
  @content
end

#styleObject (readonly)

Holds the style information for this box. Supported properties are:

width: the box width (default: bold.black) align: the default alignment to use (default: left) separator: the style for separators, defined as a hash

color: separator color (default: bold.black)
pattern: separator pattern (default: '-')
width: separator width (default: box width)


41
42
43
# File 'lib/yummi/text_box.rb', line 41

def style
  @style
end

Instance Method Details

#<<(object) ⇒ Object

Adds the given object as it



128
129
130
131
132
133
# File 'lib/yummi/text_box.rb', line 128

def << (object)
  text = object.to_s
  text.each_line do |line|
    add line
  end
end

#add(obj, params = {}) ⇒ Object

Adds a line text to this box

Args

obj

The obj to add (will be converted to string).

params

A hash of parameters. Currently supported are:

color: the text color (see #Yummi#COLORS)
width: the text maximum width. Set this to break the lines automatically.
       If the #width is set, this will override the box width for this lines.
raw:   if true, the entire text will be used as one word to align the text.
align: the text alignment (see #Yummi#Aligner)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/yummi/text_box.rb', line 97

def add (obj, params = {})
  text = obj.to_s
  params = {
    :width => style.width,
    :align => style.align
  }.merge! params
  if params[:width]
    width = params[:width]
    words = text.gsub($/, ' ').split(' ') unless params[:raw]
    words ||= [text]
    buff = ''
    words.each do |word|
      # go to next line if the current word blows up the width limit
      if buff.size + word.size >= width and not buff.empty?
        _add_ buff, params
        buff = ''
      end
      buff << ' ' unless buff.empty?
      buff << word
    end
    unless buff.empty?
      _add_ buff, params
    end
  else
    text.each_line do |line|
      _add_ line, params
    end
  end
end

#line_breakObject

Adds a line break to the text.



158
159
160
# File 'lib/yummi/text_box.rb', line 158

def line_break
  @content << $/
end

#no_borderObject



78
79
80
# File 'lib/yummi/text_box.rb', line 78

def no_border
  @style.border = nil
end

Prints the #to_s into the given object.



163
164
165
# File 'lib/yummi/text_box.rb', line 163

def print (to = $stdout)
  to.print to_s
end

#separator(params = {}) ⇒ Object

Adds a line separator.

Args

params

A hash of parameters. Currently supported are:

pattern: the pattern to build the line
color: the separator color (see #Yummi#COLORS)
width: the separator width (#self#width will be used if unset)
align: the separator alignment (see #Yummi#Aligner)

Raises:

  • (Exception)


147
148
149
150
151
152
153
154
155
# File 'lib/yummi/text_box.rb', line 147

def separator (params = {})
  params = style.separator.merge params
  params[:width] ||= style.width
  raise Exception::new("Define a width for using separators") unless params[:width]
  line = fill(params[:pattern], params[:width])
  #replace the width with the box width to align the separator
  params[:width] = style.width
  add line, params
end

#to_sObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/yummi/text_box.rb', line 167

def to_s
  width = 0
  sizes = [] # the real size of each line
  content.each do |line|
    size = line.chomp.uncolored.size
    sizes << size
    width = [width, size].max
  end
  buff = ''
  i = 0
  content.each do |line|
    diff = width - sizes[i]
    buff << line.chomp << (' ' * diff) << $/
    i += 1
  end
  buff = add_border buff, width if style.border
  buff
end