tty-table 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +22 -0
- data/Gemfile +19 -0
- data/LICENSE.txt +22 -0
- data/README.md +389 -0
- data/Rakefile +8 -0
- data/benchmarks/speed.rb +36 -0
- data/lib/tty/table/border/ascii.rb +32 -0
- data/lib/tty/table/border/null.rb +37 -0
- data/lib/tty/table/border/row_line.rb +18 -0
- data/lib/tty/table/border/unicode.rb +32 -0
- data/lib/tty/table/border.rb +219 -0
- data/lib/tty/table/border_dsl.rb +251 -0
- data/lib/tty/table/border_options.rb +53 -0
- data/lib/tty/table/column_set.rb +121 -0
- data/lib/tty/table/columns.rb +170 -0
- data/lib/tty/table/empty.rb +26 -0
- data/lib/tty/table/error.rb +39 -0
- data/lib/tty/table/field.rb +139 -0
- data/lib/tty/table/header.rb +163 -0
- data/lib/tty/table/indentation.rb +52 -0
- data/lib/tty/table/operation/alignment_set.rb +103 -0
- data/lib/tty/table/operation/escape.rb +30 -0
- data/lib/tty/table/operation/filter.rb +34 -0
- data/lib/tty/table/operation/padding.rb +95 -0
- data/lib/tty/table/operation/truncation.rb +41 -0
- data/lib/tty/table/operation/wrapped.rb +43 -0
- data/lib/tty/table/operations.rb +69 -0
- data/lib/tty/table/options.rb +30 -0
- data/lib/tty/table/orientation/horizontal.rb +48 -0
- data/lib/tty/table/orientation/vertical.rb +38 -0
- data/lib/tty/table/orientation.rb +57 -0
- data/lib/tty/table/padder.rb +180 -0
- data/lib/tty/table/renderer/ascii.rb +16 -0
- data/lib/tty/table/renderer/basic.rb +294 -0
- data/lib/tty/table/renderer/color.rb +12 -0
- data/lib/tty/table/renderer/unicode.rb +21 -0
- data/lib/tty/table/renderer.rb +101 -0
- data/lib/tty/table/row.rb +248 -0
- data/lib/tty/table/transformation.rb +39 -0
- data/lib/tty/table/validatable.rb +64 -0
- data/lib/tty/table/version.rb +7 -0
- data/lib/tty/table.rb +469 -0
- data/lib/tty-table.rb +48 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/unit/access_spec.rb +86 -0
- data/spec/unit/add_row_spec.rb +28 -0
- data/spec/unit/border/ascii/rendering_spec.rb +90 -0
- data/spec/unit/border/new_spec.rb +27 -0
- data/spec/unit/border/null/rendering_spec.rb +130 -0
- data/spec/unit/border/options/from_spec.rb +38 -0
- data/spec/unit/border/options/new_spec.rb +14 -0
- data/spec/unit/border/unicode/rendering_spec.rb +63 -0
- data/spec/unit/border_options/new_spec.rb +20 -0
- data/spec/unit/border_options/update_spec.rb +18 -0
- data/spec/unit/column_set/extract_widths_spec.rb +15 -0
- data/spec/unit/column_set/total_width_spec.rb +15 -0
- data/spec/unit/column_set/widths_from_spec.rb +51 -0
- data/spec/unit/columns/enforce_spec.rb +67 -0
- data/spec/unit/columns/widths_spec.rb +35 -0
- data/spec/unit/data_spec.rb +14 -0
- data/spec/unit/each_spec.rb +41 -0
- data/spec/unit/each_with_index_spec.rb +57 -0
- data/spec/unit/empty_spec.rb +23 -0
- data/spec/unit/eql_spec.rb +34 -0
- data/spec/unit/field/equality_spec.rb +51 -0
- data/spec/unit/field/length_spec.rb +21 -0
- data/spec/unit/field/lines_spec.rb +21 -0
- data/spec/unit/field/new_spec.rb +29 -0
- data/spec/unit/field/width_spec.rb +23 -0
- data/spec/unit/filter_spec.rb +23 -0
- data/spec/unit/header/call_spec.rb +30 -0
- data/spec/unit/header/color_spec.rb +19 -0
- data/spec/unit/header/equality_spec.rb +51 -0
- data/spec/unit/header/height_spec.rb +27 -0
- data/spec/unit/header/new_spec.rb +25 -0
- data/spec/unit/header/set_spec.rb +20 -0
- data/spec/unit/header/to_ary_spec.rb +14 -0
- data/spec/unit/header_spec.rb +13 -0
- data/spec/unit/indentation/insert_indent_spec.rb +27 -0
- data/spec/unit/initialize_spec.rb +88 -0
- data/spec/unit/operation/alignment_set/call_spec.rb +39 -0
- data/spec/unit/operation/alignment_set/each_spec.rb +17 -0
- data/spec/unit/operation/alignment_set/new_spec.rb +27 -0
- data/spec/unit/operation/alignment_set/to_ary_spec.rb +14 -0
- data/spec/unit/operation/escape/call_spec.rb +16 -0
- data/spec/unit/operation/filter/call_spec.rb +17 -0
- data/spec/unit/operation/truncation/call_spec.rb +32 -0
- data/spec/unit/operation/wrapped/call_spec.rb +33 -0
- data/spec/unit/operations/new_spec.rb +30 -0
- data/spec/unit/options/access_spec.rb +14 -0
- data/spec/unit/options_spec.rb +25 -0
- data/spec/unit/orientation_spec.rb +145 -0
- data/spec/unit/padder/parse_spec.rb +45 -0
- data/spec/unit/padder/to_s_spec.rb +14 -0
- data/spec/unit/padding_spec.rb +120 -0
- data/spec/unit/properties_spec.rb +25 -0
- data/spec/unit/render_spec.rb +63 -0
- data/spec/unit/render_with_spec.rb +106 -0
- data/spec/unit/renderer/ascii/indentation_spec.rb +41 -0
- data/spec/unit/renderer/ascii/padding_spec.rb +61 -0
- data/spec/unit/renderer/ascii/render_spec.rb +68 -0
- data/spec/unit/renderer/ascii/resizing_spec.rb +114 -0
- data/spec/unit/renderer/ascii/separator_spec.rb +28 -0
- data/spec/unit/renderer/basic/alignment_spec.rb +88 -0
- data/spec/unit/renderer/basic/coloring_spec.rb +46 -0
- data/spec/unit/renderer/basic/extract_column_widths_spec.rb +28 -0
- data/spec/unit/renderer/basic/filter_spec.rb +53 -0
- data/spec/unit/renderer/basic/indentation_spec.rb +48 -0
- data/spec/unit/renderer/basic/multiline_content_spec.rb +135 -0
- data/spec/unit/renderer/basic/new_spec.rb +26 -0
- data/spec/unit/renderer/basic/options_spec.rb +52 -0
- data/spec/unit/renderer/basic/padding_spec.rb +52 -0
- data/spec/unit/renderer/basic/render_spec.rb +57 -0
- data/spec/unit/renderer/basic/resizing_spec.rb +96 -0
- data/spec/unit/renderer/basic/separator_spec.rb +43 -0
- data/spec/unit/renderer/basic/truncation_spec.rb +35 -0
- data/spec/unit/renderer/basic/wrapping_spec.rb +40 -0
- data/spec/unit/renderer/border_spec.rb +104 -0
- data/spec/unit/renderer/render_spec.rb +36 -0
- data/spec/unit/renderer/select_spec.rb +22 -0
- data/spec/unit/renderer/style_spec.rb +72 -0
- data/spec/unit/renderer/unicode/indentation_spec.rb +41 -0
- data/spec/unit/renderer/unicode/padding_spec.rb +61 -0
- data/spec/unit/renderer/unicode/render_spec.rb +68 -0
- data/spec/unit/renderer/unicode/separator_spec.rb +26 -0
- data/spec/unit/renderer_spec.rb +19 -0
- data/spec/unit/rotate_spec.rb +86 -0
- data/spec/unit/row/access_spec.rb +25 -0
- data/spec/unit/row/call_spec.rb +45 -0
- data/spec/unit/row/data_spec.rb +26 -0
- data/spec/unit/row/each_spec.rb +31 -0
- data/spec/unit/row/equality_spec.rb +73 -0
- data/spec/unit/row/height_spec.rb +27 -0
- data/spec/unit/row/new_spec.rb +41 -0
- data/spec/unit/row/to_ary_spec.rb +14 -0
- data/spec/unit/to_s_spec.rb +63 -0
- data/spec/unit/transformation/extract_tuples_spec.rb +35 -0
- data/spec/unit/validatable/validate_options_spec.rb +33 -0
- data/spec/unit/validatable_spec.rb +32 -0
- data/tasks/console.rake +10 -0
- data/tasks/coverage.rake +11 -0
- data/tasks/spec.rake +29 -0
- data/tty-table.gemspec +28 -0
- metadata +371 -0
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
module Operation
|
6
|
+
# A class responsible for escaping special chars in a table field
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Escape
|
10
|
+
|
11
|
+
# Escape special characters in a table field
|
12
|
+
#
|
13
|
+
# @param [TTY::Table::Field] field
|
14
|
+
#
|
15
|
+
# @param [Integer] row
|
16
|
+
# the field row index
|
17
|
+
#
|
18
|
+
# @param [Integer] col
|
19
|
+
# the field column index
|
20
|
+
#
|
21
|
+
# @api public
|
22
|
+
def call(field, row, col)
|
23
|
+
field.value = field.value.to_s.gsub(/(\t|\r|\n)/) do |val|
|
24
|
+
val.dump.gsub('"', '')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end # Escape
|
28
|
+
end # Operation
|
29
|
+
end # Table
|
30
|
+
end # TTY
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
module Operation
|
6
|
+
# A class responsible for transforming table field
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Filter
|
10
|
+
# Initialize a Filter
|
11
|
+
#
|
12
|
+
# @api public
|
13
|
+
def initialize(filter)
|
14
|
+
@filter = filter
|
15
|
+
end
|
16
|
+
|
17
|
+
# Apply filer to the provided table field
|
18
|
+
#
|
19
|
+
# @param [TTY::Table::Field] field
|
20
|
+
#
|
21
|
+
# @param [Integer] row
|
22
|
+
# the field row index
|
23
|
+
#
|
24
|
+
# @param [Integer] col
|
25
|
+
# the field column index
|
26
|
+
#
|
27
|
+
# @api public
|
28
|
+
def call(field, row, col)
|
29
|
+
field.value = @filter.call(field.value, row, col)
|
30
|
+
end
|
31
|
+
end # Filter
|
32
|
+
end # Operation
|
33
|
+
end # Table
|
34
|
+
end # TTY
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
module Operation
|
6
|
+
|
7
|
+
# A class responsible for padding field with whitespace
|
8
|
+
class Padding
|
9
|
+
|
10
|
+
attr_reader :padding_top
|
11
|
+
|
12
|
+
attr_reader :padding_right
|
13
|
+
|
14
|
+
attr_reader :padding_bottom
|
15
|
+
|
16
|
+
attr_reader :padding_left
|
17
|
+
|
18
|
+
attr_reader :padding_width
|
19
|
+
|
20
|
+
attr_reader :multiline
|
21
|
+
|
22
|
+
# Initialize a Padding operation
|
23
|
+
#
|
24
|
+
# @param [TTY::Table::Padder]
|
25
|
+
#
|
26
|
+
# @api public
|
27
|
+
def initialize(padding, multiline)
|
28
|
+
@padding_top = "\n" * padding.top
|
29
|
+
@padding_right = ' ' * padding.right
|
30
|
+
@padding_bottom = "\n" * padding.bottom
|
31
|
+
@padding_left = ' ' * padding.left
|
32
|
+
@padding_width = padding.left + padding.right
|
33
|
+
@multiline = multiline
|
34
|
+
end
|
35
|
+
|
36
|
+
# Apply padding to a field
|
37
|
+
#
|
38
|
+
# @param [TTY::Table::Field] field
|
39
|
+
# the table field
|
40
|
+
#
|
41
|
+
# @param [Integer] row
|
42
|
+
# the field row index
|
43
|
+
#
|
44
|
+
# @param [Integer] col
|
45
|
+
# the field column index
|
46
|
+
#
|
47
|
+
# @return [TTY::Table::Field]
|
48
|
+
#
|
49
|
+
# @api public
|
50
|
+
def call(field, row, col)
|
51
|
+
text = field.value.to_s
|
52
|
+
|
53
|
+
text = multiline ? pad_multi_line(text) : pad_single_line(text)
|
54
|
+
text.insert(0, padding_top).insert(-1, padding_bottom)
|
55
|
+
|
56
|
+
field.value = text
|
57
|
+
end
|
58
|
+
|
59
|
+
# Apply padding to multi line text
|
60
|
+
#
|
61
|
+
# @param [String] text
|
62
|
+
#
|
63
|
+
# @return [String]
|
64
|
+
#
|
65
|
+
# @api private
|
66
|
+
def pad_multi_line(text)
|
67
|
+
text.split("\n", -1).map { |part| pad_around(part) }.join("\n")
|
68
|
+
end
|
69
|
+
|
70
|
+
# Apply padding to single line text
|
71
|
+
#
|
72
|
+
# @param [String] text
|
73
|
+
#
|
74
|
+
# @return [String]
|
75
|
+
#
|
76
|
+
# @api private
|
77
|
+
def pad_single_line(text)
|
78
|
+
pad_around(text.strip)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Apply padding to left and right side of string
|
82
|
+
#
|
83
|
+
# @param [String] text
|
84
|
+
#
|
85
|
+
# @return [String]
|
86
|
+
#
|
87
|
+
# @api private
|
88
|
+
def pad_around(text)
|
89
|
+
text.insert(0, padding_left).insert(-1, padding_right)
|
90
|
+
end
|
91
|
+
|
92
|
+
end # Padding
|
93
|
+
end # Operation
|
94
|
+
end # Table
|
95
|
+
end # TTY
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
module Operation
|
6
|
+
# A class responsible for shortening text.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Truncation
|
10
|
+
|
11
|
+
attr_reader :widths
|
12
|
+
|
13
|
+
# Initialize a Truncation
|
14
|
+
#
|
15
|
+
# @api public
|
16
|
+
def initialize(widths)
|
17
|
+
@widths = widths
|
18
|
+
end
|
19
|
+
|
20
|
+
# Apply truncation to a field
|
21
|
+
#
|
22
|
+
# @param [TTY::Table::Field] field
|
23
|
+
# the table field
|
24
|
+
#
|
25
|
+
# @param [Integer] row
|
26
|
+
# the field row index
|
27
|
+
#
|
28
|
+
# @param [Integer] col
|
29
|
+
# the field column index
|
30
|
+
#
|
31
|
+
# @return [TTY::Table::Field]
|
32
|
+
#
|
33
|
+
# @api public
|
34
|
+
def call(field, row, col)
|
35
|
+
width = widths[col] || field.width
|
36
|
+
field.value = Verse.truncate(field.value, width)
|
37
|
+
end
|
38
|
+
end # Truncation
|
39
|
+
end # Operation
|
40
|
+
end # Table
|
41
|
+
end # TTY
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
module Operation
|
6
|
+
# A class responsible for wrapping text.
|
7
|
+
#
|
8
|
+
# @api private
|
9
|
+
class Wrapped
|
10
|
+
attr_reader :widths
|
11
|
+
|
12
|
+
attr_reader :padding
|
13
|
+
|
14
|
+
# Initialize a Wrapped
|
15
|
+
#
|
16
|
+
# @api public
|
17
|
+
def initialize(widths, padding)
|
18
|
+
@widths = widths
|
19
|
+
@padding = padding.padding
|
20
|
+
end
|
21
|
+
|
22
|
+
# Apply wrapping to a field
|
23
|
+
#
|
24
|
+
# @param [TTY::Table::Field] field
|
25
|
+
# the table field
|
26
|
+
#
|
27
|
+
# @param [Integer] row
|
28
|
+
# the field row index
|
29
|
+
#
|
30
|
+
# @param [Integer] col
|
31
|
+
# the field column index
|
32
|
+
#
|
33
|
+
# @return [Array[String]]
|
34
|
+
#
|
35
|
+
# @api public
|
36
|
+
def call(field, row, col)
|
37
|
+
width = widths[col] || field.width
|
38
|
+
field.value = Verse.wrap(field.value, width, padding: padding)
|
39
|
+
end
|
40
|
+
end # Wrapped
|
41
|
+
end # Operation
|
42
|
+
end # Table
|
43
|
+
end # TTY
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
# A class holding table field operations.
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
class Operations
|
9
|
+
# The table
|
10
|
+
#
|
11
|
+
# @api private
|
12
|
+
attr_reader :table
|
13
|
+
private :table
|
14
|
+
|
15
|
+
# Available operations
|
16
|
+
#
|
17
|
+
# @return [Hash]
|
18
|
+
#
|
19
|
+
# @api public
|
20
|
+
attr_reader :operations
|
21
|
+
|
22
|
+
# Initialize Operations
|
23
|
+
#
|
24
|
+
# @param [TTY::Table] table
|
25
|
+
# the table to perform operations on
|
26
|
+
#
|
27
|
+
# @return [Object]
|
28
|
+
#
|
29
|
+
# @api public
|
30
|
+
def initialize(table)
|
31
|
+
@table = table
|
32
|
+
@operations = Hash.new { |hash, key| hash[key] = [] }
|
33
|
+
end
|
34
|
+
|
35
|
+
# Add operation
|
36
|
+
#
|
37
|
+
# @param [Symbol] operation_type
|
38
|
+
# the operation type
|
39
|
+
# @param [Object] object
|
40
|
+
# the callable object
|
41
|
+
#
|
42
|
+
# @return [Hash]
|
43
|
+
#
|
44
|
+
# @api public
|
45
|
+
def add(operation_type, object)
|
46
|
+
operations[operation_type] << object
|
47
|
+
end
|
48
|
+
|
49
|
+
# Apply operations to a table row
|
50
|
+
#
|
51
|
+
# @param [Array[Symbol]] types
|
52
|
+
# the operation types
|
53
|
+
# @param [Hash] options
|
54
|
+
# the options for the row
|
55
|
+
#
|
56
|
+
# @return [TTY::Table]
|
57
|
+
#
|
58
|
+
# @api public
|
59
|
+
def run_operations(*args)
|
60
|
+
operation_types = args
|
61
|
+
table.each_with_index do |val, row, col|
|
62
|
+
operation_types.each do |type|
|
63
|
+
operations[type].each { |op| op.call(val, row, col) }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end # Operations
|
68
|
+
end # Table
|
69
|
+
end # TTY
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
|
5
|
+
module TTY
|
6
|
+
class Table
|
7
|
+
|
8
|
+
# Structure for holding table options with indifferent access
|
9
|
+
class Options < DelegateClass(Hash)
|
10
|
+
|
11
|
+
def initialize(hash={}, &block)
|
12
|
+
super(&block)
|
13
|
+
|
14
|
+
hash.each do |key, value|
|
15
|
+
self[convert_key(key)] = valu
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def []=(key, value)
|
20
|
+
super(convert_key(key), value)
|
21
|
+
end
|
22
|
+
|
23
|
+
def convert_key(key)
|
24
|
+
key.is_a?(Symbol) ? key.to_s : key
|
25
|
+
end
|
26
|
+
|
27
|
+
end # Options
|
28
|
+
|
29
|
+
end # Table
|
30
|
+
end # TTY
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
# A class representing table orientation
|
6
|
+
class Orientation
|
7
|
+
# A class responsible for horizontal table transformation
|
8
|
+
class Horizontal < Orientation
|
9
|
+
# Rotate table horizontally
|
10
|
+
#
|
11
|
+
# @param [Table] table
|
12
|
+
#
|
13
|
+
# @return [nil]
|
14
|
+
#
|
15
|
+
# @api public
|
16
|
+
def transform(table)
|
17
|
+
table.rotate_horizontal
|
18
|
+
end
|
19
|
+
|
20
|
+
# Slice vertical table data into horizontal
|
21
|
+
#
|
22
|
+
# @param [Table] table
|
23
|
+
#
|
24
|
+
# @api public
|
25
|
+
def slice(table)
|
26
|
+
head, body, array_h, array_b = 4.times.map { [] }
|
27
|
+
index = 0
|
28
|
+
first_column = 0
|
29
|
+
second_column = 1
|
30
|
+
|
31
|
+
(0...table.original_columns * table.original_rows).each do |col_index|
|
32
|
+
row = table.rows[index]
|
33
|
+
array_h += [row[first_column]]
|
34
|
+
array_b += [row[second_column]]
|
35
|
+
|
36
|
+
if col_index % table.original_columns == 2
|
37
|
+
head << array_h
|
38
|
+
body << array_b
|
39
|
+
array_h, array_b = [], []
|
40
|
+
end
|
41
|
+
index += 1
|
42
|
+
end
|
43
|
+
[head, body]
|
44
|
+
end
|
45
|
+
end # Horizontal
|
46
|
+
end # Orientation
|
47
|
+
end # Table
|
48
|
+
end # TTY
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
# A class representing table orientation
|
6
|
+
class Orientation
|
7
|
+
# A class responsible for vertical table transformation
|
8
|
+
class Vertical < Orientation
|
9
|
+
# Rotate table vertically
|
10
|
+
#
|
11
|
+
# @param [Table] table
|
12
|
+
#
|
13
|
+
# @return [nil]
|
14
|
+
#
|
15
|
+
# @api public
|
16
|
+
def transform(table)
|
17
|
+
table.rotate_vertical
|
18
|
+
end
|
19
|
+
|
20
|
+
# Slice horizontal table data into vertical
|
21
|
+
#
|
22
|
+
# @param [Table] table
|
23
|
+
#
|
24
|
+
# @api public
|
25
|
+
def slice(table)
|
26
|
+
header = table.header
|
27
|
+
row_size = table.row_size
|
28
|
+
|
29
|
+
head = header ? header : (0..row_size).map { |n| (n + 1).to_s }
|
30
|
+
|
31
|
+
(0...row_size).reduce([]) do |array, index|
|
32
|
+
array + head.zip(table.rows[index]).map { |row| table.to_row(row) }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end # Vertical
|
36
|
+
end # Orientation
|
37
|
+
end # Table
|
38
|
+
end # TTY
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
# A class representing table orientation
|
6
|
+
#
|
7
|
+
# @api private
|
8
|
+
class Orientation
|
9
|
+
# The name for the orientation
|
10
|
+
#
|
11
|
+
# @api public
|
12
|
+
attr_reader :name
|
13
|
+
|
14
|
+
# Initialize an Orientation
|
15
|
+
#
|
16
|
+
# @api public
|
17
|
+
def initialize(name)
|
18
|
+
@name = name
|
19
|
+
end
|
20
|
+
|
21
|
+
# Coerce the name argument into an orientation
|
22
|
+
#
|
23
|
+
# @param [Symbol] name
|
24
|
+
#
|
25
|
+
# @api public
|
26
|
+
def self.coerce(name)
|
27
|
+
case name.to_s
|
28
|
+
when /h|horiz(ontal)?/i
|
29
|
+
Horizontal.new :horizontal
|
30
|
+
when /v|ert(ical)?/i
|
31
|
+
Vertical.new :vertical
|
32
|
+
else
|
33
|
+
fail InvalidOrientationError,
|
34
|
+
'orientation must be one of :horizontal, :vertical'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Check if orientation is vertical
|
39
|
+
#
|
40
|
+
# @return [Boolean]
|
41
|
+
#
|
42
|
+
# @api public
|
43
|
+
def vertical?
|
44
|
+
name == :vertical
|
45
|
+
end
|
46
|
+
|
47
|
+
# Check if orientation is horizontal
|
48
|
+
#
|
49
|
+
# @return [Boolean]
|
50
|
+
#
|
51
|
+
# @api public
|
52
|
+
def horizontal?
|
53
|
+
name == :horizontal
|
54
|
+
end
|
55
|
+
end # Orientation
|
56
|
+
end # Table
|
57
|
+
end # TTY
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
# A class responsible for processing table field padding
|
6
|
+
#
|
7
|
+
# Used internally by {Table::Renderer}
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
class Padder
|
11
|
+
include Equatable
|
12
|
+
|
13
|
+
# Padding for the table cells
|
14
|
+
#
|
15
|
+
# @return [Array[Integer]]
|
16
|
+
attr_reader :padding
|
17
|
+
|
18
|
+
# Initialize a Padder
|
19
|
+
#
|
20
|
+
# @api public
|
21
|
+
def initialize(padding)
|
22
|
+
@padding = padding
|
23
|
+
end
|
24
|
+
|
25
|
+
# Parse padding options
|
26
|
+
#
|
27
|
+
# Turn possible values into 4 element array
|
28
|
+
#
|
29
|
+
# @example
|
30
|
+
# padder = TTY::Table::Padder.parse(5)
|
31
|
+
# padder.padding # => [5, 5, 5, 5]
|
32
|
+
#
|
33
|
+
# @param [Object] value
|
34
|
+
#
|
35
|
+
# @return [TTY::Padder]
|
36
|
+
# the new padder with padding values
|
37
|
+
#
|
38
|
+
# @api public
|
39
|
+
def self.parse(value = nil)
|
40
|
+
return value if value.is_a?(self)
|
41
|
+
|
42
|
+
new(convert_to_ary(value))
|
43
|
+
end
|
44
|
+
|
45
|
+
# Convert value to 4 element array
|
46
|
+
#
|
47
|
+
# @return [Array[Integer]]
|
48
|
+
# the 4 element padding array
|
49
|
+
#
|
50
|
+
# @api private
|
51
|
+
def self.convert_to_ary(value)
|
52
|
+
if value.class <= Numeric
|
53
|
+
[value, value, value, value]
|
54
|
+
elsif value.nil?
|
55
|
+
[]
|
56
|
+
elsif value.size == 2
|
57
|
+
[value[0], value[1], value[0], value[1]]
|
58
|
+
elsif value.size == 4
|
59
|
+
value
|
60
|
+
else
|
61
|
+
fail ArgumentError, 'Wrong :padding parameter, must be an array'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Top padding
|
66
|
+
#
|
67
|
+
# @return [Integer]
|
68
|
+
#
|
69
|
+
# @api public
|
70
|
+
def top
|
71
|
+
@padding[0].to_i
|
72
|
+
end
|
73
|
+
|
74
|
+
# Set top padding
|
75
|
+
#
|
76
|
+
# @param [Integer] val
|
77
|
+
#
|
78
|
+
# @return [nil]
|
79
|
+
#
|
80
|
+
# @api public
|
81
|
+
def top=(value)
|
82
|
+
@padding[0] = value
|
83
|
+
end
|
84
|
+
|
85
|
+
# Right padding
|
86
|
+
#
|
87
|
+
# @return [Integer]
|
88
|
+
#
|
89
|
+
# @api public
|
90
|
+
def right
|
91
|
+
@padding[1].to_i
|
92
|
+
end
|
93
|
+
|
94
|
+
# Set right padding
|
95
|
+
#
|
96
|
+
# @param [Integer] val
|
97
|
+
#
|
98
|
+
# @api public
|
99
|
+
def right=(value)
|
100
|
+
@padding[1] = value
|
101
|
+
end
|
102
|
+
|
103
|
+
# Bottom padding
|
104
|
+
#
|
105
|
+
# @return [Integer]
|
106
|
+
#
|
107
|
+
# @api public
|
108
|
+
def bottom
|
109
|
+
@padding[2].to_i
|
110
|
+
end
|
111
|
+
|
112
|
+
# Set bottom padding
|
113
|
+
#
|
114
|
+
# @param [Integer] value
|
115
|
+
#
|
116
|
+
# @return [nil]
|
117
|
+
#
|
118
|
+
# @api public
|
119
|
+
def bottom=(value)
|
120
|
+
@padding[2] = value
|
121
|
+
end
|
122
|
+
|
123
|
+
# Left padding
|
124
|
+
#
|
125
|
+
# @return [Integer]
|
126
|
+
#
|
127
|
+
# @api public
|
128
|
+
def left
|
129
|
+
@padding[3].to_i
|
130
|
+
end
|
131
|
+
|
132
|
+
# Set left padding
|
133
|
+
#
|
134
|
+
# @param [Integer] value
|
135
|
+
#
|
136
|
+
# @return [nil]
|
137
|
+
#
|
138
|
+
# @api public
|
139
|
+
def left=(value)
|
140
|
+
@padding[3] = value
|
141
|
+
end
|
142
|
+
|
143
|
+
# Check if padding is set
|
144
|
+
#
|
145
|
+
# @return [Boolean]
|
146
|
+
#
|
147
|
+
# @api public
|
148
|
+
def empty?
|
149
|
+
padding.empty?
|
150
|
+
end
|
151
|
+
|
152
|
+
# Check if vertical padding is applied
|
153
|
+
#
|
154
|
+
# @return [Boolean]
|
155
|
+
#
|
156
|
+
# @api public
|
157
|
+
def vertical?
|
158
|
+
top.nonzero? || bottom.nonzero?
|
159
|
+
end
|
160
|
+
|
161
|
+
# Check if horizontal padding is applied
|
162
|
+
#
|
163
|
+
# @return [Boolean]
|
164
|
+
#
|
165
|
+
# @api public
|
166
|
+
def horizontal?
|
167
|
+
left.nonzero? || right.nonzero?
|
168
|
+
end
|
169
|
+
|
170
|
+
# String represenation of this padder with padding values
|
171
|
+
#
|
172
|
+
# @return [String]
|
173
|
+
#
|
174
|
+
# @api public
|
175
|
+
def to_s
|
176
|
+
inspect
|
177
|
+
end
|
178
|
+
end # Padder
|
179
|
+
end # Table
|
180
|
+
end # TTY
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TTY
|
4
|
+
class Table
|
5
|
+
class Renderer
|
6
|
+
class ASCII < Basic
|
7
|
+
# Create ASCII renderer
|
8
|
+
#
|
9
|
+
# @api private
|
10
|
+
def initialize(table, options = {})
|
11
|
+
super(table, options.merge(border_class: TTY::Table::Border::ASCII))
|
12
|
+
end
|
13
|
+
end # ASCII
|
14
|
+
end # Renderer
|
15
|
+
end # Table
|
16
|
+
end # TTY
|