yummi 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,14 +24,14 @@ the examples dir to see how to use Yummi features.
24
24
 
25
25
  ## Command Line Tools
26
26
 
27
- Yummi exposes a 'colorize' program that you can use to colorize texts and apply
27
+ Yummi exposes a 'yummi' program that you can use to colorize texts and apply
28
28
  patterns to colorize lines (usefull to tail logs).
29
29
 
30
30
  Examples:
31
31
 
32
- colorize -c intense_red -m "some text"
33
- echo "some text" | colorize -c intense_red
34
- tail -f $JBOSS_HOME/standalone/log/server.log | colorize -p path-to-your-jboss7-mapping.yaml
32
+ yummi -c intense_red -m "some text"
33
+ echo "some text" | yummi -c intense_red
34
+ tail -f $JBOSS_HOME/standalone/log/server.log | yummi -p path-to-your-jboss7-mapping.yaml
35
35
 
36
36
  Line patterns are configured with an yaml file containing:
37
37
 
@@ -54,7 +54,7 @@ Yummi provides a set of mappings that you, check yummi/mappings dir.
54
54
 
55
55
  Mappings provided by yummi can be passed with the file name, example:
56
56
 
57
- tail -f $JBOSS_HOME/standalone/log/server.log | colorize -p jboss7
57
+ tail -f $JBOSS_HOME/standalone/log/server.log | yummi -p jboss7
58
58
 
59
59
  Mappings in ~/.yummi dir may also used only with the file name.
60
60
 
@@ -46,7 +46,7 @@ end
46
46
  opt.on '--data-type', 'Defines the data type to parse the values' do |data_type|
47
47
  @data_type = data_type
48
48
  end
49
- opt.on '-e FILES', '--eval=FILES', Array, 'Sets a file to eval for extending components' do |files|
49
+ opt.on '-e FILES', '--eval=FILES', Array, 'Include the files for extending components' do |files|
50
50
  files.each do |file|
51
51
  load File.expand_path(file)
52
52
  end
@@ -30,7 +30,7 @@ tablebuilder = Yummi::TableBuilder::new('cash_flow_table.yaml')
30
30
 
31
31
  @table = tablebuilder.defaults.build_table
32
32
 
