yummi 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -75,13 +75,16 @@ opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
75
75
  end
76
76
  opt.on '--layout LAYOUT', 'Defines the table layout (horizontal or vertical)' do |layout|
77
77
  case layout
78
- when 'horizonta'
78
+ when 'horizontal'
79
79
  @table.layout = :horizontal
80
80
  when 'vertical'
81
81
  @table.layout = :vertical
82
82
  else
83
83
  end
84
84
  end
85
+ opt.on '--box', 'Prints the table inside a box' do
86
+ @box = Yummi::TextBox::new
87
+ end
85
88
  opt.on '--help', 'Prints this message' do
86
89
  puts opt
87
90
  exit 0
@@ -89,4 +92,9 @@ end
89
92
 
90
93
  opt.parse ARGV
91
94
 
92
- @table.print
95
+ if @box
96
+ @box << @table
97
+ @box.print
98
+ else
99
+ @table.print
100
+ end
@@ -0,0 +1,57 @@
1
+ # The MIT License
2
+ #
3
+ # Copyright (c) 2012 Marcelo Guimarães <ataxexe@gmail.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'optparse'
24
+ require_relative '../lib/yummi'
25
+
26
+ @box = Yummi::TextBox.new
27
+
28
+ @box.width = 70
29
+ @box.default_align = :justify
30
+ @box.default_separator[:color] = :brown
31
+
32
+ opt = OptionParser::new
33
+
34
+ opt.on '--align ALIGN', 'Sets the default alignment to use' do |align|
35
+ @box.default_align = align.to_sym
36
+ end
37
+ opt.on '--width WIDTH', Integer, 'Sets the text box width' do |width|
38
+ @box.width = width
39
+ end
40
+ opt.on '--help', 'Prints this message' do
41
+ puts opt
42
+ exit 0
43
+ end
44
+
45
+ opt.parse! ARGV
46
+
47
+ @box.add 'The MIT License', :color => :yellow, :align => :center
48
+ @box.line_break
49
+ @box.add 'Copyright (c) 2012 Marcelo Guimaraes <ataxexe@gmail.com>', :color => :green, :align => :center
50
+ @box.separator
51
+ @box.add 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:'
52
+ @box.line_break
53
+ @box.add 'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.'
54
+ @box.separator :width => @box.width / 3, :align => :center
55
+ @box.add 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.',
56
+ :color => :red
57
+ @box.print
@@ -74,6 +74,9 @@ opt.on '--layout LAYOUT', 'Defines the table layout (horizontal or vertical)' do
74
74
  else
75
75
  end
76
76
  end
77
+ opt.on '--box', 'Prints the table inside a box' do
78
+ @box = Yummi::TextBox::new
79
+ end
77
80
  opt.on '--help', 'Prints this message' do
78
81
  puts opt
79
82
  exit 0
@@ -93,4 +96,9 @@ opt.parse ARGV
93
96
  ['Server 6', 1_000_000, 5_000, 200, 180],
94
97
  ]
95
98
 
96
- @table.print
99
+ if @box
100
+ @box << @table
101
+ @box.print
102
+ else
103
+ @table.print
104
+ end
data/lib/yummi.rb CHANGED
@@ -178,6 +178,7 @@ module Yummi
178
178
 
179
179
  # Aligns the text to the center
180
180
  def self.center text, width
181
+ return text if text.size >= width
181
182
  size = width - text.size
182
183
  left_size = size / 2
183
184
  right_size = size - left_size
@@ -187,8 +188,9 @@ module Yummi
187
188
  # Aligns the text to both sides
188
189
  def self.justify text, width
189
190
  extra_spaces = width - text.size
190
- return text if extra_spaces < 0
191
+ return text unless extra_spaces > 0
191
192
  words = text.split ' '
193
+ return text if words.size == 1
192
194
  return text if extra_spaces / (words.size - 1) > 2
193
195
  until extra_spaces == 0
194
196
  words.each_index do |i|
@@ -28,18 +28,20 @@ module Yummi
28
28
  attr_accessor :color
29
29
  # The box content
30
30
  attr_accessor :content
31
- # The box maximum width
32
- attr_accessor :max_width
31
+ # The box width (leave it null to manually manage the width)
32
+ attr_accessor :width
33
33
  # The default alignment to use
34
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
35
38
 
36
- def initialize params = {}
37
- params = {
38
- :color => :white,
39
- :content => ''
40
- }.merge! params
41
- @color = params[:color]
39
+ def initialize
40
+ @color = :white
42
41
  @content = []
