tty 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/CHANGELOG.md +5 -0
- data/README.md +42 -306
- data/lib/tty/{text → shell}/distance.rb +5 -27
- data/lib/tty/shell/suggestion.rb +42 -22
- data/lib/tty/version.rb +1 -1
- data/lib/tty.rb +2 -42
- data/spec/tty/{text → shell}/distance/distance_spec.rb +4 -4
- data/spec/tty/shell/distance/initialize_spec.rb +14 -0
- data/tty.gemspec +4 -3
- metadata +37 -269
- data/benchmarks/table.rb +0 -40
- data/lib/tty/table/border/ascii.rb +0 -32
- data/lib/tty/table/border/null.rb +0 -37
- data/lib/tty/table/border/row_line.rb +0 -18
- data/lib/tty/table/border/unicode.rb +0 -32
- data/lib/tty/table/border.rb +0 -222
- data/lib/tty/table/border_dsl.rb +0 -251
- data/lib/tty/table/border_options.rb +0 -53
- data/lib/tty/table/column_set.rb +0 -121
- data/lib/tty/table/columns.rb +0 -170
- data/lib/tty/table/error.rb +0 -18
- data/lib/tty/table/field.rb +0 -139
- data/lib/tty/table/header.rb +0 -133
- data/lib/tty/table/indentation.rb +0 -52
- data/lib/tty/table/operation/alignment.rb +0 -107
- data/lib/tty/table/operation/alignment_set.rb +0 -77
- data/lib/tty/table/operation/escape.rb +0 -30
- data/lib/tty/table/operation/filter.rb +0 -34
- data/lib/tty/table/operation/padding.rb +0 -95
- data/lib/tty/table/operation/truncation.rb +0 -55
- data/lib/tty/table/operation/wrapped.rb +0 -58
- data/lib/tty/table/operations.rb +0 -69
- data/lib/tty/table/orientation/horizontal.rb +0 -48
- data/lib/tty/table/orientation/vertical.rb +0 -38
- data/lib/tty/table/orientation.rb +0 -57
- data/lib/tty/table/padder.rb +0 -180
- data/lib/tty/table/renderer/ascii.rb +0 -16
- data/lib/tty/table/renderer/basic.rb +0 -294
- data/lib/tty/table/renderer/color.rb +0 -12
- data/lib/tty/table/renderer/unicode.rb +0 -21
- data/lib/tty/table/renderer.rb +0 -101
- data/lib/tty/table/row.rb +0 -217
- data/lib/tty/table/transformation.rb +0 -39
- data/lib/tty/table/validatable.rb +0 -64
- data/lib/tty/table.rb +0 -476
- data/lib/tty/text/truncation.rb +0 -99
- data/lib/tty/text/wrapping.rb +0 -118
- data/lib/tty/text.rb +0 -98
- data/spec/tty/table/access_spec.rb +0 -86
- data/spec/tty/table/add_row_spec.rb +0 -28
- data/spec/tty/table/border/ascii/rendering_spec.rb +0 -90
- data/spec/tty/table/border/new_spec.rb +0 -27
- data/spec/tty/table/border/null/rendering_spec.rb +0 -130
- data/spec/tty/table/border/options/from_spec.rb +0 -38
- data/spec/tty/table/border/options/new_spec.rb +0 -14
- data/spec/tty/table/border/unicode/rendering_spec.rb +0 -63
- data/spec/tty/table/border_options/new_spec.rb +0 -20
- data/spec/tty/table/border_options/update_spec.rb +0 -18
- data/spec/tty/table/column_set/extract_widths_spec.rb +0 -15
- data/spec/tty/table/column_set/total_width_spec.rb +0 -15
- data/spec/tty/table/column_set/widths_from_spec.rb +0 -51
- data/spec/tty/table/columns/enforce_spec.rb +0 -68
- data/spec/tty/table/columns/widths_spec.rb +0 -35
- data/spec/tty/table/data_spec.rb +0 -14
- data/spec/tty/table/each_spec.rb +0 -41
- data/spec/tty/table/each_with_index_spec.rb +0 -57
- data/spec/tty/table/empty_spec.rb +0 -23
- data/spec/tty/table/eql_spec.rb +0 -34
- data/spec/tty/table/field/equality_spec.rb +0 -51
- data/spec/tty/table/field/length_spec.rb +0 -21
- data/spec/tty/table/field/lines_spec.rb +0 -21
- data/spec/tty/table/field/new_spec.rb +0 -29
- data/spec/tty/table/field/width_spec.rb +0 -23
- data/spec/tty/table/filter_spec.rb +0 -23
- data/spec/tty/table/header/call_spec.rb +0 -30
- data/spec/tty/table/header/height_spec.rb +0 -27
- data/spec/tty/table/header/new_spec.rb +0 -25
- data/spec/tty/table/header/set_spec.rb +0 -20
- data/spec/tty/table/header/to_ary_spec.rb +0 -14
- data/spec/tty/table/header_spec.rb +0 -13
- data/spec/tty/table/indentation/insert_indent_spec.rb +0 -27
- data/spec/tty/table/initialize_spec.rb +0 -88
- data/spec/tty/table/operation/alignment/format_spec.rb +0 -47
- data/spec/tty/table/operation/alignment/new_spec.rb +0 -31
- data/spec/tty/table/operation/alignment_set/call_spec.rb +0 -39
- data/spec/tty/table/operation/alignment_set/each_spec.rb +0 -17
- data/spec/tty/table/operation/alignment_set/new_spec.rb +0 -27
- data/spec/tty/table/operation/alignment_set/to_ary_spec.rb +0 -14
- data/spec/tty/table/operation/escape/call_spec.rb +0 -16
- data/spec/tty/table/operation/filter/call_spec.rb +0 -17
- data/spec/tty/table/operation/truncation/call_spec.rb +0 -32
- data/spec/tty/table/operation/truncation/truncate_spec.rb +0 -22
- data/spec/tty/table/operation/wrapped/call_spec.rb +0 -33
- data/spec/tty/table/operation/wrapped/wrap_spec.rb +0 -23
- data/spec/tty/table/operations/new_spec.rb +0 -30
- data/spec/tty/table/options_spec.rb +0 -25
- data/spec/tty/table/orientation_spec.rb +0 -145
- data/spec/tty/table/padder/parse_spec.rb +0 -45
- data/spec/tty/table/padder/to_s_spec.rb +0 -14
- data/spec/tty/table/padding_spec.rb +0 -120
- data/spec/tty/table/properties_spec.rb +0 -25
- data/spec/tty/table/render_spec.rb +0 -63
- data/spec/tty/table/render_with_spec.rb +0 -106
- data/spec/tty/table/renderer/ascii/indentation_spec.rb +0 -41
- data/spec/tty/table/renderer/ascii/padding_spec.rb +0 -61
- data/spec/tty/table/renderer/ascii/render_spec.rb +0 -68
- data/spec/tty/table/renderer/ascii/resizing_spec.rb +0 -114
- data/spec/tty/table/renderer/ascii/separator_spec.rb +0 -28
- data/spec/tty/table/renderer/basic/alignment_spec.rb +0 -88
- data/spec/tty/table/renderer/basic/coloring_spec.rb +0 -46
- data/spec/tty/table/renderer/basic/extract_column_widths_spec.rb +0 -28
- data/spec/tty/table/renderer/basic/filter_spec.rb +0 -53
- data/spec/tty/table/renderer/basic/indentation_spec.rb +0 -48
- data/spec/tty/table/renderer/basic/multiline_content_spec.rb +0 -135
- data/spec/tty/table/renderer/basic/new_spec.rb +0 -22
- data/spec/tty/table/renderer/basic/options_spec.rb +0 -48
- data/spec/tty/table/renderer/basic/padding_spec.rb +0 -52
- data/spec/tty/table/renderer/basic/render_spec.rb +0 -57
- data/spec/tty/table/renderer/basic/resizing_spec.rb +0 -96
- data/spec/tty/table/renderer/basic/separator_spec.rb +0 -43
- data/spec/tty/table/renderer/basic/truncation_spec.rb +0 -35
- data/spec/tty/table/renderer/basic/wrapping_spec.rb +0 -40
- data/spec/tty/table/renderer/basic_spec.rb +0 -0
- data/spec/tty/table/renderer/border_spec.rb +0 -104
- data/spec/tty/table/renderer/render_spec.rb +0 -36
- data/spec/tty/table/renderer/select_spec.rb +0 -22
- data/spec/tty/table/renderer/style_spec.rb +0 -72
- data/spec/tty/table/renderer/unicode/indentation_spec.rb +0 -41
- data/spec/tty/table/renderer/unicode/padding_spec.rb +0 -61
- data/spec/tty/table/renderer/unicode/render_spec.rb +0 -68
- data/spec/tty/table/renderer/unicode/separator_spec.rb +0 -26
- data/spec/tty/table/renderer_spec.rb +0 -19
- data/spec/tty/table/rotate_spec.rb +0 -86
- data/spec/tty/table/row/access_spec.rb +0 -25
- data/spec/tty/table/row/call_spec.rb +0 -41
- data/spec/tty/table/row/data_spec.rb +0 -26
- data/spec/tty/table/row/each_spec.rb +0 -31
- data/spec/tty/table/row/equality_spec.rb +0 -73
- data/spec/tty/table/row/height_spec.rb +0 -27
- data/spec/tty/table/row/new_spec.rb +0 -41
- data/spec/tty/table/row/to_ary_spec.rb +0 -14
- data/spec/tty/table/to_s_spec.rb +0 -63
- data/spec/tty/table/transformation/extract_tuples_spec.rb +0 -35
- data/spec/tty/table/validatable/validate_options_spec.rb +0 -33
- data/spec/tty/table/validatable_spec.rb +0 -32
- data/spec/tty/text/distance/initialize_spec.rb +0 -14
- data/spec/tty/text/distance_spec.rb +0 -11
- data/spec/tty/text/truncate_spec.rb +0 -12
- data/spec/tty/text/truncation/initialize_spec.rb +0 -29
- data/spec/tty/text/truncation/truncate_spec.rb +0 -94
- data/spec/tty/text/wrap_spec.rb +0 -13
- data/spec/tty/text/wrapping/initialize_spec.rb +0 -25
- data/spec/tty/text/wrapping/wrap_spec.rb +0 -97
data/lib/tty/table/columns.rb
DELETED
@@ -1,170 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Table
|
5
|
-
# A class responsible for enforcing column constraints.
|
6
|
-
#
|
7
|
-
# Used internally by {Renderer::Basic} to enforce correct column widths.
|
8
|
-
#
|
9
|
-
# @api private
|
10
|
-
class Columns
|
11
|
-
|
12
|
-
attr_reader :table
|
13
|
-
|
14
|
-
attr_reader :renderer
|
15
|
-
|
16
|
-
MIN_WIDTH = 1
|
17
|
-
|
18
|
-
BORDER_WIDTH = 1
|
19
|
-
|
20
|
-
# Initialize a Columns
|
21
|
-
#
|
22
|
-
# @param [TTY::Table::Renderer] renderer
|
23
|
-
#
|
24
|
-
# @api public
|
25
|
-
def initialize(renderer)
|
26
|
-
@renderer = renderer
|
27
|
-
@table = renderer.table
|
28
|
-
end
|
29
|
-
|
30
|
-
# Estimate outside border size
|
31
|
-
#
|
32
|
-
# @return [Integer]
|
33
|
-
#
|
34
|
-
# @api public
|
35
|
-
def outside_border_size
|
36
|
-
renderer.border_class == TTY::Table::Border::Null ? 0 : 2
|
37
|
-
end
|
38
|
-
|
39
|
-
# Total border size
|
40
|
-
#
|
41
|
-
# @return [Integer]
|
42
|
-
#
|
43
|
-
# @api public
|
44
|
-
def border_size
|
45
|
-
BORDER_WIDTH * (table.column_size - 1) + outside_border_size
|
46
|
-
end
|
47
|
-
|
48
|
-
# Estimate minimum table width to be able to display content
|
49
|
-
#
|
50
|
-
# @return [Integer]
|
51
|
-
#
|
52
|
-
# @api public
|
53
|
-
def minimum_width
|
54
|
-
table.column_size * MIN_WIDTH + border_size
|
55
|
-
end
|
56
|
-
|
57
|
-
# Return column's natural unconstrained widths
|
58
|
-
#
|
59
|
-
# @return [Integer]
|
60
|
-
#
|
61
|
-
# @api public
|
62
|
-
def natural_width
|
63
|
-
renderer.column_widths.inject(0, &:+) + border_size
|
64
|
-
end
|
65
|
-
|
66
|
-
# Return the constrained column widths.
|
67
|
-
#
|
68
|
-
# Account for table field widths and any user defined
|
69
|
-
# constraints on the table width.
|
70
|
-
#
|
71
|
-
# @api public
|
72
|
-
def enforce
|
73
|
-
assert_minimum_width
|
74
|
-
|
75
|
-
unless renderer.padding.empty?
|
76
|
-
renderer.column_widths = adjust_padding
|
77
|
-
end
|
78
|
-
|
79
|
-
if natural_width <= renderer.width
|
80
|
-
renderer.column_widths = expand if renderer.resize
|
81
|
-
else
|
82
|
-
if renderer.resize
|
83
|
-
renderer.column_widths = shrink
|
84
|
-
else
|
85
|
-
rotate
|
86
|
-
renderer.column_widths = ColumnSet.widths_from(table)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
# Adjust column widths to account for padding whitespace
|
92
|
-
#
|
93
|
-
# @api private
|
94
|
-
def adjust_padding
|
95
|
-
padding = renderer.padding
|
96
|
-
column_size = table.column_size
|
97
|
-
|
98
|
-
(0...column_size).reduce([]) do |lengths, col|
|
99
|
-
lengths + [padding.left + renderer.column_widths[col] + padding.right]
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
# Rotate table to vertical orientation and print information to stdout
|
104
|
-
#
|
105
|
-
# @api private
|
106
|
-
def rotate
|
107
|
-
TTY.shell.warn 'The table size exceeds the currently set width.' \
|
108
|
-
'To avoid error either. Defaulting to vertical ' \
|
109
|
-
'orientation.'
|
110
|
-
table.orientation = :vertical
|
111
|
-
table.rotate
|
112
|
-
end
|
113
|
-
|
114
|
-
# Expand column widths to match the requested width
|
115
|
-
#
|
116
|
-
# @api private
|
117
|
-
def expand
|
118
|
-
column_size = table.column_size
|
119
|
-
ratio = ((renderer.width - natural_width) / column_size.to_f).floor
|
120
|
-
|
121
|
-
widths = (0...column_size).reduce([]) do |lengths, col|
|
122
|
-
lengths + [renderer.column_widths[col] + ratio]
|
123
|
-
end
|
124
|
-
distribute_extra_width(widths)
|
125
|
-
end
|
126
|
-
|
127
|
-
# Shrink column widths to match the requested width
|
128
|
-
#
|
129
|
-
# @api private
|
130
|
-
def shrink
|
131
|
-
column_size = table.column_size
|
132
|
-
ratio = ((natural_width - renderer.width) / column_size.to_f).ceil
|
133
|
-
|
134
|
-
widths = (0...column_size).reduce([]) do |lengths, col|
|
135
|
-
lengths + [renderer.column_widths[col] - ratio]
|
136
|
-
end
|
137
|
-
distribute_extra_width(widths)
|
138
|
-
end
|
139
|
-
|
140
|
-
# Assert minimum width for the table content
|
141
|
-
#
|
142
|
-
# @raise [TTY::ResizeError]
|
143
|
-
#
|
144
|
-
# @api private
|
145
|
-
def assert_minimum_width
|
146
|
-
width = renderer.width
|
147
|
-
return unless width <= minimum_width
|
148
|
-
fail ResizeError, "Table's width is too small to contain the content " \
|
149
|
-
"(min width #{minimum_width}, currently set #{width})"
|
150
|
-
end
|
151
|
-
|
152
|
-
# Distribute remaining width to meet the total width requirement.
|
153
|
-
#
|
154
|
-
# @param [Array[Integer]] widths
|
155
|
-
#
|
156
|
-
# @api private
|
157
|
-
def distribute_extra_width(widths)
|
158
|
-
column_size = table.column_size
|
159
|
-
extra_width = renderer.width - (widths.reduce(:+) + border_size)
|
160
|
-
per_field_width = extra_width / column_size
|
161
|
-
remaining_width = extra_width % column_size
|
162
|
-
extra = [1] * remaining_width + [0] * (column_size - remaining_width)
|
163
|
-
|
164
|
-
widths.map.with_index do |width, index|
|
165
|
-
width + per_field_width + extra[index]
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end # Columns
|
169
|
-
end # Table
|
170
|
-
end # TTY
|
data/lib/tty/table/error.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Table
|
5
|
-
# Raised when inserting into table with a mismatching row(s)
|
6
|
-
class DimensionMismatchError < ArgumentError; end
|
7
|
-
|
8
|
-
# Raised when reading non-existent element from a table
|
9
|
-
class TupleMissing < IndexError
|
10
|
-
attr_reader :i, :j
|
11
|
-
|
12
|
-
def initialize(i, j)
|
13
|
-
@i, @j = i, j
|
14
|
-
super("element at(#{i},#{j}) not found")
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end # Table
|
18
|
-
end # TTY
|
data/lib/tty/table/field.rb
DELETED
@@ -1,139 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Table
|
5
|
-
# A class that represents a unique element in a table.
|
6
|
-
#
|
7
|
-
# Used internally by {Table::Row} to define internal structure.
|
8
|
-
#
|
9
|
-
# @api private
|
10
|
-
class Field
|
11
|
-
include Equatable
|
12
|
-
|
13
|
-
# The value inside the field
|
14
|
-
#
|
15
|
-
# @api public
|
16
|
-
attr_reader :value
|
17
|
-
|
18
|
-
# The name for the value
|
19
|
-
#
|
20
|
-
# @api public
|
21
|
-
attr_reader :name
|
22
|
-
|
23
|
-
# TODO: Change to :content to separate value from formatted string
|
24
|
-
attr_writer :value
|
25
|
-
|
26
|
-
# The field value width
|
27
|
-
#
|
28
|
-
# @api public
|
29
|
-
attr_reader :width
|
30
|
-
|
31
|
-
# Number of columns this field spans. Defaults to 1.
|
32
|
-
#
|
33
|
-
attr_reader :colspan
|
34
|
-
|
35
|
-
# Number of rows this field spans. Defaults to 1.
|
36
|
-
#
|
37
|
-
attr_reader :rowspan
|
38
|
-
|
39
|
-
attr_reader :align
|
40
|
-
|
41
|
-
# Initialize a Field
|
42
|
-
#
|
43
|
-
# @example
|
44
|
-
# field = TTY::Table::Field.new 'a1'
|
45
|
-
# field.value # => a1
|
46
|
-
#
|
47
|
-
# @example
|
48
|
-
# field = TTY::Table::Field.new value: 'a1'
|
49
|
-
# field.value # => a1
|
50
|
-
#
|
51
|
-
# @example
|
52
|
-
# field = TTY::Table::Field.new value: 'a1', align: :center
|
53
|
-
# field.value # => a1
|
54
|
-
# field.align # => :center
|
55
|
-
#
|
56
|
-
# @api private
|
57
|
-
def initialize(value)
|
58
|
-
options = extract_options(value)
|
59
|
-
@width = options.fetch(:width) { @value.to_s.size }
|
60
|
-
@align = options.fetch(:align) { nil }
|
61
|
-
@colspan = options.fetch(:colspan) { 1 }
|
62
|
-
@rowspan = options.fetch(:rowspan) { 1 }
|
63
|
-
end
|
64
|
-
|
65
|
-
# Extract options and set value
|
66
|
-
#
|
67
|
-
# @api private
|
68
|
-
def extract_options(value)
|
69
|
-
if value.class <= Hash
|
70
|
-
options = value
|
71
|
-
@value = options.fetch(:value)
|
72
|
-
else
|
73
|
-
@value = value
|
74
|
-
options = {}
|
75
|
-
end
|
76
|
-
options
|
77
|
-
end
|
78
|
-
|
79
|
-
# Return the width this field would normally have bar other contraints
|
80
|
-
#
|
81
|
-
# @api public
|
82
|
-
def value_width
|
83
|
-
@width
|
84
|
-
end
|
85
|
-
|
86
|
-
def value_height
|
87
|
-
@height
|
88
|
-
end
|
89
|
-
|
90
|
-
# Return number of lines this value spans.
|
91
|
-
#
|
92
|
-
# A distinction is being made between escaped and non-escaped strings.
|
93
|
-
#
|
94
|
-
# @return [Array[String]]
|
95
|
-
#
|
96
|
-
# @api public
|
97
|
-
def lines
|
98
|
-
escaped = value.to_s.scan(/(\\n|\\t|\\r)/)
|
99
|
-
escaped.empty? ? value.to_s.split(/\n/, -1) : [value.to_s]
|
100
|
-
end
|
101
|
-
|
102
|
-
# If the string contains unescaped new lines then the longest token
|
103
|
-
# deterimines the actual field length.
|
104
|
-
#
|
105
|
-
# @return [Integer]
|
106
|
-
#
|
107
|
-
# @api public
|
108
|
-
def length
|
109
|
-
(lines.max_by(&:length) || '').size
|
110
|
-
end
|
111
|
-
|
112
|
-
# Extract the number of lines this value spans
|
113
|
-
#
|
114
|
-
# @return [Integer]
|
115
|
-
#
|
116
|
-
# @api public
|
117
|
-
def height
|
118
|
-
lines.size
|
119
|
-
end
|
120
|
-
|
121
|
-
def chars
|
122
|
-
value.chars
|
123
|
-
end
|
124
|
-
|
125
|
-
# Render value inside this field box
|
126
|
-
#
|
127
|
-
# @api public
|
128
|
-
def render
|
129
|
-
end
|
130
|
-
|
131
|
-
# Return field value
|
132
|
-
#
|
133
|
-
# @api public
|
134
|
-
def to_s
|
135
|
-
value
|
136
|
-
end
|
137
|
-
end # Field
|
138
|
-
end # Table
|
139
|
-
end # TTY
|
data/lib/tty/table/header.rb
DELETED
@@ -1,133 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'tty/vector'
|
4
|
-
require 'forwardable'
|
5
|
-
|
6
|
-
module TTY
|
7
|
-
class Table
|
8
|
-
# Convert an Array row into Header
|
9
|
-
#
|
10
|
-
# @return [TTY::Table::Header]
|
11
|
-
#
|
12
|
-
# @api private
|
13
|
-
def to_header(row)
|
14
|
-
Header.new(row)
|
15
|
-
end
|
16
|
-
|
17
|
-
# A set of header elements that correspond to values in each row
|
18
|
-
class Header < Vector
|
19
|
-
include Equatable
|
20
|
-
extend Forwardable
|
21
|
-
|
22
|
-
def_delegators :@attributes, :join, :map, :map!
|
23
|
-
|
24
|
-
# The header attributes
|
25
|
-
#
|
26
|
-
# @return [Array]
|
27
|
-
#
|
28
|
-
# @api private
|
29
|
-
attr_reader :attributes
|
30
|
-
alias :fields :attributes
|
31
|
-
|
32
|
-
# Initialize a Header
|
33
|
-
#
|
34
|
-
# @return [undefined]
|
35
|
-
#
|
36
|
-
# @api public
|
37
|
-
def initialize(attributes = [])
|
38
|
-
@attributes = attributes.map { |attr| to_field(attr) }
|
39
|
-
@attribute_for = Hash[@attributes.each_with_index.map.to_a]
|
40
|
-
end
|
41
|
-
|
42
|
-
# Instantiates a new field
|
43
|
-
#
|
44
|
-
# @param [String,Hash] attribute
|
45
|
-
# the attribute value to convert to field object
|
46
|
-
#
|
47
|
-
# @api public
|
48
|
-
def to_field(attribute = nil)
|
49
|
-
Field.new(attribute)
|
50
|
-
end
|
51
|
-
|
52
|
-
# Lookup a column in the header given a name
|
53
|
-
#
|
54
|
-
# @param [Integer, String] attribute
|
55
|
-
# the attribute to look up by
|
56
|
-
#
|
57
|
-
# @api public
|
58
|
-
def [](attribute)
|
59
|
-
case attribute
|
60
|
-
when Integer
|
61
|
-
@attributes[attribute].value
|
62
|
-
else
|
63
|
-
@attribute_for.fetch(to_field(attribute)) do |header_name|
|
64
|
-
fail UnknownAttributeError,
|
65
|
-
"the header '#{header_name.value}' is unknown"
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# Lookup attribute without evaluation
|
71
|
-
#
|
72
|
-
# @api public
|
73
|
-
def call(attribute)
|
74
|
-
@attributes[attribute]
|
75
|
-
end
|
76
|
-
|
77
|
-
# Set value at index
|
78
|
-
#
|
79
|
-
# @example
|
80
|
-
# header[attribute] = value
|
81
|
-
#
|
82
|
-
# @api public
|
83
|
-
def []=(attribute, value)
|
84
|
-
attributes[attribute] = to_field(value)
|
85
|
-
end
|
86
|
-
|
87
|
-
# Size of the header
|
88
|
-
#
|
89
|
-
# @return [Integer]
|
90
|
-
#
|
91
|
-
# @api public
|
92
|
-
def size
|
93
|
-
to_ary.size
|
94
|
-
end
|
95
|
-
alias :length :size
|
96
|
-
|
97
|
-
# Find maximum header height
|
98
|
-
#
|
99
|
-
# @return [Integer]
|
100
|
-
#
|
101
|
-
# @api public
|
102
|
-
def height
|
103
|
-
attributes.map { |field| field.height }.max
|
104
|
-
end
|
105
|
-
|
106
|
-
# Convert the Header into an Array
|
107
|
-
#
|
108
|
-
# @return [Array]
|
109
|
-
#
|
110
|
-
# @api public
|
111
|
-
def to_ary
|
112
|
-
attributes.map { |attr| attr.value if attr }
|
113
|
-
end
|
114
|
-
|
115
|
-
# Check if this header is equivalent to another header
|
116
|
-
#
|
117
|
-
# @return [Boolean]
|
118
|
-
#
|
119
|
-
# @api public
|
120
|
-
def ==(other)
|
121
|
-
to_a == other.to_a
|
122
|
-
end
|
123
|
-
alias :eql? :==
|
124
|
-
|
125
|
-
# Provide an unique hash value
|
126
|
-
#
|
127
|
-
# @api public
|
128
|
-
def to_hash
|
129
|
-
to_a.hash
|
130
|
-
end
|
131
|
-
end # Header
|
132
|
-
end # Table
|
133
|
-
end # TTY
|
@@ -1,52 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Table
|
5
|
-
# A class responsible for indenting table representation
|
6
|
-
class Indentation
|
7
|
-
|
8
|
-
attr_reader :renderer
|
9
|
-
|
10
|
-
# Initialize an Indentation
|
11
|
-
#
|
12
|
-
# @api public
|
13
|
-
def initialize(renderer)
|
14
|
-
@renderer = renderer
|
15
|
-
end
|
16
|
-
|
17
|
-
# Create indentation
|
18
|
-
#
|
19
|
-
# @api public
|
20
|
-
def indentation
|
21
|
-
' ' * renderer.indent
|
22
|
-
end
|
23
|
-
|
24
|
-
# Return a table part with indentation inserted
|
25
|
-
#
|
26
|
-
# @param [#map, #to_s] part
|
27
|
-
# the rendered table part
|
28
|
-
#
|
29
|
-
# @api public
|
30
|
-
def insert_indent(part)
|
31
|
-
if part.respond_to?(:to_a)
|
32
|
-
part.map { |line| insert_indentation(line) }
|
33
|
-
else
|
34
|
-
insert_indentation(part)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
# Insert indentation into a table renderd line
|
41
|
-
#
|
42
|
-
# @param [#to_a, #to_s] line
|
43
|
-
# the rendered table line
|
44
|
-
#
|
45
|
-
# @api public
|
46
|
-
def insert_indentation(line)
|
47
|
-
line = line.is_a?(Array) ? line[0] : line
|
48
|
-
line.insert(0, indentation) if line
|
49
|
-
end
|
50
|
-
end # Indentation
|
51
|
-
end # Table
|
52
|
-
end # TTY
|
@@ -1,107 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Table
|
5
|
-
module Operation
|
6
|
-
# A class representing an alignment of cell data.
|
7
|
-
#
|
8
|
-
# @api private
|
9
|
-
class Alignment
|
10
|
-
include TTY::Coercion
|
11
|
-
|
12
|
-
LEFT = :left.freeze
|
13
|
-
|
14
|
-
RIGHT = :right.freeze
|
15
|
-
|
16
|
-
CENTER = :center.freeze
|
17
|
-
|
18
|
-
# Hold the type of alignment
|
19
|
-
#
|
20
|
-
# @api private
|
21
|
-
attr_reader :type
|
22
|
-
|
23
|
-
# Initialize an Alignment
|
24
|
-
#
|
25
|
-
# @return [self]
|
26
|
-
#
|
27
|
-
# @raise [TypeError]
|
28
|
-
# raise if the alignment is not supported type
|
29
|
-
#
|
30
|
-
# @api private
|
31
|
-
def initialize(type = nil)
|
32
|
-
@type = coerce_to((type || LEFT), Symbol, :to_sym)
|
33
|
-
assert_valid_type
|
34
|
-
end
|
35
|
-
|
36
|
-
# Assert the type is valid
|
37
|
-
#
|
38
|
-
# @return [undefined]
|
39
|
-
#
|
40
|
-
# @raise [TypeError]
|
41
|
-
# raise if the alignment is not supported type
|
42
|
-
#
|
43
|
-
# @api private
|
44
|
-
def assert_valid_type
|
45
|
-
return if supported.include?(type)
|
46
|
-
fail TypeError, "Alignment must be one of: #{supported.join(' ')}"
|
47
|
-
end
|
48
|
-
|
49
|
-
# List supported alignment types
|
50
|
-
#
|
51
|
-
# @return [Array]
|
52
|
-
# valid alignments
|
53
|
-
#
|
54
|
-
# @api private
|
55
|
-
def supported
|
56
|
-
[LEFT, RIGHT, CENTER]
|
57
|
-
end
|
58
|
-
|
59
|
-
# Format field with a given alignment
|
60
|
-
#
|
61
|
-
# @param [Object] field
|
62
|
-
#
|
63
|
-
# @param [Integer] column_width
|
64
|
-
#
|
65
|
-
# @param [String] space
|
66
|
-
#
|
67
|
-
# @return [String] aligned
|
68
|
-
#
|
69
|
-
# @api public
|
70
|
-
def format(field, column_width, space = '')
|
71
|
-
case type
|
72
|
-
when :left
|
73
|
-
"%-#{column_width}s#{space}" % field.to_s
|
74
|
-
when :right
|
75
|
-
"%#{column_width}s#{space}" % field.to_s
|
76
|
-
when :center
|
77
|
-
center_align field, column_width, space
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
# Center aligns field
|
84
|
-
#
|
85
|
-
# @param [Object] field
|
86
|
-
#
|
87
|
-
# @param [Integer] column_width
|
88
|
-
#
|
89
|
-
# @param [String] space
|
90
|
-
#
|
91
|
-
# @return [String] aligned
|
92
|
-
#
|
93
|
-
# @api private
|
94
|
-
def center_align(field, column_width, space)
|
95
|
-
chars = field.to_s.chars.to_a
|
96
|
-
if column_width >= chars.size
|
97
|
-
right = ((pad_length = column_width - chars.length).to_f / 2).ceil
|
98
|
-
left = pad_length - right
|
99
|
-
[' ' * left, field, ' ' * right, space].join
|
100
|
-
else
|
101
|
-
"#{field}#{space}"
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end # Alignment
|
105
|
-
end # Operation
|
106
|
-
end # Table
|
107
|
-
end # TTY
|
@@ -1,77 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module TTY
|
4
|
-
class Table
|
5
|
-
module Operation
|
6
|
-
# A class which responsiblity is to align table rows and header.
|
7
|
-
class AlignmentSet < Vector
|
8
|
-
|
9
|
-
attr_reader :widths
|
10
|
-
|
11
|
-
# Initialize an AlignmentSet
|
12
|
-
#
|
13
|
-
# @api private
|
14
|
-
def initialize(aligns, widths = nil)
|
15
|
-
@converter = Necromancer.new
|
16
|
-
@elements = @converter.convert(aligns).to(:array)
|
17
|
-
@widths = widths
|
18
|
-
end
|
19
|
-
|
20
|
-
# Lookup an alignment by index
|
21
|
-
#
|
22
|
-
# @param [Integer]
|
23
|
-
#
|
24
|
-
# @return [Symbol] alignment
|
25
|
-
#
|
26
|
-
# @api public
|
27
|
-
def [](index)
|
28
|
-
elements.fetch(index, :left)
|
29
|
-
end
|
30
|
-
|
31
|
-
# Return each alignment in an Array
|
32
|
-
#
|
33
|
-
# @return [Array]
|
34
|
-
#
|
35
|
-
# @api private
|
36
|
-
def alignments
|
37
|
-
map { |alignment| alignment }
|
38
|
-
end
|
39
|
-
|
40
|
-
# Evaluate alignment of the provided row
|
41
|
-
#
|
42
|
-
# @param [Array] row
|
43
|
-
# the table row
|
44
|
-
# @param [Hash] options
|
45
|
-
# the table options
|
46
|
-
#
|
47
|
-
# @return [TTY::Table::Field]
|
48
|
-
#
|
49
|
-
# @api public
|
50
|
-
def call(field, row, col)
|
51
|
-
align_field(field, col)
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
|
56
|
-
# Align each field in a row
|
57
|
-
#
|
58
|
-
# @param [TTY::Table::Field] field
|
59
|
-
# the table field
|
60
|
-
#
|
61
|
-
# @param [Integer] col
|
62
|
-
# the table column index
|
63
|
-
#
|
64
|
-
# @param [Hash] options
|
65
|
-
#
|
66
|
-
# @return [TTY::Table::Field]
|
67
|
-
#
|
68
|
-
# @api private
|
69
|
-
def align_field(field, col)
|
70
|
-
column_width = widths[col]
|
71
|
-
alignment = Alignment.new(field.align || self[col])
|
72
|
-
field.value = alignment.format(field, column_width)
|
73
|
-
end
|
74
|
-
end # AlignmentSet
|
75
|
-
end # Operation
|
76
|
-
end # Table
|
77
|
-
end # TTY
|