33
- @table.data = [['Initial', 0, 0, false, nil],
33
+ @table.data = [['Initial', nil, 0, nil, nil],
34
34
  ['Deposit', 100.58, 100.58, true, "QAWSEDRFTGH535"],
35
35
  ['Withdraw', -50.23, 50.35, true, "34ERDTF6GYU"],
36
36
  ['Withdraw', -100, -49.65, true, "2344EDRFT5"],
@@ -44,6 +44,10 @@ end
44
44
 
45
45
  opt.on '--box', 'Prints the table inside a box' do
46
46
  @box = Yummi::TextBox::new
47
+ @box.style.top_left = '/'
48
+ @box.style.bottom_left = '\\'
49
+ @box.style.top_right = '\\'
50
+ @box.style.bottom_right = '/'
47
51
  end
48
52
  opt.on '--help', 'Prints this message' do
49
53
  puts opt
@@ -35,7 +35,7 @@ color:
35
35
  row_color:
36
36
  blessed_income: yellow
37
37
 
38
- contexts:
38
+ top:
39
39
  - format:
40
40
  total:
41
41
  numeric:
@@ -43,4 +43,13 @@ contexts:
43
43
  zero: "%.2f"
44
44
  negative: "%.2f"
45
45
  row_color:
46
- with: white
46
+ with: bold_white
47
+ bottom:
48
+ - format:
49
+ total:
50
+ numeric:
51
+ positive: "%.2f"
52
+ zero: "%.2f"
53
+ negative: "%.2f"
54
+ row_color:
55
+ with: bold_white
@@ -1,4 +1,4 @@
1
- Initial, 0, 0, false
1
+ Initial, , 0, false
2
2
  Deposit, 100.58, 100.58, true, QAWSEDRFTGH535
3
3
  Withdraw, -50.23, 50.35, true, 34ERDTF6GYU
4
4
  Withdraw, -100, -49.65, true, 2344EDRFT5
@@ -1,5 +1,5 @@
1
1
  - - Initial
2
- - 0
2
+ -
3
3
  - 0
4
4
  - false
5
5
  -
@@ -1,8 +1,8 @@
1
1
  module Yummi
2
2
  module Colorizers
3
3
  def self.blessed_income color
4
- Yummi::to_colorize do |data| # or |data, index| if you need the index
5
- color if data[:value] > data[:total]
4
+ Yummi::to_colorize do |ctx|
5
+ color if ctx[:value] > ctx[:total]
6
6
  end
7
7
  end
8
8
  end
@@ -25,17 +25,48 @@ require_relative '../lib/yummi'
25
25
 
26
26
  @box = Yummi::TextBox.new
27
27
 
28
- @box.width = 70
29
- @box.default_align = :justify
30
- @box.default_separator[:color] = :yellow
28
+ @box.style.width = 70
29
+ @box.style.align = :justify
30
+ @box.style.separator[:color] = :purple
31
+ @box.style.border[:color] = :intense_purple
31
32
 
32
33
  opt = OptionParser::new
33
34
 
34
35
  opt.on '--align ALIGN', 'Sets the default alignment to use' do |align|
35
- @box.default_align = align.to_sym
36
+ @box.style.align = align.to_sym
36
37
  end
37
38
  opt.on '--width WIDTH', Integer, 'Sets the text box width' do |width|
38
- @box.width = width
39
+ @box.style.border[:width] = width
40
+ end
41
+ opt.on '--left PATTERN', 'Sets the left line pattern' do |pattern|
42
+ @box.style.border[:left] = pattern
43
+ end
44
+ opt.on '--right PATTERN', 'Sets the right line pattern' do |pattern|
45
+ @box.style.border[:right] = pattern
46
+ end
47
+ opt.on '--top-left PATTERN', 'Sets the top left corner pattern' do |pattern|
48
+ @box.style.border[:top_left] = pattern
49
+ end
50
+ opt.on '--top-right PATTERN', 'Sets the top right corner pattern' do |pattern|
51
+ @box.style.border[:top_right] = pattern
52
+ end
53
+ opt.on '--bottom-left PATTERN', 'Sets the bottom left corner pattern' do |pattern|
54
+ @box.style.border[:bottom_left] = pattern
55
+ end
56
+ opt.on '--bottom-right PATTERN', 'Sets the bottom right corner pattern' do |pattern|
57
+ @box.style.border[:bottom_right] = pattern
58
+ end
59
+ opt.on '--top PATTERN', 'Sets the top line pattern' do |pattern|
60
+ @box.style.border[:top] = pattern
61
+ end
62
+ opt.on '--bottom PATTERN', 'Sets the bottom line pattern' do |pattern|
63
+ @box.style.border[:bottom] = pattern
64
+ end
65
+ opt.on '--border COLOR', 'Sets the border color' do |color|
66
+ @box.style.border[:color] = color
67
+ end
68
+ opt.on '--no-border', 'Hide the text box borders' do
69
+ @box.no_border
39
70
  end
40
71
  opt.on '--help', 'Prints this message' do
41
72
  puts opt
@@ -56,12 +87,12 @@ opt.parse! ARGV
56
87
  @box.line_break
57
88
  @box.add 'The above copyright notice and this permission notice shall be included in all
58
89
  copies or substantial portions of the Software.'
59
- @box.separator :width => @box.width / 3, :align => :center
90
+ @box.separator :width => @box.style.width / 3, :align => :center
60
91
  @box.add 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61
92
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
62
93
  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
63
94
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
64
95
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
65
96
  OTHER DEALINGS IN THE SOFTWARE.',
66
- :color => :red
97
+ :color => :yellow
67
98
  @box.print
@@ -40,7 +40,7 @@ opt.on '--color TYPE', 'Specify the color type (zebra,file,none)' do |type|
40
40
  case type
41
41
  when 'zebra'
42
42
  @table.colorize_row :using => Yummi::Colorizers.stripe(:intense_gray, :intense_white)
43
- @table.context do
43
+ @table.bottom do
44
44
  @table.colorize_row :with => :intense_blue
45
45
  @table.format :size, :using => Yummi::Formatters.byte
46
46
  end
@@ -48,7 +48,7 @@ opt.on '--color TYPE', 'Specify the color type (zebra,file,none)' do |type|
48
48
  @table.colorize_row do |data| # or |data, index| if you need the index
49
49
  data[:directory] ? :intense_gray : :intense_white
50
50
  end
51
- @table.context do
51
+ @table.bottom do
52
52
  @table.colorize_row :with => :intense_blue
53
53
  @table.format :size, :using => Yummi::Formatters.byte
54
54
  end
@@ -33,32 +33,45 @@ opt = OptionParser::new
33
33
  # formats memory info for easily reading
34
34
  @table.format [:max_memory, :free_memory], :using => Yummi::Formatters.byte
35
35
 
36
- @table.context do
36
+ @table.bottom do
37
37
  @table.format [:max_memory, :free_memory], :using => Yummi::Formatters.byte
38
- @table.colorize_row :with => :white
38
+ @table.colorize_row :with => :intense_white
39
39
  end
40
40
 
41
41
  # colorizer for memory
42
- memory_colorizer = Yummi::Colorizers.percentage :max => :max_memory, :free => :free_memory
42
+ @memory_colorizer = Yummi::Colorizers.percentage :max => :max_memory, :free => :free_memory
43
43
 
44
44
  # colorizer for threads
45
- thread_colorizer = Yummi::Colorizers.percentage :max => :max_threads,
46
- :using => :in_use_threads,
47
- :threshold => {
48
- :warn => 0.9,
49
- :bad => 0.7
50
- }
51
-
52
- opt.on '--color TYPE', 'Specify the color type (zebra,cell,none)' do |type|
45
+ @thread_colorizer = Yummi::Colorizers.percentage :max => :max_threads,
46
+ :using => :in_use_threads,
47
+ :colors => {
48
+ :omg => :intense_red
49
+ },
50
+ :threshold => {
51
+ :warn => 0.9,
52
+ :bad => 0.5,
53
+ :omg => 0.3
54
+ }
55
+
56
+ def zebra_colors
57
+ @table.colorize_row :using => Yummi::Colorizers.stripe(:yellow, :purple)
58
+ end
59
+
60
+ def full_colors
61
+ @table.colorize :server_name, :with => :purple
62
+ @table.colorize :free_memory, :using => @memory_colorizer
63
+ @table.colorize :in_use_threads, :using => @thread_colorizer
64
+ @table.colorize [:max_memory, :max_threads], :with => :gray
65
+ end
66
+
67
+ full_colors
68
+
69
+ opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
53
70
  case type
54
71
  when 'zebra'
55
- @table.colorize_row :using => Yummi::Colorizers.stripe(:yellow, :purple)
56
- when 'cell'
57
- @table.colorize :server_name, :with => :purple
58
- @table.using_row do
59
- @table.colorize :free_memory, :using => memory_colorizer
60
- @table.colorize :in_use_threads, :using => thread_colorizer
61
- end
72
+ zebra_colors
73
+ when 'full'
74
+ full_colors
62
75
  when 'none'
63
76
  @table.no_colors
64
77
  else
@@ -88,7 +101,7 @@ opt.parse ARGV
88
101
  ['Server 1', 1_000_000_000, 750_000_000, 200, 170],
89
102
  ['Server 2', 1_000_000_000, 700_000_000, 200, 180],
90
103
  ['Server 3', 1_000_000_000, 50_000_000, 200, 50],
91
- ['Server 4', 1_000_000_000, 200_000_000, 200, 50],
104
+ ['Server 4', 1_000_000_000, 200_000_000, 200, 100],
92
105
  ['Server 5', 1_000_000_000, 5_000_000, 200, 50],
93
106
  ['Server 6', 1_000_000_000, 750_000_000, 200, 50],
94
107
  ]
@@ -99,11 +112,25 @@ opt.parse ARGV
99
112
  :max_threads => 200,
100
113
  :in_use_threads => 170
101
114
 
102
- @table.add :server_name => 'Server 8',
103
- :max_memory => 1_000_000_000,
104
- :free_memory => 5_000_000,
105
- :max_threads => 200,
106
- :in_use_threads => 180
115
+ class ServerStatus
116
+ attr_reader :server_name, :max_memory, :free_memory, :max_threads, :in_use_threads
117
+
118
+ def initialize server_name, max_memory, free_memory, max_threads, in_use_threads
119
+ @server_name = server_name
120
+ @max_memory = max_memory
121
+ @free_memory = free_memory
122
+ @max_threads = max_threads
123
+ @in_use_threads = in_use_threads
124
+ end
125
+ end
126
+
127
+ @table.add ServerStatus::new(
128
+ 'Server 8',
129
+ 1_000_000_000,
130
+ 5_000_000,
131
+ 200,
132
+ 180
133
+ )
107
134
 
108
135
  @table.add :server_name => 'Total',
109
136
  :max_memory => @table.column(:max_memory).inject(:+),
data/lib/yummi.rb CHANGED
@@ -28,20 +28,19 @@ module Yummi
28
28
 
29
29
  # The Color Schema Definition
30
30
  module Schema
31
- # Normal Linux Terminal Colors, used by default in normal, underscore, blink and
32
- # highlight color types
31
+ # Normal Linux Terminal Colors, used by default in normal color types
33
32
  NORMAL_COLORS = {
34
- :colors => [:black, :red, :green, :yellow, :blue, :purple, :cyan, :gray],
33
+ :colors => [:black, :red, :green, :yellow, :blue, :purple, :cyan, [:gray, :white]],
35
34
  :default => :gray
36
35
  }
37
- # Intense Linux Terminal Colors, used by default in intense and strong color types
36
+ # Intense Linux Terminal Colors, used by default in bold color types
38
37
  ALTERNATE_COLORS = {
39
38
  :colors => [:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white],
40
39
  :default => :gray
41
40
  }
42
41
  end
43
42
 
44
- # Colors from default linux terminal scheme
43
+ # Color mappings
45
44
  COLORS = {}
46
45
 
47
46
  #
@@ -72,30 +71,34 @@ module Yummi
72
71
  # (1 based) too. (:intense_red and :intense_2, for example)
73
72
  #
74
73
  def self.add_color_map mappings
75
- mappings.each do |type, config|
76
- schema = config[:schema]
77
- schema[:colors].each_with_index do |color, key_code|
78
- # maps the default color for a type
79
- COLORS[type] = "#{config[:key_code]};3#{key_code}" if color == schema[:default]
80
- # do not use prefix if schema is default
81
- prefix = (type == :default ? '' : "#{type}_")
82
- # maps the color using color name
83
- key = "#{prefix}#{color}"
84
- COLORS[key.to_sym] = "#{config[:key_code]};3#{key_code}"
85
- # maps the color using color key code
86
- key = "#{prefix}#{key_code + 1}"
87
- COLORS[key.to_sym] = "#{config[:key_code]};3#{key_code}"
88
- # maps the color using color name if default schema does not defines it
89
- # example: yellow and white are present only in strong/intense schema
90
- COLORS[color.to_sym] = "#{config[:key_code]};3#{key_code}" unless COLORS[color]
74
+ mappings.each do |types, config|
75
+ [*types].each do |type|
76
+ schema = config[:schema]
77
+ schema[:colors].each_with_index do |colors, key_code|
78
+ [*colors].each do |color|
79
+ # maps the default color for a type
80
+ COLORS[type] = "#{config[:key_code]}#{key_code}" if color == schema[:default]
81
+ # do not use prefix if schema is default
82
+ prefix = (type == :default ? '' : "#{type}_")
83
+ # maps the color using color name
84
+ key = "#{prefix}#{color}"
85
+ COLORS[key.to_sym] = "#{config[:key_code]}#{key_code}"
86
+ # maps the color using color key code
87
+ key = "#{prefix}#{key_code + 1}"
88
+ COLORS[key.to_sym] = "#{config[:key_code]}#{key_code}"
89
+ # maps the color using color name if default schema does not defines it
90
+ # example: yellow and white are present only in strong/intense schema
91
+ COLORS[color.to_sym] = "#{config[:key_code]}#{key_code}" unless COLORS[color]
92
+ end
93
+ end
91
94
  end
92
95
  end
93
96
  end
94
97
 
95
98
  # Escape the given text with the given color code
96
99
  def self.escape key
97
- return key unless key and COLORS[key.to_sym]
98
- "\e[#{COLORS[key.to_sym]}m"
100
+ return key unless key and COLORS[key.to_s.to_sym]
101
+ "\e[#{COLORS[key.to_s.to_sym]}m"
99
102
  end
100
103
 
101
104
  # Colorize the given text with the given color
@@ -111,37 +114,26 @@ module Yummi
111
114
 
112
115
  end
113
116
 
117
+ #
118
+ # Colorizes the text using the given color.
119
+ #
120
+ # This method also checks if the color mode is in chain (like in "underline_bold_green").
121
+ #
114
122
  # see #Color#colorize
115
- def self.colorize string, color
116
- Color.colorize string, color
117
- end
118
-
119
- #
120
- # A module to handle blocks by dynamically resolving parameters
121
- #
122
- # see #DataEvalColorizer
123
123
  #
124
- module BlockHandler
125
-
126
- #
127
- # Calls the block resolving the parameters by getting the parameter name from the
128
- # given context.
129
- #
130
- # === Example
131
- #
132
- # context = :max => 10, :curr => 5, ratio => 0.15
133
- # percentage = BlockHandler.call_block(context) { |max,curr| curr.to_f / max }
134
- #
135
- def block_call (context, &block)
136
- args = []
137
- block.parameters.each do |parameter|
138
- args << context[parameter[1]]
124
+ def self.colorize string, color
125
+ #check if there is more than one color classifier
126
+ modes = color.to_s.split(/_/)
127
+ if modes.size >= 3
128
+ color = modes.last
129
+ modes.delete_at(-1)
130
+ modes.each do |mode|
131
+ string = Color.colorize(string, "#{mode}_#{color}")
139
132
  end
140
- block.call(*args)
133
+ string
134
+ else
135
+ Color.colorize string, color
141
136
  end
142
-
143
- module_function :block_call
144
-
145
137
  end
146
138
 
147
139
  # A module to align texts based on a reference width
@@ -191,7 +183,9 @@ module Yummi
191
183
  extra_spaces = width - text.size
192
184
  return text unless extra_spaces > 0
193
185
  words = text.split ' '
186
+ # do not justify only one word
194
187
  return text if words.size == 1
188
+ # do not justify few words
195
189
  return text if extra_spaces / (words.size - 1) > 2
196
190
  until extra_spaces == 0
197
191
  words.each_index do |i|
@@ -206,6 +200,46 @@ module Yummi
206
200
 
207
201
  end
208
202
 
203
+ #
204
+ # A module to handle blocks by dynamically resolving parameters
205
+ #
206
+ # see #DataEvalColorizer
207
+ #
208
+ module BlockHandler
209
+
210
+ #
211
+ # Calls the block resolving the parameters by getting the parameter name from the
212
+ # given context.
213
+ #
214
+ # === Example
215
+ #
216
+ # context = :max => 10, :curr => 5, ratio => 0.15
217
+ # percentage = BlockHandler.call_block(context) { |max,curr| curr.to_f / max }
218
+ #
219
+ def block_call (context, &block)
220
+ args = []
221
+ block.parameters.each do |parameter|
222
+ args << context[parameter[1]]
223
+ end
224
+ block.call(*args)
225
+ end
226
+
227
+ module_function :block_call
228
+
229
+ end
230
+
231
+ class Context
232
+ attr_reader :value, :obj
233
+
234
+ def initialize (value)
235
+ @value = value
236
+ end
237
+
238
+ def [] (index)
239
+ obj[index]
240
+ end
241
+ end
242
+
209
243
  # A class to expose indexed data by numeric indexes and aliases.
210
244
  class IndexedData
211
245