42
+ @default_separator = {
43
+ :pattern => '-'
44
+ }
43
45
  end
44
46
 
45
47
  #
@@ -53,10 +55,12 @@ module Yummi
53
55
  # A hash of parameters. Currently supported are:
54
56
  # color: the text color (see #Yummi#COLORS)
55
57
  # width: the text maximum width. Set this to break the lines automatically.
58
+ # If the #width is set, this will override the box width for this lines.
59
+ # align: the text alignment (see #Yummi#Aligner)
56
60
  #
57
61
  def add text, params = {}
58
62
  params = {
59
- :width => @max_width,
63
+ :width => @width,
60
64
  :align => @default_align
61
65
  }.merge! params
62
66
  if params[:width]
@@ -65,7 +69,7 @@ module Yummi
65
69
  buff = ''
66
70
  words.each do |word|
67
71
  # go to next line if the current word blows up the width limit
68
- if buff.size + word.size >= width
72
+ if buff.size + word.size >= width and not buff.empty?
69
73
  _add_ buff, params
70
74
  buff = ''
71
75
  end
@@ -82,16 +86,47 @@ module Yummi
82
86
  end
83
87
  end
84
88
 
85
- alias_method :<<, :add
89
+ # Adds the given object as it
90
+ def << object
91
+ text = object.to_s
92
+ text.each_line do |line|
93
+ add line
94
+ end
95
+ end
96
+
97
+ #
98
+ # Adds a line separator.
99
+ #
100
+ # === Args
101
+ #
102
+ # +pattern+::
103
+ # The pattern to build the line
104
+ # +params+::
105
+ # A hash of parameters. Currently supported are:
106
+ # color: the separator color (see #Yummi#COLORS)
107
+ # width: the separator width (#self#width will be used if unset)
108
+ # align: the separator alignment (see #Yummi#Aligner)
109
+ #
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
122
+ end
86
123
 
87
124
  # Adds a line break to the text.
88
125
  def line_break
89
126
  @content << $/
90
127
  end
91
128
 
92
- #
93
129
  # Prints the #to_s into the given object.
94
- #
95
130
  def print to = $stdout
96
131
  to.print to_s
97
132
  end
data/lib/yummi/version.rb CHANGED
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module Yummi
24
- VERSION = "0.2.1"
24
+ VERSION = "0.2.2"
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.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-14 00:00:00.000000000Z
12
+ date: 2012-05-16 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A tool to colorize your console application.
15
15
  email:
@@ -24,11 +24,11 @@ files:
24
24
  - LICENSE
25
25
  - README.md
26
26
  - Rakefile
27
- - examples/box_license.rb
27
+ - examples/cash_flow_table.rb
28
+ - examples/license_box.rb
28
29
  - examples/list_files.rb
29
30
  - examples/logger.rb
30
31
  - examples/monitor_table.rb
31
- - examples/table_cash_flow.rb
32
32
  - lib/yummi.rb
33
33
  - lib/yummi/logger.rb
34
34
  - lib/yummi/no_colors.rb
@@ -1,40 +0,0 @@
1
- # The MIT License
2
- #
3
- # Copyright (c) 2012 Marcelo Guimarães <ataxexe@gmail.com>
4
- #
5
- # Permission is hereby granted, free of charge, to any person obtaining a copy
6
- # of this software and associated documentation files (the "Software"), to deal
7
- # in the Software without restriction, including without limitation the rights
8
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- # copies of the Software, and to permit persons to whom the Software is
10
- # furnished to do so, subject to the following conditions:
11
- #
12
- # The above copyright notice and this permission notice shall be included in
13
- # all copies or substantial portions of the Software.
14
- #
15
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- # THE SOFTWARE.
22
-
23
- require_relative '../lib/yummi'
24
-
25
- box = Yummi::TextBox.new
26
-
27
- box.max_width = 70
28
- box.default_align = :justify
29
-
30
- box.add 'The MIT License', :color => :yellow, :align => :center
31
- box.line_break
32
- box.add 'Copyright (c) 2012 Marcelo Guimaraes <ataxexe@gmail.com>', :color => :green, :align => :center
33
- box.line_break
34
- box.add 'Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:'
35
- box.line_break
36
- box.add 'The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.'
37
- box.line_break
38
- box.add 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.',
39
- :color => :red
40
- box.print