kosi 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,15 @@
1
- # encoding: utf-8
2
-
3
- # TableFormat for Terminal(Use Japanese Charactors)
4
- module Kosi
5
- # Options
6
- module OptionKeys
7
- ALIGN = :align
8
- HEADER = :header
9
- HAS_HEADER = :has_header
10
- CONNECTOR_CHAR = :connector_char
11
- HORIZONTAL_BORDER_CHAR = :horizontal_border_char
12
- VERTICAL_BORDER_CHAR = :vertical_border_char
13
- SEPARATE_EACH_ROW = :separate_each_row
14
- end
15
- end
1
+ # encoding: utf-8
2
+
3
+ # TableFormat for Terminal(Use Japanese Charactors)
4
+ module Kosi
5
+ # Options
6
+ module OptionKeys
7
+ ALIGN = :align
8
+ HEADER = :header
9
+ HAS_HEADER = :has_header
10
+ CONNECTOR_CHAR = :connector_char
11
+ HORIZONTAL_BORDER_CHAR = :horizontal_border_char
12
+ VERTICAL_BORDER_CHAR = :vertical_border_char
13
+ SEPARATE_EACH_ROW = :separate_each_row
14
+ end
15
+ end
@@ -1,15 +1,15 @@
1
- # encoding: utf-8
2
- require 'kosi/options'
3
-
4
- # TableFormat for Terminal(Use Japanese Charactors)
5
- module Kosi
6
- # SeparateEachRow
7
- class SeparateEachRow
8
- attr_reader :value
9
- DEFAULT = false
10
-
11
- def initialize(options)
12
- @value = options[OptionKeys::SEPARATE_EACH_ROW] || DEFAULT
13
- end
14
- end
15
- end
1
+ # encoding: utf-8
2
+ require 'kosi/options'
3
+
4
+ # TableFormat for Terminal(Use Japanese Charactors)
5
+ module Kosi
6
+ # SeparateEachRow
7
+ class SeparateEachRow
8
+ attr_reader :value
9
+ DEFAULT = false
10
+
11
+ def initialize(options)
12
+ @value = options[OptionKeys::SEPARATE_EACH_ROW] || DEFAULT
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
- # encoding: utf-8
2
- require 'kosi/validators/each_array_length_validator'
3
- require 'kosi/validators/row_type_validator'
1
+ # encoding: utf-8
2
+ require 'kosi/validators/each_array_length_validator'
3
+ require 'kosi/validators/row_type_validator'
@@ -1,21 +1,21 @@
1
- # encoding: utf-8
2
-
3
- # Kosi
4
- module Kosi
5
- # Validators
6
- module Validators
7
- # EachArrayLength Validator
8
- # if row class is not Array, fail ArgumentError.
9
- class EachArrayLength
10
- INVALID_ARRAY_LENGTH_MESSAGE = \
11
- 'invalid array length.each array must be same length'
12
- def self.validate(row)
13
- row_sizes = row.map(&:size)
14
- return if row_sizes.uniq.size == 0
15
- unless row_sizes.uniq.size == 1
16
- fail ArgumentError, INVALID_ARRAY_LENGTH_MESSAGE
17
- end
18
- end
19
- end
20
- end
21
- end
1
+ # encoding: utf-8
2
+
3
+ # Kosi
4
+ module Kosi
5
+ # Validators
6
+ module Validators
7
+ # EachArrayLength Validator
8
+ # if row class is not Array, fail ArgumentError.
9
+ class EachArrayLength
10
+ INVALID_ARRAY_LENGTH_MESSAGE = \
11
+ 'invalid array length.each array must be same length'
12
+ def self.validate(row)
13
+ row_sizes = row.map(&:size)
14
+ return if row_sizes.uniq.size == 0
15
+ unless row_sizes.uniq.size == 1
16
+ fail ArgumentError, INVALID_ARRAY_LENGTH_MESSAGE
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,19 +1,19 @@
1
- # encoding: utf-8
2
-
3
- # Kosi
4
- module Kosi
5
- # Validators
6
- module Validators
7
- # RowType Validator
8
- # if row class is not Array, fail ArgumentError.
9
- class RowType
10
- INVALID_ROW_MESSAGE = 'invalid row class %s. row class must be Array.'
11
- def self.validate(row)
12
- unless row.is_a?(Array)
13
- message = format(INVALID_ROW_MESSAGE, row.class)
14
- fail ArgumentError, message
15
- end
16
- end
17
- end
18
- end
19
- end
1
+ # encoding: utf-8
2
+
3
+ # Kosi
4
+ module Kosi
5
+ # Validators
6
+ module Validators
7
+ # RowType Validator
8
+ # if row class is not Array, fail ArgumentError.
9
+ class RowType
10
+ INVALID_ROW_MESSAGE = 'invalid row class %s. row class must be Array.'
11
+ def self.validate(row)
12
+ unless row.is_a?(Array)
13
+ message = format(INVALID_ROW_MESSAGE, row.class)
14
+ fail ArgumentError, message
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,4 +1,4 @@
1
- # TableFormat for Terminal(Use Japanese Charactors)
2
- module Kosi
3
- VERSION = '0.0.1'
4
- end
1
+ # TableFormat for Terminal(Use Japanese Charactors)
2
+ module Kosi
3
+ VERSION = '0.0.2'
4
+ end
@@ -1,26 +1,26 @@
1
- require 'kosi/options'
2
- require 'kosi/char_option'
3
-
4
- # TableFormat for Terminal(Use Japanese Charactors)
5
- module Kosi
6
- # VerticalBorderChar
7
- class VerticalBorderChar < CharOption
8
- DEFAULT = '|'
9
- INVALID_CHAR_MSG = \
10
- 'vertical_border_char must be 1 length(%d) char %s'
11
-
12
- def default
13
- DEFAULT
14
- end
15
-
16
- def key
17
- OptionKeys::VERTICAL_BORDER_CHAR
18
- end
19
-
20
- private
21
-
22
- def invalid_char_msg
23
- INVALID_CHAR_MSG
24
- end
25
- end
26
- end
1
+ require 'kosi/options'
2
+ require 'kosi/char_option'
3
+
4
+ # TableFormat for Terminal(Use Japanese Charactors)
5
+ module Kosi
6
+ # VerticalBorderChar
7
+ class VerticalBorderChar < CharOption
8
+ DEFAULT = '|'
9
+ INVALID_CHAR_MSG = \
10
+ 'vertical_border_char must be 1 length(%d) char %s'
11
+
12
+ def default
13
+ DEFAULT
14
+ end
15
+
16
+ def key
17
+ OptionKeys::VERTICAL_BORDER_CHAR
18
+ end
19
+
20
+ private
21
+
22
+ def invalid_char_msg
23
+ INVALID_CHAR_MSG
24
+ end
25
+ end
26
+ end
@@ -1,138 +1,149 @@
1
- # encoding: utf-8
2
- require 'kosi'
3
- require 'kosi/validators'
4
-
5
- # TableFormat for Terminal(Use Japanese Charactors)
6
- module Kosi
7
- # Table
8
- class Table
9
- attr_reader :align, :header, :connector_char, :horizontal_border_char,
10
- :vertical_border_char, :separate_each_row
11
-
12
- def initialize(options = {})
13
- @align = Kosi::Align.new(options)
14
- @header = Kosi::Header.new(options)
15
- @connector_char = Kosi::ConnectorChar.new(options)
16
- @horizontal_border_char = Kosi::HorizontalBorderChar.new(options)
17
- @vertical_border_char = Kosi::VerticalBorderChar.new(options)
18
- @separate_each_row = Kosi::SeparateEachRow.new(options)
19
- end
20
-
21
- def render(rows)
22
- check_rows_type(rows)
23
- check_each_array_length(rows)
24
- header = read_header
25
- max_lengths = get_max_lengths(rows, header)
26
- table_format_rows = get_table_format_rows(rows, max_lengths, header)
27
- table_format_rows.join("\n") + "\n"
28
- end
29
-
30
- private
31
-
32
- def check_rows_type(rows)
33
- Validators::RowType.validate(rows)
34
- rows.each { |row|Validators::RowType.validate(row) }
35
- end
36
-
37
- def check_each_array_length(rows)
38
- Validators::EachArrayLength.validate(rows)
39
- end
40
-
41
- def get_max_lengths(rows, header)
42
- tmp_rows = rows.dup
43
- tmp_rows << header unless @header.empty?
44
- tmp_rows.reduce([]) do |max_lengths, row|
45
- row.each_with_index do |e, i|
46
- s = ascii1_other2_size(String(e))
47
- next unless greater?(s, max_lengths[i])
48
- max_lengths[i] = s
49
- end
50
- max_lengths
51
- end
52
- end
53
-
54
- def greater?(one, other)
55
- return true if other.nil?
56
- one > other
57
- end
58
-
59
- def ascii1_other2_size(column)
60
- column.split('').reduce(0) do |a, e|
61
- a += half?(e) ? 1 : 2
62
- a
63
- end
64
- end
65
-
66
- def half?(char)
67
- case char.ord
68
- when 1..127, 65_377..65_439
69
- true
70
- else
71
- false
72
- end
73
- end
74
-
75
- def get_table_format_rows(rows, max_lengths, header)
76
- top_bottom_line = top_bottom(max_lengths)
77
- results = [top_bottom_line]
78
- append_header(results, header, max_lengths, top_bottom_line)
79
- rows.each do |col|
80
- results += formated_row(col, max_lengths, top_bottom_line)
81
- end
82
- results << top_bottom_line unless @separate_each_row.value
83
- results
84
- end
85
-
86
- def formated_row(col, max_lengths, top_bottom_line)
87
- results = []
88
- row_results = get_columns(col, max_lengths)
89
- virtical = @vertical_border_char.value
90
- results << "#{virtical}#{row_results.join(virtical)}#{virtical}"
91
- results << top_bottom_line if @separate_each_row.value
92
- results
93
- end
94
-
95
- def append_header(results, header, max_lengths, top_bottom_line)
96
- unless @header.empty?
97
- results << formated_header(header, max_lengths)
98
- results << top_bottom_line
99
- end
100
- end
101
-
102
- def top_bottom(max_lengths)
103
- results = max_lengths.reduce([]) do |ret, column_size|
104
- ret << @horizontal_border_char.value * column_size
105
- ret
106
- end
107
- con = @connector_char.value
108
- "#{con}#{results.join(con)}#{con}"
109
- end
110
-
111
- def read_header
112
- return nil if @header.empty?
113
- @header.value
114
- end
115
-
116
- def formated_header(header, max_lengths)
117
- header_results = []
118
- header.size.times do |i|
119
- column = String(header[i])
120
- diff = ascii1_other2_size(column) - column.size
121
- header_results << @align.apply(column, max_lengths[i], diff)
122
- end
123
- virtical = @vertical_border_char.value
124
- header_line = header_results.join(virtical)
125
- "#{virtical}#{header_line}#{virtical}"
126
- end
127
-
128
- def get_columns(columns, max_lengths)
129
- column_results = []
130
- columns.size.times do |i|
131
- column = String(columns[i])
132
- diff = ascii1_other2_size(column) - column.size
133
- column_results << @align.apply(column, max_lengths[i], diff)
134
- end
135
- column_results
136
- end
137
- end
138
- end
1
+ # encoding: utf-8
2
+ require 'kosi'
3
+ require 'kosi/validators'
4
+
5
+ # TableFormat for Terminal(Use Japanese Charactors)
6
+ module Kosi
7
+ # Table
8
+ class Table
9
+ attr_reader :align, :header, :connector_char, :horizontal_border_char,
10
+ :vertical_border_char, :separate_each_row
11
+
12
+ def initialize(options = {})
13
+ @align = Kosi::Align.new(options)
14
+ @header = Kosi::Header.new(options)
15
+ @connector_char = Kosi::ConnectorChar.new(options)
16
+ @horizontal_border_char = Kosi::HorizontalBorderChar.new(options)
17
+ @vertical_border_char = Kosi::VerticalBorderChar.new(options)
18
+ @separate_each_row = Kosi::SeparateEachRow.new(options)
19
+ end
20
+
21
+ def render(rows)
22
+ check_rows_type(rows)
23
+ check_each_array_length(rows)
24
+ header = read_header
25
+ escaped_rows = escape_ansi_escape_sequence(rows)
26
+ max_lengths = get_max_lengths(escaped_rows, header)
27
+ table_format_rows = get_table_format_rows(rows, max_lengths, header)
28
+ table_format_rows.join("\n") + "\n"
29
+ end
30
+
31
+ private
32
+
33
+ def check_rows_type(rows)
34
+ Validators::RowType.validate(rows)
35
+ rows.each { |row|Validators::RowType.validate(row) }
36
+ end
37
+
38
+ def check_each_array_length(rows)
39
+ Validators::EachArrayLength.validate(rows)
40
+ end
41
+
42
+ def get_max_lengths(rows, header)
43
+ tmp_rows = rows.dup
44
+ tmp_rows << header unless @header.empty?
45
+ tmp_rows.reduce([]) do |max_lengths, row|
46
+ row.each_with_index do |e, i|
47
+ s = ascii1_other2_size(String(e))
48
+ next unless greater?(s, max_lengths[i])
49
+ max_lengths[i] = s
50
+ end
51
+ max_lengths
52
+ end
53
+ end
54
+
55
+ def greater?(one, other)
56
+ return true if other.nil?
57
+ one > other
58
+ end
59
+
60
+ def ascii1_other2_size(column)
61
+ column.split('').reduce(0) do |a, e|
62
+ a += half?(e) ? 1 : 2
63
+ a
64
+ end
65
+ end
66
+
67
+ def half?(char)
68
+ case char.ord
69
+ when 1..127, 65_377..65_439
70
+ true
71
+ else
72
+ false
73
+ end
74
+ end
75
+
76
+ def get_table_format_rows(rows, max_lengths, header)
77
+ top_bottom_line = top_bottom(max_lengths)
78
+ results = [top_bottom_line]
79
+ append_header(results, header, max_lengths, top_bottom_line)
80
+ rows.each do |col|
81
+ results += formated_row(col, max_lengths, top_bottom_line)
82
+ end
83
+ results << top_bottom_line unless @separate_each_row.value
84
+ results
85
+ end
86
+
87
+ def formated_row(col, max_lengths, top_bottom_line)
88
+ results = []
89
+ row_results = get_columns(col, max_lengths)
90
+ virtical = @vertical_border_char.value
91
+ results << "#{virtical}#{row_results.join(virtical)}#{virtical}"
92
+ results << top_bottom_line if @separate_each_row.value
93
+ results
94
+ end
95
+
96
+ def append_header(results, header, max_lengths, top_bottom_line)
97
+ unless @header.empty?
98
+ results << formated_header(header, max_lengths)
99
+ results << top_bottom_line
100
+ end
101
+ end
102
+
103
+ def top_bottom(max_lengths)
104
+ results = max_lengths.reduce([]) do |ret, column_size|
105
+ ret << @horizontal_border_char.value * column_size
106
+ ret
107
+ end
108
+ con = @connector_char.value
109
+ "#{con}#{results.join(con)}#{con}"
110
+ end
111
+
112
+ def read_header
113
+ return nil if @header.empty?
114
+ @header.value
115
+ end
116
+
117
+ def escape_ansi_escape_sequence(rows)
118
+ rows.reduce([]) do |r, e|
119
+ r << e.map do |f|f.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
120
+ .gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '')
121
+ .gsub(/(\x03|\x1a)/, '')
122
+ end
123
+ r
124
+ end
125
+ end
126
+
127
+ def formated_header(header, max_lengths)
128
+ header_results = []
129
+ header.size.times do |i|
130
+ column = String(header[i])
131
+ diff = ascii1_other2_size(column) - column.size
132
+ header_results << @align.apply(column, max_lengths[i], diff)
133
+ end
134
+ virtical = @vertical_border_char.value
135
+ header_line = header_results.join(virtical)
136
+ "#{virtical}#{header_line}#{virtical}"
137
+ end
138
+
139
+ def get_columns(columns, max_lengths)
140
+ column_results = []
141
+ columns.size.times do |i|
142
+ column = String(columns[i])
143
+ diff = ascii1_other2_size(column) - column.size
144
+ column_results << @align.apply(column, max_lengths[i], diff)
145
+ end
146
+ column_results
147
+ end
148
+ end
149
+ end