yummi 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,24 +34,20 @@ module Yummi
34
34
 
35
35
  @repositories[:formatters] = [Yummi::Formatters]
36
36
  @repositories[:colorizers] = [Yummi::Colorizers]
37
- @repositories[:row_based_colorizers] = [Yummi::Colorizers]
38
- @repositories[:using_row_colorizers] = [Yummi::Colorizers]
37
+ @repositories[:row_colorizers] = [Yummi::Colorizers]
39
38
  end
40
39
 
41
40
  def defaults
42
- component [:color, :colorize], :repository => :colorizers,
43
- :invoke => :colorize
41
+ component [:color, :colorize, :state, :health], :repository => :colorizers,
42
+ :invoke => :colorize
44
43
 
45
44
  component :format, :repository => :formatters,
46
45
  :invoke => :format
47
46
 
48
- component [:row_color, :colorize_row], :repository => :row_based_colorizers,
47
+ component [:row_color, :colorize_row], :repository => :row_colorizers,
49
48
  :invoke => :colorize_row,
50
49
  :row_based => true
51
50
 
52
- component [:state, :health], :repository => :using_row_colorizers,
53
- :invoke => :colorize,
54
- :using_row => true
55
51
  self
56
52
  end
57
53
 
@@ -75,11 +71,14 @@ module Yummi
75
71
  table.layout = config[:layout].to_sym if config[:layout]
76
72
 
77
73
  build_components table, config
78
- contexts = config[:contexts]
79
- if contexts
80
- contexts.each do |context_config|
81
- table.context context_config do
82
- build_components table, context_config
74
+
75
+ [:bottom, :top].each do |context|
76
+ contexts = config[context]
77
+ if contexts
78
+ contexts.each do |context_config|
79
+ table.send context, context_config do
80
+ build_components table, context_config
81
+ end
83
82
  end
84
83
  end
85
84
  end
@@ -90,13 +89,7 @@ module Yummi
90
89
  def build_components(table, config)
91
90
  components.each do |key, component_config|
92
91
  block = lambda do |params|
93
- if component_config[:using_row]
94
- table.using_row do
95
- table.send component_config[:invoke], *params
96
- end
97
- else
98
- table.send component_config[:invoke], *params
99
- end
92
+ table.send component_config[:invoke], *params
100
93
  end
101
94
  if component_config[:row_based]
102
95
  parse_row_based_component config[key], component_config, &block
@@ -20,28 +20,49 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
+ require 'ostruct'
24
+
23
25
  module Yummi
24
26
 
25
27
  # A box to decorate texts
26
28
  class TextBox
27
- # The border color
28
- attr_accessor :color
29
29
  # The box content
30
30
  attr_accessor :content
31
- # The box width (leave it null to manually manage the width)
32
- attr_accessor :width
33
- # The default alignment to use
34
- attr_accessor :default_align
35
- # The default separator parameters to use. Use the :pattern: key to set the pattern
36
- # and the keys supported in #self#separator
37
- attr_accessor :default_separator
31
+ #
32
+ # Holds the style information for this box. Supported properties are:
33
+ #
34
+ # width: the box width (default: none)
35
+ # align: the default alignment to use (default: left)
36
+ # separator: the style for separators, defined as a hash
37
+ # color: separator color (default: none)
38
+ # pattern: separator pattern (default: '-')
39
+ # width: separator width (default: box width)
40
+ #
41
+ attr_reader :style
38
42
 
39
43
  def initialize
40
44
  @color = :white
41
45
  @content = []
42
- @default_separator = {
43
- :pattern => '-'
44
- }
46
+ @style = OpenStruct::new
47
+ @style.separator = {}
48
+ @style.separator[:pattern] = '-'
49
+ @style.separator[:width] = nil
50
+ @style.separator[:align] = :left
51
+
52
+ @style.border = {}
53
+ @style.border[:color] = nil
54
+ @style.border[:top] = '-'
55
+ @style.border[:bottom] = '-'
56
+ @style.border[:left] = '|'
57
+ @style.border[:right] = '|'
58
+ @style.border[:top_left] = '+'
59
+ @style.border[:top_right] = '+'
60
+ @style.border[:bottom_left] = '+'
61
+ @style.border[:bottom_right] = '+'
62
+ end
63
+
64
+ def no_border
65
+ @style.border = nil
45
66
  end
46
67
 
47
68
  #
@@ -60,8 +81,8 @@ module Yummi
60
81
  #
61
82
  def add (text, params = {})
62
83
  params = {
63
- :width => @width,
64
- :align => @default_align
84
+ :width => style.width,
85
+ :align => style.align
65
86
  }.merge! params
