yummi 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,40 @@
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
@@ -32,14 +32,14 @@ opt = OptionParser::new
32
32
  @table.header = ['Name', 'Size']
33
33
  @table.aliases << :directory
34
34
  # sets the title
35
- @table.title = 'Files in home folder'
35
+ @table.title = 'File List'
36
36
  # formats size for easily reading
37
37
  @table.format :size, :using => Yummi::Formatter.unit(:byte)
38
38
 
39
39
  opt.on '--color TYPE', 'Specify the color type (zebra,file,none)' do |type|
40
40
  case type
41
41
  when 'zebra'
42
- @table.row_colorizer Yummi::IndexedDataColorizer.zebra :intense_gray, :intense_white
42
+ @table.row_colorizer Yummi::Colorizer.stripe :intense_gray, :intense_white
43
43
  when 'file'
44
44
  @table.row_colorizer do |i, data|
45
45
  data[:directory] ? :intense_gray : :intense_white
@@ -51,7 +51,7 @@ thread_colorizer.use(:intense_cyan) { |value| value >= 0.9 }
51
51
  opt.on '--color TYPE', 'Specify the color type (zebra,row,cell,none)' do |type|
52
52
  case type
53
53
  when 'zebra'
54
- @table.row_colorizer Yummi::Colorizer.by_index.zebra :brown, :purple
54
+ @table.row_colorizer Yummi::Colorizer.stripe :brown, :purple
55
55
  when 'row'
56
56
  @table.row_colorizer memory_colorizer
57
57
  @table.row_colorizer thread_colorizer
@@ -65,14 +65,13 @@ opt.on '--color TYPE', 'Specify the color type (zebra,row,cell,none)' do |type|
65
65
  else
66
66
  end
67
67
  end
68
- opt.on '--layout LAYOUT', 'Specify the layout (horizontal or vertical)' do |layout|
68
+ opt.on '--layout LAYOUT', 'Defines the table layout (horizontal or vertical)' do |layout|
69
69
  case layout
70
- when 'horizontal'
70
+ when 'horizonta'
71
71
  @table.layout = :horizontal
72
72
  when 'vertical'
73
73
  @table.layout = :vertical
74
74
  else
75
- abort "Not recognized layout: #{layout}"
76
75
  end
77
76
  end
78
77
  opt.on '--help', 'Prints this message' do
@@ -47,7 +47,7 @@ opt = OptionParser::new
47
47
  opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
48
48
  case type
49
49
  when 'zebra'
50
- @table.row_colorizer Yummi::Colorizer.by_index.zebra :brown, :purple
50
+ @table.row_colorizer Yummi::Colorizer.stripe :brown, :purple
51
51
  when 'full'
52
52
  # colorize all values from the Description column to purple
53
53
  @table.colorize :description, :with => :purple
@@ -73,6 +73,15 @@ opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
73
73
  else
74
74
  end
75
75
  end
76
+ opt.on '--layout LAYOUT', 'Defines the table layout (horizontal or vertical)' do |layout|
77
+ case layout
78
+ when 'horizonta'
79
+ @table.layout = :horizontal
80
+ when 'vertical'
81
+ @table.layout = :vertical
82
+ else
83
+ end
84
+ end
76
85
  opt.on '--help', 'Prints this message' do
77
86
  puts opt
78
87
  exit 0
data/lib/yummi.rb CHANGED
@@ -22,6 +22,7 @@
22
22
 
23
23
  require_relative "yummi/version"
24
24
  require_relative "yummi/table"
25
+ require_relative "yummi/text_box"
25
26
  require_relative "yummi/logger"
26
27
 
27
28
  module Yummi
@@ -29,8 +30,6 @@ module Yummi
29
30
  module Color
30
31
  # Colors from default linux terminal scheme
