-
Notifications
You must be signed in to change notification settings - Fork 6
/
border.rb
200 lines (185 loc) · 5.53 KB
/
border.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
module Tabulo
# @!visibility private
class Border
STYLES = {
ascii: {
corner_top_left: "+",
corner_top_right: "+",
corner_bottom_right: "+",
corner_bottom_left: "+",
edge_top: "-",
edge_right: "|",
edge_bottom: "-",
edge_left: "|",
tee_top: "+",
tee_right: "+",
tee_bottom: "+",
tee_left: "+",
divider_vertical: "|",
divider_horizontal: "-",
intersection: "+",
},
classic: {
corner_top_left: "+",
corner_top_right: "+",
edge_top: "-",
edge_right: "|",
edge_left: "|",
tee_top: "+",
tee_right: "+",
tee_left: "+",
divider_vertical: "|",
divider_horizontal: "-",
intersection: "+",
},
reduced_ascii: {
corner_top_left: "",
corner_top_right: "",
corner_bottom_right: "",
corner_bottom_left: "",
edge_top: "-",
edge_right: "",
edge_bottom: "-",
edge_left: "",
tee_top: " ",
tee_right: "",
tee_bottom: " ",
tee_left: "",
divider_vertical: " ",
divider_horizontal: "-",
intersection: " ",
},
reduced_modern: {
corner_top_left: "",
corner_top_right: "",
corner_bottom_right: "",
corner_bottom_left: "",
edge_top: "─",
edge_right: "",
edge_bottom: "─",
edge_left: "",
tee_top: " ",
tee_right: "",
tee_bottom: " ",
tee_left: "",
divider_vertical: " ",
divider_horizontal: "─",
intersection: " ",
},
markdown: {
corner_top_left: "",
corner_top_right: "",
corner_bottom_right: "",
corner_bottom_left: "",
edge_top: "",
edge_right: "|",
edge_bottom: "",
edge_left: "|",
tee_top: "",
tee_right: "|",
tee_bottom: "",
tee_left: "|",
divider_vertical: "|",
divider_horizontal: "-",
intersection: "|",
},
modern: {
corner_top_left: "┌",
corner_top_right: "┐",
corner_bottom_right: "┘",
corner_bottom_left: "└",
edge_top: "─",
edge_right: "│",
edge_bottom: "─",
edge_left: "│",
tee_top: "┬",
tee_right: "┤",
tee_bottom: "┴",
tee_left: "├",
divider_vertical: "│",
divider_horizontal: "─",
intersection: "┼",
},
blank: {
},
}
# @!visibility private
def self.from(initializer, styler = nil)
new(options(initializer).merge(styler: styler))
end
# @!visibility private
def horizontal_rule(column_widths, position = :bottom)
left, center, right, segment =
case position
when :top
[@corner_top_left, @tee_top, @corner_top_right, @edge_top]
when :middle
[@tee_left, @intersection, @tee_right, @divider_horizontal]
when :bottom
[@corner_bottom_left, @tee_bottom, @corner_bottom_right, @edge_bottom]
end
segments = column_widths.map { |width| segment * width }
style("#{left}#{segments.join(center)}#{right}")
end
# @!visibility private
def join_cell_contents(cells)
styled_divider_vertical = style(@divider_vertical)
styled_edge_left = style(@edge_left)
styled_edge_right = style(@edge_right)
styled_edge_left + cells.join(styled_divider_vertical) + styled_edge_right
end
private
def self.options(kind)
opts = STYLES[kind]
return opts if opts
raise InvalidBorderError
end
# @param [nil, #to_proc] styler (nil) A lambda or other callable object taking
# a single parameter, representing a section of the table's borders (which for this purpose
# include any horizontal and vertical lines inside the table), and returning a string.
# If passed <tt>nil</tt>, then no additional styling will be applied to borders. If passed a
# callable, then that callable will be called for each border section, with the
# resulting string rendered in place of that border. The extra width of the string returned by the
# {styler} is not taken into consideration by the internal table rendering calculations
# Thus it can be used to apply ANSI escape codes to border characters, to colour the borders
# for example, without breaking the table formatting.
# @return [Border] a new {Border}
def initialize(
corner_top_left: "",
corner_top_right: "",
corner_bottom_right: "",
corner_bottom_left: "",
edge_top: "",
edge_right: "",
edge_bottom: "",
edge_left: "",
tee_top: "",
tee_right: "",
tee_bottom: "",
tee_left: "",
divider_vertical: "",
divider_horizontal: "",
intersection: "",
styler: nil)
@corner_top_left = corner_top_left
@corner_top_right = corner_top_right
@corner_bottom_right = corner_bottom_right
@corner_bottom_left = corner_bottom_left
@edge_top = edge_top
@edge_right = edge_right
@edge_bottom = edge_bottom
@edge_left = edge_left
@tee_top = tee_top
@tee_right = tee_right
@tee_bottom = tee_bottom
@tee_left = tee_left
@divider_vertical = divider_vertical
@divider_horizontal = divider_horizontal
@intersection = intersection
@styler = styler
end
def style(s)
@styler ? @styler.call(s) : s
end
end
end