66
87
  if params[:width]
67
88
  width = params[:width]
@@ -99,26 +120,21 @@ module Yummi
99
120
  #
100
121
  # === Args
101
122
  #
102
- # +pattern+::
103
- # The pattern to build the line
104
123
  # +params+::
105
124
  # A hash of parameters. Currently supported are:
125
+ # pattern: the pattern to build the line
106
126
  # color: the separator color (see #Yummi#COLORS)
107
127
  # width: the separator width (#self#width will be used if unset)
108
128
  # align: the separator alignment (see #Yummi#Aligner)
109
129
  #
110
- def separator (pattern = @default_separator[:pattern], params = {})
111
- unless pattern.is_a? String
112
- params = pattern
113
- pattern = @default_separator[:pattern]
114
- end
115
- params = @default_separator.merge params
116
- width = (params[:width] or @width)
117
- if @width and width < @width
118
- params[:width] = @width
119
- end
120
- line = pattern * width
121
- add line[0...width], params
130
+ def separator (params = {})
131
+ params = style.separator.merge! params
132
+ params[:width] ||= style.width
133
+ raise Exception::new("Define a width for using separators") unless params[:width]
134
+ line = fill(params[:pattern], params[:width])
135
+ #replace the width with the box width to align the separator
136
+ params[:width] = style.width
137
+ add line, params
122
138
  end
123
139
 
124
140
  # Adds a line break to the text.
@@ -133,33 +149,66 @@ module Yummi
133
149
 
134
150
  def to_s
135
151
  width = 0
136
- sizes = []
152
+ sizes = [] # the real size of each line
137
153
  content.each do |line|
138
154
  size = (Yummi::Color::raw line.chomp).size
139
155
  sizes << size
140
156
  width = [width, size].max
141
157
  end
142
- border = Yummi.colorize('+' + ('-' * width) + '+', color) + $/
143
- pipe = Yummi.colorize '|', color
144
158
  buff = ''
145
- buff << border
146
159
  i = 0
147
160
  content.each do |line|
148
161
  diff = width - sizes[i]
149
- buff << pipe << line.chomp << (' ' * diff) << pipe << $/
162
+ buff << line.chomp << (' ' * diff) << $/
150
163
  i += 1
151
164
  end
152
- buff << border
165
+ buff = add_border buff, width if style.border
153
166
  buff
154
167
  end
155
168
 
156
169
  private
157
170
 
171
+ def add_border (text, width)
172
+ border = style.border
173
+ color = border[:color]
174
+ left = fill(border[:left], content.size).split(//).collect do |c|
175
+ Yummi.colorize(c, color)
176
+ end
177
+ right = fill(border[:right], content.size).split(//).collect do |c|
178
+ Yummi.colorize(c, color)
179
+ end
180
+ result = Yummi.colorize(
181
+ border[:top_left] + fill(border[:top], width) + border[:top_right],
182
+ color
183
+ ) + $/
184
+ text.each_line do |line|
185
+ result << left.shift.to_s
186
+ result << line.chomp
187
+ result << right.shift.to_s
188
+ result << $/
189
+ end
190
+ result << Yummi.colorize(
191
+ border[:bottom_left] + fill(border[:bottom], width) + border[:bottom_right],
192
+ color
193
+ )
194
+ result << $/
195
+ end
196
+
158
197
  def _add_ (text, params)
159
198
  if params[:align] and params[:width]
160
199
  text = Yummi::Aligner.align params[:align], text, params[:width]
161
200
  end
162
- @content << Yummi.colorize(text, params[:color])
201
+ @content << Yummi.colorize(truncate(text), params[:color])
202
+ end
203
+
204
+ def truncate (text, width = style.width)
205
+ return text[0...width] if width
206
+ text
207
+ end
208
+
209
+ def fill (text, width = style.width)
210
+ width = [width, style.width].min if style.width
211
+ truncate text * width, width
163
212
  end
164
213
 
165
214
  end
data/lib/yummi/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Yummi
24
- VERSION = "0.5.2"
24
+ VERSION = "0.6.0"
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yummi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,13 +9,13 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-25 00:00:00.000000000 Z
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A tool to colorize your console application.
15
15
  email:
16
16
  - ataxexe@gmail.com
17
17
  executables:
18
- - colorize
18
+ - yummi
19
19
  extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
@@ -25,7 +25,7 @@ files:
25
25
  - LICENSE
26
26
  - README.md
27
27
  - Rakefile
28
- - bin/colorize
28
+ - bin/yummi
29
29
  - examples/cash_flow_table.rb
30
30
  - examples/cash_flow_table.yaml
31
31
  - examples/cash_flow_table_data.csv