31
32
  COLORS = {
32
- :nothing => '0;0',
33
-
34
33
  :black => '0;30',
35
34
  :red => '0;31',
36
35
  :green => '0;32',
@@ -97,16 +96,22 @@ module Yummi
97
96
  # Escape the given text with the given color code
98
97
  def self.escape key
99
98
  return key unless key
100
- color = COLORS[key]
99
+ color = (COLORS[key] or key)
101
100
  color ||= parse(key)
102
101
  "\033[#{color}m"
103
102
  end
104
103
 
105
104
  # Colorize the given text with the given color
106
105
  def self.colorize string, color
107
- col, nocol = [color, :nothing].map { |key| Color.escape(key) }
108
- col ? "#{col}#{string}#{nocol}" : string
106
+ color, end_color = [color, '0;0'].map { |key| Color.escape(key) }
107
+ color ? "#{color}#{string}#{end_color}" : string
108
+ end
109
+
110
+ # Extracts the text from a colorized string
111
+ def self.raw string
112
+ string.gsub(/\033\[\d;\d{2}m/, '').gsub(/\033\[0;0m/, '')
109
113
  end
114
+
110
115
  end
111
116
 
112
117
  # see #Color#colorize
@@ -145,6 +150,22 @@ module Yummi
145
150
  # A module to align texts based on a reference width
146
151
  module Aligner
147
152
 
153
+ #
154
+ # Aligns the text
155
+ #
156
+ # === Args
157
+ #
158
+ # +to+::
159
+ # Defines the type of the alignment (must be a method defined in this module)
160
+ # +text+::
161
+ # The text to align
162
+ # +width+::
163
+ # The width of alignment, this will define how much the text will be moved.
164
+ #
165
+ def self.align to, text, width
166
+ send to, text, width
167
+ end
168
+
148
169
  # Aligns the text to the right
149
170
  def self.right text, width
150
171
  text.rjust(width)
@@ -155,6 +176,31 @@ module Yummi
155
176
  text.ljust(width)
156
177
  end
157
178
 
179
+ # Aligns the text to the center
180
+ def self.center text, width
181
+ size = width - text.size
182
+ left_size = size / 2
183
+ right_size = size - left_size
184
+ (' ' * left_size) << text << (' ' * right_size)
185
+ end
186
+
187
+ # Aligns the text to both sides
188
+ def self.justify text, width
189
+ extra_spaces = width - text.size
190
+ return text if extra_spaces == 0
191
+ words = text.split ' '
192
+ return text if extra_spaces / (words.size - 1) > 2
193
+ until extra_spaces == 0
194
+ words.each_index do |i|
195
+ break if i - 1 == words.size # do not add spaces in the last word
196
+ words[i] << ' '
197
+ extra_spaces -= 1
198
+ break if extra_spaces == 0
199
+ end
200
+ end
201
+ words.join ' '
202
+ end
203
+
158
204
  end
159
205
 
160
206
  # A module with useful colorizers
@@ -177,9 +223,25 @@ module Yummi
177
223
  EvalColorizer::new &block
178
224
  end
179
225
 
180
- # Returns the #IndexedDataColorizer module
181
- def self.by_index
182
- IndexedDataColorizer
226
+ # Returns a new instance of #StripeColorizer
227
+ def self.stripe *colors
228
+ StripeColorizer::new *colors
229
+ end
230
+
231
+ # A colorizer that cycles through colors to create a striped effect
232
+ class StripeColorizer
233
+
234
+ # Creates a new colorizer using the given colors
235
+ def initialize *colors
236
+ @colors = colors
237
+ @count = -1
238
+ end
239
+
240
+ def call *args
241
+ @count += 1
242
+ @colors[@count % @colors.size]
243
+ end
244
+
183
245
  end
184
246
 
185
247
  #
@@ -256,31 +318,6 @@ module Yummi
256
318
 
257
319
  end
258
320
 
259
- # A module with colorizers that uses indexes
260
- module IndexedDataColorizer
261
-
262
- # Returns a colorizer that uses the given color in odd indexes
263
- def self.odd color
264
- lambda do |index, data|
265
- color if index.odd?
266
- end
267
- end
268
-
269
- # Returns a colorizer that uses the given color in even indexes
270
- def self.even color
271
- lambda do |index, data|
272
- color if index.even?
273
- end
274
- end
275
-
276
- # Returns a colorizer that uses the first color for odd indexes and the second for
277
- # even indexes.
278
- def self.zebra first_color, second_color
279
- Yummi::Colorizer.join odd(first_color), even(second_color)
280
- end
281
-
282
- end
283
-
284
321
  end
285
322
 
286
323
  # A module with useful formatters
data/lib/yummi/table.rb CHANGED
@@ -266,9 +266,9 @@ module Yummi
266
266
  row.each_index do |j|
267
267
  column = row[j]
268
268
  width = max_width data, j
269
- align = (@align[j] or @default_align)
269
+ alignment = (@align[j] or @default_align)
270
270
  color = color_map[i][j]
271
- value = Aligner.send align, column.to_s, width
271
+ value = Aligner.align alignment, column.to_s, width
272
272
  value = Color.colorize value, color unless @no_colors
273
273
  string << value
274
274
  string << (' ' * @colspan)
@@ -0,0 +1,132 @@
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
+ module Yummi
24
+
25
+ # A box to decorate texts
26
+ class TextBox
27
+ # The border color
28
+ attr_accessor :color
29
+ # The box content
30
+ attr_accessor :content
31
+ # The box maximum width
32
+ attr_accessor :max_width
33
+ # The default alignment to use
34
+ attr_accessor :default_align
35
+
36
+ def initialize params = {}
37
+ params = {
38
+ :color => :white,
39
+ :content => ''
40
+ }.merge! params
41
+ @color = params[:color]
42
+ @content = params[:content].to_s
43
+ end
44
+
45
+ #
46
+ # Adds a line text to this box
47
+ #
48
+ # === Args
49
+ #
50
+ # +line_text+::
51
+ # The text to add.
52
+ # +params+::
53
+ # A hash of parameters. Currently supported are:
54
+ # color: the text color (see #Yummi#COLORS)
55
+ # width: the text maximum width. Set this to break the lines automatically.
56
+ #
57
+ def add text, params = {}
58
+ params = {
59
+ :width => @max_width + 1,
60
+ :align => @default_align
61
+ }.merge! params
62
+ if params[:width]
63
+ width = params[:width]
64
+ words = text.gsub($/, ' ').split(' ')
65
+ buff = ''
66
+ words.each do |word|
67
+ if buff.size + word.size > width
68
+ _add_ buff, params
69
+ buff = ''
70
+ end
71
+ buff << ' ' unless buff.empty?
72
+ buff << word
73
+ end
74
+ unless buff.empty?
75
+ _add_ buff, params
76
+ end
77
+ else
78
+ text.each_line do |line|
79
+ _add_ line, params
80
+ end
81
+ end
82
+ end
83
+
84
+ alias_method :<<, :add
85
+
86
+ # Adds a line break to the text.
87
+ def line_break
88
+ @content << $/
89
+ end
90
+
91
+ #
92
+ # Prints the #to_s into the given object.
93
+ #
94
+ def print to = $stdout
95
+ to.print to_s
96
+ end
97
+
98
+ def to_s
99
+ width = 0
100
+ sizes = []
101
+ content.each_line do |line|
102
+ size = (Yummi::Color::raw line.chomp).size
103
+ sizes << size
104
+ width = [width, size].max
105
+ end
106
+ border = Yummi.colorize('+' + ('-' * width) + '+', color) + $/
107
+ pipe = Yummi.colorize '|', color
108
+ buff = ''
109
+ buff << border
110
+ i = 0
111
+ content.each_line do |line|
112
+ diff = width - sizes[i]
113
+ buff << pipe << line.chomp << (' ' * diff) << pipe << $/
114
+ i += 1
115
+ end
116
+ buff << border
117
+ buff
118
+ end
119
+
120
+ private
121
+
122
+ def _add_ text, params
123
+ if params[:align] and params[:width]
124
+ text = Yummi::Aligner.align params[:align], text, params[:width]
125
+ end
126
+ @content << Yummi.colorize(text, params[:color])
127
+ line_break
128
+ end
129
+
130
+ end
131
+
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.1.0"
24
+ VERSION = "0.2.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.1.0
4
+ version: 0.2.0
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-04-30 00:00:00.000000000Z
12
+ date: 2012-05-14 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: A tool to colorize your console application.
15
15
  email:
@@ -24,6 +24,7 @@ files:
24
24
  - LICENSE
25
25
  - README.md
26
26
  - Rakefile
27
+ - examples/box_license.rb
27
28
  - examples/list_files.rb
28
29
  - examples/logger.rb
29
30
  - examples/monitor_table.rb
@@ -32,6 +33,7 @@ files:
32
33
  - lib/yummi/logger.rb
33
34
  - lib/yummi/no_colors.rb
34
35
  - lib/yummi/table.rb
36
+ - lib/yummi/text_box.rb
35
37
  - lib/yummi/version.rb
36
38
  - yummi.gemspec
37
39
  homepage: https://github.com/ataxexe/yummi