utils_drawer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a1156807d839430463b403ef305d252b0579e6ed
4
+ data.tar.gz: 05dc85d0d046a4800867bd00abd02597397f8d96
5
+ SHA512:
6
+ metadata.gz: eeccd378c0c5c4f688457bead453a1990eecb26148d1de73a9a6462aa6463b4734240f933ed6d073e700a2a4f507c4aa786448a6cc61f07470e987038bd0eb2d
7
+ data.tar.gz: 44b53f23f4426a16611475ec8c131835db0bfd0450572994d2032a448d7feb4d9e355419713102a921927ac11c4b36f63f3fe19138cc5fb8dbbb7cf993b5961c
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in utils_drawer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 kaihar4
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,102 @@
1
+ # UtilsDrawer
2
+
3
+ Helper for drawing utils on CLI.
4
+
5
+ ## Description
6
+
7
+ UtilsDrawer will help to draw utils such as a table or graph.
8
+
9
+ Supporting the simple drawing by using Ruby's Block syntax.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'utils_drawer'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Table Drawer
22
+
23
+ Adjeust the column width at first row.
24
+ The column width is 10 by the default.
25
+ It is also possible to specify the column width in UtilsDrawer.column second argument.
26
+
27
+ ```ruby
28
+ require 'utils_drawer'
29
+
30
+ include UtilsDrawer
31
+
32
+ table do
33
+ row do
34
+ column('NAME')
35
+ column('SCORE', 20)
36
+ column('AGE')
37
+ end
38
+ row do
39
+ column('kaihara')
40
+ column('100,000,000')
41
+ column('22')
42
+ end
43
+ row do
44
+ column('hogefugahara')
45
+ column('200,000,000')
46
+ column('33')
47
+ end
48
+ end
49
+ ```
50
+
51
+ If the number of characters exceeds the column width, it is omitted.
52
+
53
+ ![table](https://raw.github.com/wiki/kaihar4/utils_drawer/images/table.png)
54
+
55
+ If you want to change the column width of the default:
56
+
57
+ ```ruby
58
+ ...
59
+
60
+ table(20) do
61
+ ...
62
+ end
63
+ ```
64
+
65
+ ### Graph Drawer
66
+
67
+ UtilsDrawer.data accepts the data label in the first argument, the value in the second argument.
68
+ The value is the bar length.
69
+ The color of the bar is determined by the bar length.
70
+
71
+ ```ruby
72
+ require 'utils_drawer'
73
+
74
+ include UtilsDrawer
75
+
76
+ graph do
77
+ data('foo', 10)
78
+ data('hoge', 17)
79
+ data('nullpo', 0)
80
+ data('nullnull', 0)
81
+ data('nuller', 0)
82
+ data('nullest', 0)
83
+ data('bar', 7)
84
+ data('fuga', 24)
85
+ end
86
+ ```
87
+
88
+ Be omitted value is 0 to continue.
89
+
90
+ ![graph1](https://raw.github.com/wiki/kaihar4/utils_drawer/images/graph1.png)
91
+
92
+ If you don't want to omit:
93
+
94
+ ```ruby
95
+ ...
96
+
97
+ graph({ omission: false }) do
98
+ ...
99
+ end
100
+ ```
101
+
102
+ ![graph2](https://raw.github.com/wiki/kaihar4/utils_drawer/images/graph2.png)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,38 @@
1
+ require 'utils_drawer/table_drawer/table'
2
+ require 'utils_drawer/table_drawer/row'
3
+ require 'utils_drawer/table_drawer/column_validator'
4
+ require 'utils_drawer/table_drawer/column'
5
+
6
+ require 'utils_drawer/graph_drawer/graph.rb'
7
+ require 'utils_drawer/graph_drawer/data.rb'
8
+
9
+ module UtilsDrawer
10
+ def table(min_column_size = 10)
11
+ @table = TableDrawer::Table.new(min_column_size)
12
+ yield
13
+ @table.to_s
14
+ end
15
+
16
+ def row
17
+ @row = TableDrawer::Row.new
18
+ yield
19
+ @table.add(@row)
20
+ end
21
+
22
+ def column(text, size = nil)
23
+ @column = TableDrawer::Column.new(text, size)
24
+ @row.add(@column)
25
+ end
26
+
27
+ def graph(option = { omission: true })
28
+ @graph = GraphDrawer::Graph.new(option)
29
+ yield
30
+ @graph.datas_init
31
+ @graph.to_s
32
+ end
33
+
34
+ def data(item, value)
35
+ @data = GraphDrawer::Data.new(item, value)
36
+ @graph.add(@data)
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ require 'safe_colorize'
2
+
3
+ module UtilsDrawer
4
+ module GraphDrawer
5
+ class Data
6
+ using SafeColorize
7
+
8
+ attr_accessor :item
9
+ attr_accessor :value
10
+ attr_accessor :level
11
+
12
+ def initialize(item, value)
13
+ @item = item
14
+ @value = value
15
+ @level = nil
16
+ end
17
+
18
+ def to_s
19
+ color =
20
+ case @level
21
+ when 1
22
+ :green
23
+ when 2
24
+ :cyan
25
+ when 3
26
+ :blue
27
+ end
28
+
29
+ if @value.zero?
30
+ draw_str = "#{@item}|"
31
+ else
32
+ bar = ' ' * (@value - @value.to_s.size)
33
+ data_label = @value.to_s.color(:black)
34
+ draw_str = "#{@item}|#{(bar + data_label).bg_color(color)}"
35
+ end
36
+
37
+ draw_str
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,101 @@
1
+ module UtilsDrawer
2
+ module GraphDrawer
3
+ class Graph
4
+ def initialize(option)
5
+ @datas = []
6
+ @max_item_size = 0
7
+ @max_value_size = 0
8
+ @omission = option[:omission]
9
+ end
10
+
11
+ def add(data)
12
+ @datas << data
13
+ @max_item_size = data.item.size if @max_item_size < data.item.size
14
+ @max_value_size = data.value if @max_value_size < data.value
15
+ end
16
+
17
+ def datas_init
18
+ @datas.each do |data|
19
+ # Adjust the data item size
20
+ data.item = adjust_item_size(data)
21
+
22
+ # Set the data level
23
+ data.level = calc_data_level(data)
24
+ end
25
+ end
26
+
27
+ def to_s
28
+ puts separate
29
+
30
+ ommit_flag = false
31
+ omitted = false
32
+
33
+ @datas.each.with_index do |data, index|
34
+ ommit_flag = false if ommit_flag == true && !data.value.zero?
35
+
36
+ # First and Last
37
+ if index.zero? || index == @datas.size - 1
38
+ puts data.to_s
39
+
40
+ # Omission
41
+ elsif ommit_flag == true && omitted == false && @datas[index + 1].value.zero?
42
+ puts draw_omission
43
+
44
+ omitted = true
45
+
46
+ # Omitted already
47
+ elsif ommit_flag == true && omitted == true && @datas[index + 1].value.zero?
48
+ next
49
+
50
+ else
51
+ puts data.to_s
52
+
53
+ omitted = false
54
+ end
55
+
56
+ ommit_flag = true if ommit_flag == false && data.value.zero? && @omission
57
+ end
58
+
59
+ puts separate
60
+ end
61
+
62
+ private
63
+
64
+ def adjust_item_size(data)
65
+ missing_size = @max_item_size - data.item.size
66
+
67
+ # Nothing to do
68
+ return data.item if missing_size.zero?
69
+
70
+ if missing_size.even?
71
+ ' ' * (missing_size / 2) + data.item + ' ' * (missing_size / 2)
72
+ else
73
+ ' ' * (missing_size / 2) + data.item + ' ' * (missing_size / 2 + 1)
74
+ end
75
+ end
76
+
77
+ def calc_data_level(data)
78
+ basis = (@max_value_size / 3).round
79
+ if data.value <= basis * 1
80
+ 1
81
+ elsif data.value <= basis * 2
82
+ 2
83
+ else
84
+ 3
85
+ end
86
+ end
87
+
88
+ def separate
89
+ '=' * (@max_item_size + 1 + (@max_value_size * 2))
90
+ end
91
+
92
+ def draw_omission
93
+ if @max_item_size.even?
94
+ ' ' * (@max_item_size / 2) + '|' + ' ' * (@max_item_size / 2 - 1) + '|'
95
+ else
96
+ ' ' * (@max_item_size / 2) + '|' + ' ' * (@max_item_size / 2) + '|'
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,20 @@
1
+ require 'safe_colorize'
2
+
3
+ module UtilsDrawer
4
+ module TableDrawer
5
+ class Column
6
+ using SafeColorize
7
+ prepend ColumnValidator
8
+
9
+ attr_accessor :text
10
+
11
+ def initialize(text, size)
12
+ # Ignore ANSI escape sequences
13
+ pure_text = text.to_pure
14
+
15
+ text += ' ' * (size - pure_text.size) if !size.nil? && size > pure_text.size
16
+ @text = text
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module UtilsDrawer
2
+ module TableDrawer
3
+ module ColumnValidator
4
+ def initialize(text, size)
5
+ fail 'Invalid multibyte char' if text =~ /[^\x01-\x7E]/
6
+
7
+ super
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module UtilsDrawer
2
+ module TableDrawer
3
+ class Row
4
+ attr_accessor :columns
5
+
6
+ def initialize
7
+ @columns = []
8
+ end
9
+
10
+ def add(column)
11
+ @columns << column
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,85 @@
1
+ require 'safe_colorize'
2
+
3
+ module UtilsDrawer
4
+ module TableDrawer
5
+ class Table
6
+ using SafeColorize
7
+
8
+ def initialize(min_column_size)
9
+ @rows = []
10
+ @header_column_sizes = []
11
+ @min_column_size = min_column_size
12
+ end
13
+
14
+ def add(row)
15
+ # Means the header
16
+ if @rows.size.zero?
17
+ set_header_column_size(row)
18
+
19
+ else
20
+ if row.columns.size != @header_column_sizes.size
21
+ fail "Bud number of columns (#{row.columns.size} for #{@header_column_sizes.size})"
22
+ end
23
+ end
24
+
25
+ @rows << row
26
+ end
27
+
28
+ def to_s
29
+ puts separate
30
+
31
+ @rows.each.with_index do |row, index|
32
+ puts format_line(row)
33
+ puts separate if index != @rows.size - 1
34
+ end
35
+
36
+ puts separate
37
+ end
38
+
39
+ private
40
+
41
+ def set_header_column_size(row)
42
+ row.columns.each.with_index do |column, index|
43
+ # Ignore ANSI escape sequences
44
+ pure_text = column.text.to_pure
45
+
46
+ if pure_text.size > @min_column_size
47
+ @header_column_sizes[index] = pure_text.size
48
+ else
49
+ @header_column_sizes[index] = @min_column_size
50
+ end
51
+ end
52
+ end
53
+
54
+ def format_line(row)
55
+ line = '| '
56
+
57
+ row.columns.each.with_index do |column, index|
58
+ # Ignore ANSI escape sequences
59
+ pure_text = column.text.to_pure
60
+
61
+ if pure_text.size < @header_column_sizes[index]
62
+ line << column.text + ' ' * (@header_column_sizes[index] - pure_text.size)
63
+
64
+ elsif pure_text.size > @header_column_sizes[index]
65
+ line << column.text[0, @header_column_sizes[index] - 3] + '...'
66
+
67
+ else
68
+ line << column.text
69
+ end
70
+
71
+ line << ' | ' if index != row.columns.size - 1
72
+ end
73
+
74
+ line << ' |'
75
+ end
76
+
77
+ def separate
78
+ left = middle = right = '+'
79
+ bar = '-'
80
+
81
+ left + @header_column_sizes.map { |size| bar * (size + 2) }.join(middle) + right
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module UtilsDrawer
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH << File.expand_path('../lib', __FILE__)
2
+ require 'utils_drawer/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'utils_drawer'
6
+ spec.version = UtilsDrawer::VERSION
7
+ spec.authors = ['kaihar4']
8
+ spec.email = ['kaihar4@gmail.com']
9
+ spec.summary = 'Helper for drawing utils on CLI.'
10
+ spec.description = spec.summary
11
+ spec.homepage = 'https://github.com/kaihar4/utils_drawer'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_runtime_dependency 'safe_colorize'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utils_drawer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kaihar4
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: safe_colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Helper for drawing utils on CLI.
56
+ email:
57
+ - kaihar4@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/utils_drawer.rb
68
+ - lib/utils_drawer/graph_drawer/data.rb
69
+ - lib/utils_drawer/graph_drawer/graph.rb
70
+ - lib/utils_drawer/table_drawer/column.rb
71
+ - lib/utils_drawer/table_drawer/column_validator.rb
72
+ - lib/utils_drawer/table_drawer/row.rb
73
+ - lib/utils_drawer/table_drawer/table.rb
74
+ - lib/utils_drawer/version.rb
75
+ - utils_drawer.gemspec
76
+ homepage: https://github.com/kaihar4/utils_drawer
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Helper for drawing utils on CLI.
100
+ test_files: []