yummi 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rakeTasks ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build yummi-0.0.1.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Build and install yummi-0.0.1.gem into system gems" fullCmd="install" taksId="install" /><RakeTask description="Create tag v0.0.1 and build and push yummi-0.0.1.gem to Rubygems" fullCmd="release" taksId="release" /></RakeGroup></Settings>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yummi.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Ataxexe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Yummi
2
+
3
+ This is a tool to colorize your console app.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'yummi'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install yummi
18
+
19
+ ## Usage
20
+
21
+ Comming soon...
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,70 @@
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
+ opt = OptionParser::new
27
+
28
+ @basedir = ENV['HOME']
29
+
30
+ @table = Yummi::Table::new
31
+ # setting the header sets the aliases automaically
32
+ @table.header = ['Name', 'Size']
33
+ @table.aliases << :directory
34
+ # sets the title
35
+ @table.title = 'Files in home folder'
36
+ # aligns the first column to the left
37
+ @table.align :name, :left
38
+ # formats size for easily reading
39
+ @table.format :size, :using => Yummi::Formatter.bytes
40
+
41
+ opt.on '--color TYPE', 'Specify the color type (zebra,file,none)' do |type|
42
+ case type
43
+ when 'zebra'
44
+ @table.row_colorizer Yummi::IndexedDataColorizer.odd :with => :intense_gray
45
+ @table.row_colorizer Yummi::IndexedDataColorizer.even :with => :intense_white
46
+ when 'file'
47
+ @table.row_colorizer do |i, data|
48
+ data[:directory] ? :intense_gray : :intense_white
49
+ end
50
+ when 'none'
51
+ @table.no_colors
52
+ else
53
+ end
54
+ end
55
+ opt.on '--basedir BASEDIR', 'Selects the basedir to list files' do |basedir|
56
+ @basedir = basedir
57
+ end
58
+ opt.on '--help', 'Prints this message' do
59
+ puts opt
60
+ exit 0
61
+ end
62
+
63
+ opt.parse ARGV
64
+ files = Dir[File.join(@basedir, '*')]
65
+ data = []
66
+ files.each do |f|
67
+ data << [f, File.size(f), File.directory?(f)] # the last value will not be printed
68
+ end
69
+ @table.data = data
70
+ @table.print
@@ -0,0 +1,35 @@
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 'logger'
24
+ require_relative '../lib/yummi'
25
+
26
+ logger = Logger::new STDOUT
27
+ logger.level = Logger::DEBUG
28
+ logger.formatter = Yummi::Formatter::LogFormatter.new
29
+
30
+ logger.debug "File: #{__FILE__}"
31
+ logger.info "Example started"
32
+ logger.warn "Remember to read the examples for better use of Yummi!"
33
+ logger.error "An error has occurred"
34
+ logger.fatal "A fatal exception has occurred"
35
+ logger.unknown "Unknown severity message"
@@ -0,0 +1,87 @@
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
+ opt = OptionParser::new
27
+
28
+ @table = Yummi::Table::new
29
+ # setting the header sets the aliases automaically
30
+ @table.header = ['Description', 'Value', 'Total', 'Eletronic']
31
+ # sets the title
32
+ @table.title = 'Cash Flow'
33
+ # aligns the first column to the left
34
+ @table.align :description, :left
35
+ # colorize all values from the Description column to purple
36
+ # formats booleans using Yes or No
37
+ @table.format :eletronic, :using => Yummi::Formatter.yes_or_no
38
+ # shows values without minus signal and rounded
39
+ @table.format :value, :using => lambda { |value| "%.2f" % value.abs }
40
+ # shows totals rounded
41
+ @table.format :total, :with => "%.2f"
42
+ # table data
43
+ @table.data = [['Initial', 0, 0, false],
44
+ ['Deposit', 100, 100, true],
45
+ ['Withdraw', -50, 50, true],
46
+ ['Withdraw', -100, -50, true],
47
+ ['Deposit', 50, 0, false],
48
+ ['Deposit', 600, 600, false]]
49
+
50
+ opt.on '--color TYPE', 'Specify the color type (zebra,full,none)' do |type|
51
+ case type
52
+ when 'zebra'
53
+ @table.row_colorizer Yummi::IndexedDataColorizer.odd :with => :brown
54
+ @table.row_colorizer Yummi::IndexedDataColorizer.even :with => :purple
55
+ when 'full'
56
+ @table.colorize :description, :with => :purple
57
+ # colorize booleans based on their values
58
+ @table.colorize :eletronic do |b|
59
+ b ? :blue : :cyan
60
+ end
61
+ # colorize the values based on comparison
62
+ colorizer = lambda do |value|
63
+ if value < 0 then
64
+ :red
65
+ else
66
+ value > 0 ? :green : :brown
67
+ end
68
+ end
69
+ @table.colorize :value, :using => colorizer
70
+ @table.colorize :total, :using => colorizer
71
+ # colorize rows that Value is greather than Total
72
+ @table.row_colorizer do |i, data|
73
+ :white if data[:value] > data[:total]
74
+ end
75
+ when 'none'
76
+ @table.no_colors
77
+ else
78
+ end
79
+ end
80
+ opt.on '--help', 'Prints this message' do
81
+ puts opt
82
+ exit 0
83
+ end
84
+
85
+ opt.parse ARGV
86
+
87
+ @table.print
@@ -0,0 +1,59 @@
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 'logger'
24
+
25
+ module Yummi
26
+
27
+ module Formatter
28
+
29
+ class LogFormatter < Logger::Formatter
30
+
31
+ attr_accessor :colors
32
+
33
+ def initialize
34
+ @colors = {
35
+ :debug => nil,
36
+ :info => :green,
37
+ :warn => :yellow,
38
+ :error => :red,
39
+ :fatal => :intense_red,
40
+ :any => :intense_gray
41
+ }
42
+ end
43
+
44
+ alias_method :super_call, :call
45
+
46
+ def call(severity, time, program_name, message)
47
+ color = @colors[severity.downcase.to_sym]
48
+ Yummi::Color.colorize output(severity, time, program_name, message), color
49
+ end
50
+
51
+ def output severity, time, program_name, message
52
+ super_call severity, time, program_name, message
53
+ end
54
+
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,33 @@
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
+ module Color
26
+
27
+ def self.colorize(str, color)
28
+ str
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,173 @@
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
+ class Table
26
+
27
+ attr_accessor :data, :header, :title, :default_align, :aliases, :colspan, :colors
28
+
29
+ def initialize
30
+ @data = []
31
+ @header = []
32
+ @title = nil
33
+ @colors = {
34
+ :title => :intense_yellow,
35
+ :header => :intense_blue,
36
+ :value => nil
37
+ }
38
+
39
+ @colspan = 2
40
+
41
+ @aliases = []
42
+
43
+ @align = []
44
+ @formatters = []
45
+ @colorizers = []
46
+ @row_colorizer = nil
47
+
48
+ @default_align = :right
49
+ end
50
+
51
+ def no_colors
52
+ @colors = {
53
+ :title => nil,
54
+ :header => nil,
55
+ :value => nil
56
+ }
57
+ @no_colors = true
58
+ end
59
+
60
+ def header= header
61
+ @aliases = header.map { |n| n.downcase.gsub(' ', '_').to_sym } if @aliases.empty?
62
+ @header = header
63
+ end
64
+
65
+ def align index, type
66
+ index = parse_index(index)
67
+ @align[index] = type
68
+ end
69
+
70
+ def row_colorizer colorizer = nil, &block
71
+ @row_colorizer ||= Yummi::LinkedBlocks::new
72
+ @row_colorizer << (colorizer or block)
73
+ end
74
+
75
+ def colorize index, params = {}, &block
76
+ index = parse_index(index)
77
+ @colorizers[index] = (params[:using] or block)
78
+ @colorizers[index] ||= proc do |value|
79
+ params[:with]
80
+ end
81
+ end
82
+
83
+ def format index, params = {}, &block
84
+ index = parse_index(index)
85
+ @formatters[index] = (params[:using] or block)
86
+ @formatters[index] ||= proc do |value|
87
+ params[:with] % value
88
+ end
89
+ end
90
+
91
+ def print to = $stdout
92
+ to.print to_s
93
+ end
94
+
95
+ def to_s
96
+ color_map, output = build_output
97
+ string = ""
98
+ string << Color.colorize(@title, @colors[:title]) << $/ if @title
99
+ output.each_index do |i|
100
+ row = output[i]
101
+ row.each_index do |j|
102
+ column = row[j]
103
+ width = max_width output, j
104
+ align = (@align[j] or @default_align)
105
+ color = color_map[i][j]
106
+ value = Aligner.send align, column.to_s, width
107
+ value = Color.colorize value, color unless @no_colors
108
+ string << value
109
+ string << (" " * @colspan)
110
+ end
111
+ string.strip! << $/
112
+ end
113
+ string
114
+ end
115
+
116
+ private
117
+
118
+ def build_output
119
+ color_map = []
120
+ output = []
121
+
122
+ _colors = []
123
+ _data = []
124
+
125
+ @header.each do |h|
126
+ _colors << @colors[:header]
127
+ _data << h
128
+ end
129
+ color_map << _colors
130
+ output << _data
131
+
132
+ @data.each_index do |row_index|
133
+ row = @data[row_index]
134
+ _colors = []
135
+ _data = []
136
+
137
+ row.each_index do |col_index|
138
+ next if @header and not @header[col_index]
139
+ column = row[col_index]
140
+ colorizer = @colorizers[col_index]
141
+ _colors << (colorizer ? colorizer.call(column) : @colors[:value])
142
+
143
+ formatter = @formatters[col_index]
144
+ _data << (formatter ? formatter.call(column) : column)
145
+ end
146
+ if @row_colorizer
147
+ row_data = IndexedData::new @aliases, row
148
+ row_color = @row_colorizer.call row_index, row_data
149
+ _colors.collect! { row_color } if row_color
150
+ end
151
+ color_map << _colors
152
+ output << _data
153
+ end
154
+
155
+ [color_map, output]
156
+ end
157
+
158
+ def parse_index(value)
159
+ return @aliases.index(value) unless value.is_a? Fixnum
160
+ value
161
+ end
162
+
163
+ def max_width data, column
164
+ max = 0
165
+ data.each do |row|
166
+ max = [row[column].to_s.length, max].max
167
+ end
168
+ max
169
+ end
170
+
171
+ end
172
+
173
+ end
@@ -0,0 +1,25 @@
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
+ VERSION = "0.0.1"
25
+ end
data/lib/yummi.rb ADDED
@@ -0,0 +1,208 @@
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 "yummi/version"
24
+ require_relative "yummi/table"
25
+ require_relative "yummi/logger"
26
+
27
+ module Yummi
28
+
29
+ module Color
30
+ # Colors from default linux terminal scheme
31
+ COLORS = {
32
+ :nothing => '0;0',
33
+
34
+ :black => '0;30',
35
+ :red => '0;31',
36
+ :green => '0;32',
37
+ :brown => '0;33',
38
+ :blue => '0;34',
39
+ :purple => '0;35',
40
+ :cyan => '0;36',
41
+ :gray => '0;37',
42
+
43
+ :black_underscored => '4;30',
44
+ :red_underscored => '4;31',
45
+ :green_underscored => '4;32',
46
+ :brown_underscored => '4;33',
47
+ :blue_underscored => '4;34',
48
+ :purple_underscored => '4;35',
49
+ :cyan_underscored => '4;36',
50
+ :gray_underscored => '4;37',
51
+
52
+ :blink_black => '5;30',
53
+ :blink_red => '5;31',
54
+ :blink_green => '5;32',
55
+ :blink_brown => '5;33',
56
+ :blink_blue => '5;34',
57
+ :blink_purple => '5;35',
58
+ :blink_cyan => '5;36',
59
+ :blink_gray => '5;37',
60
+
61
+ :highlight_black => '5;30',
62
+ :highlight_red => '5;31',
63
+ :highlight_green => '5;32',
64
+ :highlight_brown => '5;33',
65
+ :highlight_blue => '5;34',
66
+ :highlight_purple => '5;35',
67
+ :highlight_cyan => '5;36',
68
+ :highlight_gray => '5;37',
69
+
70
+ :intense_gray => '1;30',
71
+ :intense_red => '1;31',
72
+ :intense_green => '1;32',
73
+ :intense_yellow => '1;33',
74
+ :yellow => '1;33',
75
+ :intense_blue => '1;34',
76
+ :intense_purple => '1;35',
77
+ :intense_cyan => '1;36',
78
+ :intense_white => '1;37',
79
+ :white => '1;37'
80
+ }
81
+ # Types of color
82
+ TYPES = {
83
+ :normal => 0,
84
+ :intense => 1,
85
+ :underscored => 4,
86
+ :blink => 5,
87
+ :highlight => 7
88
+ }
89
+ # Parses the key
90
+ def self.parse(key)
91
+ keys = key.to_s.split '_'
92
+ type = keys[0].to_sym
93
+ color = keys[1].to_i
94
+ "#{TYPES[type]};3#{color - 1}"
95
+ end
96
+
97
+ # Escape the given text with the given color code
98
+ def self.escape(key)
99
+ return key unless key
100
+ color = COLORS[key]
101
+ color ||= parse(key)
102
+ "\033[#{color}m"
103
+ end
104
+
105
+ # Colorize the given text with the given color
106
+ def self.colorize(str, color)
107
+ col, nocol = [color, :nothing].map { |key| Color.escape(key) }
108
+ col ? "#{col}#{str}#{nocol}" : str
109
+ end
110
+
111
+ end
112
+
113
+ module Aligner
114
+
115
+ def self.right text, width
116
+ text.rjust(width)
117
+ end
118
+
119
+ def self.left text, width
120
+ text.ljust(width)
121
+ end
122
+
123
+ end
124
+
125
+ module IndexedDataColorizer
126
+
127
+ def self.odd params
128
+ lambda do |index, data|
129
+ params[:with] if index.odd?
130
+ end
131
+ end
132
+
133
+ def self.even params
134
+ lambda do |index, data|
135
+ params[:with] if index.even?
136
+ end
137
+ end
138
+
139
+ end
140
+
141
+ module Formatter
142
+
143
+ def self.yes_or_no
144
+ lambda do |value|
145
+ value ? "Yes" : "No"
146
+ end
147
+ end
148
+
149
+ def self.unit params
150
+ lambda do |value|
151
+ result = value
152
+ units = params[:range]
153
+ units.each_index do |i|
154
+ minimun = (params[:step] ** i)
155
+ result = "%.#{params[:precision]}f #{units[i]}" % (value.to_f / minimun) if value >= minimun
156
+ end
157
+ result
158
+ end
159
+ end
160
+
161
+ def self.bytes precision = 1
162
+ unit :range => %w{B KB MB GB TB}, :step => 1024, :precision => precision
163
+ end
164
+
165
+ end
166
+
167
+ class IndexedData
168
+
169
+ def initialize aliases, data
170
+ @aliases = aliases
171
+ @data = data
172
+ end
173
+
174
+ def [] value
175
+ if value.is_a? Fixnum
176
+ @data[value]
177
+ else
178
+ @data[@aliases.index(value)]
179
+ end
180
+ end
181
+
182
+ end
183
+
184
+ class LinkedBlocks
185
+
186
+ def initialize params = {}
187
+ @blocks = []
188
+ @call_all = params[:call_all]
189
+ end
190
+
191
+ def << block
192
+ @blocks << block
193
+ end
194
+
195
+ def call *args
196
+ result = nil
197
+ @blocks.each do |block|
198
+ break if result and not @call_all
199
+ result = block.call *args
200
+ end
201
+ result
202
+ end
203
+
204
+ end
205
+
206
+ end
207
+
208
+ require_relative 'yummi/no_colors' if RUBY_PLATFORM['mingw'] #Windows
data/yummi.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/yummi/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Ataxexe"]
6
+ gem.email = ["ataxexe@gmail.com"]
7
+ gem.description = "A tool to colorize your console application."
8
+ gem.summary = "A tool to colorize your console application."
9
+ gem.homepage = "https://github.com/ataxexe/yummi"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "yummi"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Yummi::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yummi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ataxexe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-08 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A tool to colorize your console application.
15
+ email:
16
+ - ataxexe@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rakeTasks
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - examples/list_files.rb
28
+ - examples/logger.rb
29
+ - examples/table_cash_flow.rb
30
+ - lib/yummi.rb
31
+ - lib/yummi/logger.rb
32
+ - lib/yummi/no_colors.rb
33
+ - lib/yummi/table.rb
34
+ - lib/yummi/version.rb
35
+ - yummi.gemspec
36
+ homepage: https://github.com/ataxexe/yummi
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.10
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: A tool to colorize your console application.
60
+ test_files: []