tty-box 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +59 -3
- data/examples/commander.rb +3 -1
- data/examples/connected.rb +47 -0
- data/lib/tty/box.rb +60 -44
- data/lib/tty/box/border.rb +54 -13
- data/lib/tty/box/version.rb +1 -1
- data/spec/unit/border/parse_spec.rb +61 -0
- data/spec/unit/border_spec.rb +87 -8
- data/spec/unit/style_spec.rb +90 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de83dc517a44cecc9c6936beafcc7a9e92bbef44b9f835ce8c5f537a34fcfb99
|
4
|
+
data.tar.gz: 4e5a152bf222a4a1a267fa97699b8f20cbe33be48c9d7847b83172dc26708c2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8979a9ad2c06c9ff1762005feabfbdf28456e75a496c3a39a4276b7fe73a31fc9bb69c8941d4470efb8c2a7277bdbdce62de7c788f95622a24c299cd015a2e72
|
7
|
+
data.tar.gz: 99f56b7376c5d96b933e1deb3f7282d37e7b7ce10130240516dc3408387feb38edb567de17ee72a96e9c1e5de49b3cde2c3f2d35fd0ff6cfc22767e1a010fb9b
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.3.0] - 2018-10-08
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add border parameters :top_left, :top_right, :bottom_left & :bottom_right to allow specifying values for box corners
|
7
|
+
* Add :ascii border type for drawing ASCII boxes
|
8
|
+
|
9
|
+
### Fixed
|
10
|
+
* Fix box color fill to corretly recognise missing borders and match the height and width
|
11
|
+
* Fix absolute content positioning when borders are missing
|
12
|
+
|
3
13
|
## [v0.2.1] - 2018-09-10
|
4
14
|
|
5
15
|
### Fixed
|
@@ -15,6 +25,7 @@
|
|
15
25
|
|
16
26
|
* Initial implementation and release
|
17
27
|
|
28
|
+
[v0.3.0]: https://github.com/piotrmurach/tty-box/compare/v0.2.1...v0.3.0
|
18
29
|
[v0.2.1]: https://github.com/piotrmurach/tty-box/compare/v0.2.0...v0.2.1
|
19
30
|
[v0.2.0]: https://github.com/piotrmurach/tty-box/compare/v0.1.0...v0.2.0
|
20
31
|
[v0.1.0]: https://github.com/piotrmurach/tty-box/compare/v0.1.0
|
data/README.md
CHANGED
@@ -194,7 +194,7 @@ print box
|
|
194
194
|
|
195
195
|
### 2.5 border
|
196
196
|
|
197
|
-
There are
|
197
|
+
There are three types of border `:ascii`, `:light`, `:thick`. By default the `:light` border is used. This can be changed using the `:border` keyword:
|
198
198
|
|
199
199
|
```ruby
|
200
200
|
box = TTY::Box.new(width 30, height: 10, border: :thick)
|
@@ -217,10 +217,66 @@ print box
|
|
217
217
|
# ╚════════════════════════════╝
|
218
218
|
```
|
219
219
|
|
220
|
-
You can also selectively
|
220
|
+
You can also selectively specify and turn off border parts by passing a hash with a `:border` key. The border parts are:
|
221
|
+
|
222
|
+
```
|
223
|
+
:top
|
224
|
+
:top_left ┌────────┐ :top_right
|
225
|
+
│ │
|
226
|
+
:left │ │ :right
|
227
|
+
│ │
|
228
|
+
:bottom_left └────────┘ :bottom_right
|
229
|
+
:bottom
|
230
|
+
```
|
231
|
+
|
232
|
+
The following are available border parts values:
|
233
|
+
|
234
|
+
| Border values | ASCII | Unicode Light | Unicode Thick |
|
235
|
+
| -------------------- |:-----:|:-------------:|:-------------:|
|
236
|
+
| :line | `-` | `─` | `═` |
|
237
|
+
| :pipe | `\|` | `\│` | `\║` |
|
238
|
+
| :cross | `+` | `┼` | `╬` |
|
239
|
+
| :divider_up | `+` | `┴` | `╩` |
|
240
|
+
| :divider_down | `+` | `┬` | `╦` |
|
241
|
+
| :divider_left | `+` | `┤` | `╣` |
|
242
|
+
| :divider_right | `+` | `├` | `╠` |
|
243
|
+
| :corner_top_left | `+` | `┌` | `╔` |
|
244
|
+
| :corner_top_right | `+` | `┐` | `╗` |
|
245
|
+
| :corner_bottom_left | `+` | `└` | `╚` |
|
246
|
+
| :corner_bottom_right | `+` | `┘` | `╝` |
|
247
|
+
|
248
|
+
For example, to change all box corners to be a `:cross` do:
|
221
249
|
|
222
250
|
```ruby
|
223
|
-
TTY::Box.
|
251
|
+
box = TTY::Box.frame(
|
252
|
+
width: 10, height: 4,
|
253
|
+
border: {
|
254
|
+
top_left: :cross,
|
255
|
+
top_right: :cross,
|
256
|
+
bottom_left: :cross,
|
257
|
+
bottom_right: :cross
|
258
|
+
}
|
259
|
+
)
|
260
|
+
```
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
print box
|
264
|
+
# =>
|
265
|
+
# ┼────────┼
|
266
|
+
# │ │
|
267
|
+
# │ │
|
268
|
+
# ┼────────┼
|
269
|
+
```
|
270
|
+
|
271
|
+
If you want to remoe a given border element as a value use `false`. For example to remove bottom border do:
|
272
|
+
|
273
|
+
```ruby
|
274
|
+
TTY::Box.new(
|
275
|
+
width: 30, height: 10,
|
276
|
+
border: {
|
277
|
+
type: :thick,
|
278
|
+
bottom: false
|
279
|
+
})
|
224
280
|
```
|
225
281
|
|
226
282
|
### 2.6 styling
|
data/examples/commander.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../lib/tty-box'
|
4
|
+
|
5
|
+
print TTY::Cursor.clear_screen
|
6
|
+
|
7
|
+
box_1 = TTY::Box.frame(
|
8
|
+
top: 3,
|
9
|
+
left: 10,
|
10
|
+
width: 15,
|
11
|
+
height: 5,
|
12
|
+
border: {
|
13
|
+
type: :thick,
|
14
|
+
right: false,
|
15
|
+
},
|
16
|
+
align: :center,
|
17
|
+
padding: [1, 2],
|
18
|
+
style: {
|
19
|
+
bg: :red,
|
20
|
+
border: {
|
21
|
+
bg: :red
|
22
|
+
}
|
23
|
+
}
|
24
|
+
) { "Space" }
|
25
|
+
|
26
|
+
box_2 = TTY::Box.frame(
|
27
|
+
top: 3,
|
28
|
+
left: 25,
|
29
|
+
width: 15,
|
30
|
+
height: 5,
|
31
|
+
border: {
|
32
|
+
type: :thick,
|
33
|
+
top_left: :divider_down,
|
34
|
+
bottom_left: :divider_up
|
35
|
+
},
|
36
|
+
align: :center,
|
37
|
+
padding: [1,2],
|
38
|
+
style: {
|
39
|
+
bg: :red,
|
40
|
+
border: {
|
41
|
+
bg: :red
|
42
|
+
}
|
43
|
+
}
|
44
|
+
) { "Invaders!" }
|
45
|
+
|
46
|
+
puts box_1 + box_2
|
47
|
+
print "\n" * 5
|
data/lib/tty/box.rb
CHANGED
@@ -12,52 +12,53 @@ module TTY
|
|
12
12
|
module_function
|
13
13
|
|
14
14
|
BOX_CHARS = {
|
15
|
+
ascii: %w[+ + + + + + + + - | +],
|
15
16
|
light: %w[┘ ┐ ┌ └ ┤ ┴ ┬ ├ ─ │ ┼],
|
16
17
|
thick: %w[╝ ╗ ╔ ╚ ╣ ╩ ╦ ╠ ═ ║ ╬]
|
17
18
|
}.freeze
|
18
19
|
|
19
|
-
def
|
20
|
-
BOX_CHARS[border][
|
20
|
+
def corner_bottom_right_char(border = :light)
|
21
|
+
BOX_CHARS[border][0]
|
21
22
|
end
|
22
23
|
|
23
|
-
def
|
24
|
-
BOX_CHARS[border][
|
24
|
+
def corner_top_right_char(border = :light)
|
25
|
+
BOX_CHARS[border][1]
|
25
26
|
end
|
26
27
|
|
27
|
-
def
|
28
|
-
BOX_CHARS[border][
|
28
|
+
def corner_top_left_char(border = :light)
|
29
|
+
BOX_CHARS[border][2]
|
29
30
|
end
|
30
31
|
|
31
|
-
def
|
32
|
-
BOX_CHARS[border][
|
32
|
+
def corner_bottom_left_char(border = :light)
|
33
|
+
BOX_CHARS[border][3]
|
33
34
|
end
|
34
35
|
|
35
|
-
def
|
36
|
+
def divider_left_char(border = :light)
|
36
37
|
BOX_CHARS[border][4]
|
37
38
|
end
|
38
39
|
|
39
|
-
def
|
40
|
-
BOX_CHARS[border][
|
40
|
+
def divider_up_char(border = :light)
|
41
|
+
BOX_CHARS[border][5]
|
41
42
|
end
|
42
43
|
|
43
|
-
def
|
44
|
+
def divider_down_char(border = :light)
|
44
45
|
BOX_CHARS[border][6]
|
45
46
|
end
|
46
47
|
|
47
|
-
def
|
48
|
-
BOX_CHARS[border][
|
48
|
+
def divider_right_char(border = :light)
|
49
|
+
BOX_CHARS[border][7]
|
49
50
|
end
|
50
51
|
|
51
|
-
def
|
52
|
-
BOX_CHARS[border][
|
52
|
+
def line_char(border = :light)
|
53
|
+
BOX_CHARS[border][8]
|
53
54
|
end
|
54
55
|
|
55
|
-
def
|
56
|
-
BOX_CHARS[border][
|
56
|
+
def pipe_char(border = :light)
|
57
|
+
BOX_CHARS[border][9]
|
57
58
|
end
|
58
59
|
|
59
|
-
def
|
60
|
-
BOX_CHARS[border][
|
60
|
+
def cross_char(border = :light)
|
61
|
+
BOX_CHARS[border][10]
|
61
62
|
end
|
62
63
|
|
63
64
|
def cursor
|
@@ -78,6 +79,10 @@ module TTY
|
|
78
79
|
position = top && left
|
79
80
|
|
80
81
|
border = Border.parse(border)
|
82
|
+
top_size = border.top? ? 1: 0
|
83
|
+
bottom_size = border.bottom? ? 1: 0
|
84
|
+
left_size = border.left? ? 1 : 0
|
85
|
+
right_size = border.right ? 1 : 0
|
81
86
|
|
82
87
|
if block_given?
|
83
88
|
content = format(yield, width, padding, align)
|
@@ -88,31 +93,37 @@ module TTY
|
|
88
93
|
|
89
94
|
if border.top?
|
90
95
|
output << cursor.move_to(left, top) if position
|
91
|
-
output << top_border(title, width, border
|
96
|
+
output << top_border(title, width, border, style)
|
92
97
|
output << "\n" unless position
|
93
98
|
end
|
94
|
-
|
99
|
+
|
100
|
+
(height - top_size - bottom_size).times do |i|
|
101
|
+
output << cursor.move_to(left, top + i + top_size) if position
|
95
102
|
if border.left?
|
96
|
-
output << cursor.move_to(left, top + i + 1) if position
|
97
103
|
output << border_bg.(border_fg.(pipe_char(border.type)))
|
98
104
|
end
|
99
|
-
|
100
|
-
|
101
|
-
|
105
|
+
|
106
|
+
content_size = width - left_size - right_size
|
107
|
+
unless content[i].nil?
|
102
108
|
output << bg.(fg.(content[i]))
|
103
|
-
|
104
|
-
output << bg.(fg.(' ' * (width - 2 - content[i].size)))
|
105
|
-
end
|
109
|
+
content_size -= content[i].size
|
106
110
|
end
|
111
|
+
if style[:fg] || style[:bg] || !position # something to color
|
112
|
+
output << bg.(fg.(' ' * content_size))
|
113
|
+
end
|
114
|
+
|
107
115
|
if border.right?
|
108
|
-
|
116
|
+
if position
|
117
|
+
output << cursor.move_to(left + width - right_size, top + i + top_size)
|
118
|
+
end
|
109
119
|
output << border_bg.(border_fg.(pipe_char(border.type)))
|
110
120
|
end
|
111
121
|
output << "\n" unless position
|
112
122
|
end
|
123
|
+
|
113
124
|
if border.bottom?
|
114
|
-
output << cursor.move_to(left, top + height -
|
115
|
-
output << bottom_border(title, width, border
|
125
|
+
output << cursor.move_to(left, top + height - bottom_size) if position
|
126
|
+
output << bottom_border(title, width, border, style)
|
116
127
|
output << "\n" unless position
|
117
128
|
end
|
118
129
|
|
@@ -156,19 +167,21 @@ module TTY
|
|
156
167
|
title[:top_right].to_s.size
|
157
168
|
fg, bg = *extract_style(style[:border] || {})
|
158
169
|
|
159
|
-
|
160
|
-
|
170
|
+
top_left = border.top_left? && border.left? ? send(:"#{border.top_left}_char", border.type) : ""
|
171
|
+
top_right = border.top_right? && border.right? ? send(:"#{border.top_right}_char", border.type) : ""
|
172
|
+
|
173
|
+
top_space_left = width - top_titles_size - top_left.size - top_right.size
|
161
174
|
top_space_before = top_space_left / 2
|
162
|
-
top_space_after
|
175
|
+
top_space_after = top_space_left / 2 + top_space_left % 2
|
163
176
|
|
164
177
|
[
|
165
|
-
bg.(fg.(
|
178
|
+
bg.(fg.(top_left)),
|
166
179
|
bg.(title[:top_left].to_s),
|
167
|
-
bg.(fg.(line_char(border) * top_space_before)),
|
180
|
+
bg.(fg.(line_char(border.type) * top_space_before)),
|
168
181
|
bg.(title[:top_center].to_s),
|
169
|
-
bg.(fg.(line_char(border) * top_space_after)),
|
182
|
+
bg.(fg.(line_char(border.type) * top_space_after)),
|
170
183
|
bg.(title[:top_right].to_s),
|
171
|
-
bg.(fg.(
|
184
|
+
bg.(fg.(top_right))
|
172
185
|
].join('')
|
173
186
|
end
|
174
187
|
|
@@ -183,19 +196,22 @@ module TTY
|
|
183
196
|
title[:bottom_right].to_s.size
|
184
197
|
fg, bg = *extract_style(style[:border] || {})
|
185
198
|
|
199
|
+
bottom_left = border.bottom_left? && border.left? ? send(:"#{border.bottom_left}_char", border.type) : ""
|
200
|
+
bottom_right = border.bottom_right? && border.right? ? send(:"#{border.bottom_right}_char", border.type) : ""
|
201
|
+
|
186
202
|
bottom_space_left = width - bottom_titles_size -
|
187
|
-
|
203
|
+
bottom_left.size - bottom_right.size
|
188
204
|
bottom_space_before = bottom_space_left / 2
|
189
205
|
bottom_space_after = bottom_space_left / 2 + bottom_space_left % 2
|
190
206
|
|
191
207
|
[
|
192
|
-
bg.(fg.(
|
208
|
+
bg.(fg.(bottom_left)),
|
193
209
|
bg.(title[:bottom_left].to_s),
|
194
|
-
bg.(fg.(line_char(border) * bottom_space_before)),
|
210
|
+
bg.(fg.(line_char(border.type) * bottom_space_before)),
|
195
211
|
bg.(title[:bottom_center].to_s),
|
196
|
-
bg.(fg.(line_char(border) * bottom_space_after)),
|
212
|
+
bg.(fg.(line_char(border.type) * bottom_space_after)),
|
197
213
|
bg.(title[:bottom_right].to_s),
|
198
|
-
bg.(fg.(
|
214
|
+
bg.(fg.(bottom_right))
|
199
215
|
].join('')
|
200
216
|
end
|
201
217
|
end # TTY
|
data/lib/tty/box/border.rb
CHANGED
@@ -1,38 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module TTY
|
2
4
|
module Box
|
3
5
|
# A class reponsible for retrieving border options
|
4
6
|
#
|
5
7
|
# @api private
|
6
8
|
class Border
|
9
|
+
BORDER_VALUES = [
|
10
|
+
:corner_bottom_right,
|
11
|
+
:corner_top_right,
|
12
|
+
:corner_top_left,
|
13
|
+
:corner_bottom_left,
|
14
|
+
:divider_left,
|
15
|
+
:divider_up,
|
16
|
+
:divider_down,
|
17
|
+
:divider_right,
|
18
|
+
:line,
|
19
|
+
:pipe,
|
20
|
+
:cross
|
21
|
+
].freeze
|
22
|
+
|
7
23
|
def self.parse(border)
|
8
24
|
case border
|
9
25
|
when Hash
|
10
|
-
new(border
|
11
|
-
border.fetch(:top, true),
|
12
|
-
border.fetch(:left, true),
|
13
|
-
border.fetch(:right, true),
|
14
|
-
border.fetch(:bottom, true))
|
26
|
+
new(border)
|
15
27
|
when *TTY::Box::BOX_CHARS.keys
|
16
|
-
new(border
|
28
|
+
new(type: border)
|
17
29
|
else
|
18
30
|
raise ArgumentError,
|
19
31
|
"Wrong value `#{border}` for :border configuration option"
|
20
32
|
end
|
21
33
|
end
|
22
34
|
|
23
|
-
attr_reader :type, :top, :left, :right,
|
35
|
+
attr_reader :type, :top, :top_left, :top_right, :left, :right,
|
36
|
+
:bottom, :bottom_left, :bottom_right
|
24
37
|
|
25
38
|
alias top? top
|
26
39
|
alias left? left
|
27
40
|
alias right? right
|
28
41
|
alias bottom? bottom
|
42
|
+
alias top_left? top_left
|
43
|
+
alias top_right? top_right
|
44
|
+
alias bottom_left? bottom_left
|
45
|
+
alias bottom_right? bottom_right
|
29
46
|
|
30
|
-
def initialize(type
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
47
|
+
def initialize(type: :light,
|
48
|
+
top: :line,
|
49
|
+
top_left: :corner_top_left,
|
50
|
+
top_right: :corner_top_right,
|
51
|
+
left: :pipe,
|
52
|
+
right: :pipe,
|
53
|
+
bottom: :line,
|
54
|
+
bottom_left: :corner_bottom_left,
|
55
|
+
bottom_right: :corner_bottom_right)
|
56
|
+
|
57
|
+
@type = type
|
58
|
+
@top = check_name(:top, top)
|
59
|
+
@top_left = check_name(:top_left, top_left)
|
60
|
+
@top_right = check_name(:top_right, top_right)
|
61
|
+
@left = check_name(:left, left)
|
62
|
+
@right = check_name(:right, right)
|
63
|
+
@bottom = check_name(:bottom, bottom)
|
64
|
+
@bottom_left = check_name(:bottom_left, bottom_left)
|
65
|
+
@bottom_right = check_name(:bottom_right, bottom_right)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
# Check if border values name is allowed
|
71
|
+
# @api private
|
72
|
+
def check_name(key, value)
|
73
|
+
unless (BORDER_VALUES.include?(:"#{value}") || [true, false].include?(value))
|
74
|
+
raise ArgumentError, "Invalid border value: '#{value}' for #{key.inspect}"
|
75
|
+
end
|
76
|
+
value
|
36
77
|
end
|
37
78
|
end # Border
|
38
79
|
end # Box
|
data/lib/tty/box/version.rb
CHANGED
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe TTY::Box::Border, '.parse' do
|
4
|
+
it "parses default border" do
|
5
|
+
border = TTY::Box::Border.parse({})
|
6
|
+
top_border = [border.top_left, border.top, border.top_right]
|
7
|
+
bottom_border = [border.bottom_left, border.bottom, border.bottom_right]
|
8
|
+
|
9
|
+
expect(border.type).to eq(:light)
|
10
|
+
expect(top_border).to eq([:corner_top_left, :line, :corner_top_right])
|
11
|
+
expect(bottom_border).to eq([:corner_bottom_left, :line, :corner_bottom_right])
|
12
|
+
expect(border.left).to eq(:pipe)
|
13
|
+
expect(border.right).to eq(:pipe)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "parses only border type" do
|
17
|
+
border = TTY::Box::Border.parse(:thick)
|
18
|
+
top_border = [border.top_left, border.top, border.top_right]
|
19
|
+
bottom_border = [border.bottom_left, border.bottom, border.bottom_right]
|
20
|
+
|
21
|
+
expect(border.type).to eq(:thick)
|
22
|
+
expect(top_border).to eq([:corner_top_left, :line, :corner_top_right])
|
23
|
+
expect(bottom_border).to eq([:corner_bottom_left, :line, :corner_bottom_right])
|
24
|
+
expect(border.left).to eq(:pipe)
|
25
|
+
expect(border.right).to eq(:pipe)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "parses custom border" do
|
29
|
+
border = TTY::Box::Border.parse({
|
30
|
+
top: true,
|
31
|
+
top_left: :cross,
|
32
|
+
top_right: :cross,
|
33
|
+
bottom: true,
|
34
|
+
bottom_left: :cross,
|
35
|
+
bottom_right: :cross
|
36
|
+
})
|
37
|
+
|
38
|
+
top_border = [border.top_left, border.top, border.top_right]
|
39
|
+
bottom_border = [border.bottom_left, border.bottom, border.bottom_right]
|
40
|
+
|
41
|
+
expect(border.type).to eq(:light)
|
42
|
+
expect(top_border).to eq([:cross, true, :cross])
|
43
|
+
expect(bottom_border).to eq([:cross, true, :cross])
|
44
|
+
end
|
45
|
+
|
46
|
+
it "parses divider values" do
|
47
|
+
border = TTY::Box::Border.parse({
|
48
|
+
top_left: :divider_right,
|
49
|
+
top_right: :divider_left,
|
50
|
+
bottom_left: :divider_down,
|
51
|
+
bottom_right: :divider_up
|
52
|
+
})
|
53
|
+
|
54
|
+
top_border = [border.top_left, border.top, border.top_right]
|
55
|
+
bottom_border = [border.bottom_left, border.bottom, border.bottom_right]
|
56
|
+
|
57
|
+
expect(border.type).to eq(:light)
|
58
|
+
expect(top_border).to eq([:divider_right, :line, :divider_left])
|
59
|
+
expect(bottom_border).to eq([:divider_down, :line, :divider_up])
|
60
|
+
end
|
61
|
+
end
|
data/spec/unit/border_spec.rb
CHANGED
@@ -13,7 +13,7 @@ RSpec.describe TTY::Box, ':border option' do
|
|
13
13
|
].join)
|
14
14
|
end
|
15
15
|
|
16
|
-
it "creates frame with double lines" do
|
16
|
+
it "creates frame with double lines and absolute position" do
|
17
17
|
box = TTY::Box.frame(
|
18
18
|
top: 0, left: 0,
|
19
19
|
width: 35, height: 4,
|
@@ -28,10 +28,24 @@ RSpec.describe TTY::Box, ':border option' do
|
|
28
28
|
].join)
|
29
29
|
end
|
30
30
|
|
31
|
+
it "creates an ASCII box" do
|
32
|
+
box = TTY::Box.frame(
|
33
|
+
width: 10, height: 4,
|
34
|
+
border: :ascii
|
35
|
+
)
|
36
|
+
|
37
|
+
expect(box).to eq([
|
38
|
+
"+--------+\n",
|
39
|
+
"| |\n",
|
40
|
+
"| |\n",
|
41
|
+
"+--------+\n",
|
42
|
+
].join)
|
43
|
+
end
|
44
|
+
|
31
45
|
it "creates frame with without top & bottom borders" do
|
32
46
|
box = TTY::Box.frame(
|
33
47
|
top: 0, left: 0,
|
34
|
-
width:
|
48
|
+
width: 15, height: 4,
|
35
49
|
border: {
|
36
50
|
type: :thick,
|
37
51
|
top: false,
|
@@ -40,15 +54,17 @@ RSpec.describe TTY::Box, ':border option' do
|
|
40
54
|
) { "Hello Piotr!" }
|
41
55
|
|
42
56
|
expect(box).to eq([
|
43
|
-
"\e[
|
44
|
-
"\e[
|
57
|
+
"\e[1;1H║Hello Piotr! \e[1;15H║",
|
58
|
+
"\e[2;1H║\e[2;15H║",
|
59
|
+
"\e[3;1H║\e[3;15H║",
|
60
|
+
"\e[4;1H║\e[4;15H║",
|
45
61
|
].join)
|
46
62
|
end
|
47
63
|
|
48
64
|
it "creates frame without left & right borders" do
|
49
65
|
box = TTY::Box.frame(
|
50
66
|
top: 0, left: 0,
|
51
|
-
width:
|
67
|
+
width: 15, height: 4,
|
52
68
|
border: {
|
53
69
|
left: false,
|
54
70
|
right: false
|
@@ -56,12 +72,75 @@ RSpec.describe TTY::Box, ':border option' do
|
|
56
72
|
) { "Hello Piotr!" }
|
57
73
|
|
58
74
|
expect(box).to eq([
|
59
|
-
"\e[1;1H
|
60
|
-
"
|
61
|
-
"\e[
|
75
|
+
"\e[1;1H───────────────",
|
76
|
+
"\e[2;1HHello Piotr! ",
|
77
|
+
"\e[3;1H",
|
78
|
+
"\e[4;1H───────────────"
|
79
|
+
].join)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "creates frame without left & top borders" do
|
83
|
+
box = TTY::Box.frame(
|
84
|
+
top: 0, left: 0,
|
85
|
+
width: 15, height: 4,
|
86
|
+
border: {
|
87
|
+
left: false,
|
88
|
+
top: false
|
89
|
+
}
|
90
|
+
) { "Hello Piotr!" }
|
91
|
+
|
92
|
+
expect(box).to eq([
|
93
|
+
"\e[1;1HHello Piotr! \e[1;15H│",
|
94
|
+
"\e[2;1H\e[2;15H│",
|
95
|
+
"\e[3;1H\e[3;15H│",
|
96
|
+
"\e[4;1H──────────────┘"
|
62
97
|
].join)
|
63
98
|
end
|
64
99
|
|
100
|
+
it "creates frame with all corners changed to cross" do
|
101
|
+
box = TTY::Box.frame(
|
102
|
+
width: 10, height: 4,
|
103
|
+
border: {
|
104
|
+
top_left: :cross,
|
105
|
+
top_right: :cross,
|
106
|
+
bottom_left: :cross,
|
107
|
+
bottom_right: :cross
|
108
|
+
}
|
109
|
+
)
|
110
|
+
|
111
|
+
expect(box).to eq([
|
112
|
+
"┼────────┼\n",
|
113
|
+
"│ │\n",
|
114
|
+
"│ │\n",
|
115
|
+
"┼────────┼\n"
|
116
|
+
].join)
|
117
|
+
end
|
118
|
+
|
119
|
+
it "creates frame with all corners changed to dividers" do
|
120
|
+
box = TTY::Box.frame(
|
121
|
+
width: 10, height: 4,
|
122
|
+
border: {
|
123
|
+
top_left: :divider_down,
|
124
|
+
top_right: :divider_left,
|
125
|
+
bottom_left: :divider_right,
|
126
|
+
bottom_right: :divider_up
|
127
|
+
}
|
128
|
+
) { "hello" }
|
129
|
+
|
130
|
+
expect(box).to eq([
|
131
|
+
"┬────────┤\n",
|
132
|
+
"│hello │\n",
|
133
|
+
"│ │\n",
|
134
|
+
"├────────┴\n"
|
135
|
+
].join)
|
136
|
+
end
|
137
|
+
|
138
|
+
it "fails to recognise border value" do
|
139
|
+
expect {
|
140
|
+
TTY::Box.frame(border: {left: :unknown})
|
141
|
+
}.to raise_error(ArgumentError, "Invalid border value: 'unknown' for :left")
|
142
|
+
end
|
143
|
+
|
65
144
|
it "fails to recognise border option" do
|
66
145
|
expect {
|
67
146
|
TTY::Box.frame(width: 35, height: 4, border: [:unknown])
|
data/spec/unit/style_spec.rb
CHANGED
@@ -28,4 +28,94 @@ RSpec.describe TTY::Box, ":style option" do
|
|
28
28
|
"\e[4;1H\e[44m\e[93m╚\e[0m\e[0m\e[44m\e[93m══════════════\e[0m\e[0m\e[44m\e[93m══════════════\e[0m\e[0m\e[44m\e[93m╝\e[0m\e[0m"
|
29
29
|
].join)
|
30
30
|
end
|
31
|
+
|
32
|
+
it "creates box without corners and only color fill" do
|
33
|
+
box = TTY::Box.frame(
|
34
|
+
width: 10, height: 4,
|
35
|
+
border: {
|
36
|
+
top_left: false,
|
37
|
+
top_right: false,
|
38
|
+
bottom_left: false,
|
39
|
+
bottom_right: false,
|
40
|
+
},
|
41
|
+
style: {
|
42
|
+
fg: :bright_yellow,
|
43
|
+
bg: :blue,
|
44
|
+
}
|
45
|
+
)
|
46
|
+
|
47
|
+
expect(box).to eq([
|
48
|
+
"──────────\n",
|
49
|
+
"│\e[44m\e[93m \e[0m\e[0m│\n",
|
50
|
+
"│\e[44m\e[93m \e[0m\e[0m│\n",
|
51
|
+
"──────────\n"
|
52
|
+
].join)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "creates box without left & right borders and only color fill" do
|
56
|
+
box = TTY::Box.frame(
|
57
|
+
width: 10, height: 4,
|
58
|
+
border: {
|
59
|
+
left: false,
|
60
|
+
right: false
|
61
|
+
},
|
62
|
+
style: {
|
63
|
+
fg: :bright_yellow,
|
64
|
+
bg: :blue,
|
65
|
+
}
|
66
|
+
)
|
67
|
+
|
68
|
+
expect(box).to eq([
|
69
|
+
"──────────\n",
|
70
|
+
"\e[44m\e[93m \e[0m\e[0m\n",
|
71
|
+
"\e[44m\e[93m \e[0m\e[0m\n",
|
72
|
+
"──────────\n"
|
73
|
+
].join)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "creates box without top & bottom borders and only color fill" do
|
77
|
+
box = TTY::Box.frame(
|
78
|
+
width: 10, height: 4,
|
79
|
+
border: {
|
80
|
+
top: false,
|
81
|
+
bottom: false
|
82
|
+
},
|
83
|
+
style: {
|
84
|
+
fg: :bright_yellow,
|
85
|
+
bg: :blue,
|
86
|
+
}
|
87
|
+
)
|
88
|
+
|
89
|
+
expect(box).to eq([
|
90
|
+
"│\e[44m\e[93m \e[0m\e[0m│\n",
|
91
|
+
"│\e[44m\e[93m \e[0m\e[0m│\n",
|
92
|
+
"│\e[44m\e[93m \e[0m\e[0m│\n",
|
93
|
+
"│\e[44m\e[93m \e[0m\e[0m│\n",
|
94
|
+
].join)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "creates box without top & left borders and only color fill" do
|
98
|
+
box = TTY::Box.frame(
|
99
|
+
width: 10, height: 4,
|
100
|
+
border: {
|
101
|
+
top: false,
|
102
|
+
left: false
|
103
|
+
},
|
104
|
+
style: {
|
105
|
+
fg: :bright_yellow,
|
106
|
+
bg: :blue,
|
107
|
+
border: {
|
108
|
+
fg: :bright_yellow,
|
109
|
+
bg: :blue
|
110
|
+
}
|
111
|
+
}
|
112
|
+
)
|
113
|
+
|
114
|
+
expect(box).to eq([
|
115
|
+
"\e[44m\e[93m \e[0m\e[0m\e[44m\e[93m│\e[0m\e[0m\n",
|
116
|
+
"\e[44m\e[93m \e[0m\e[0m\e[44m\e[93m│\e[0m\e[0m\n",
|
117
|
+
"\e[44m\e[93m \e[0m\e[0m\e[44m\e[93m│\e[0m\e[0m\n",
|
118
|
+
"\e[44m\e[93m────\e[0m\e[0m\e[44m\e[93m─────\e[0m\e[0m\e[44m\e[93m┘\e[0m\e[0m\n"
|
119
|
+
].join)
|
120
|
+
end
|
31
121
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-box
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pastel
|
@@ -108,12 +108,14 @@ files:
|
|
108
108
|
- bin/console
|
109
109
|
- bin/setup
|
110
110
|
- examples/commander.rb
|
111
|
+
- examples/connected.rb
|
111
112
|
- lib/tty-box.rb
|
112
113
|
- lib/tty/box.rb
|
113
114
|
- lib/tty/box/border.rb
|
114
115
|
- lib/tty/box/version.rb
|
115
116
|
- spec/spec_helper.rb
|
116
117
|
- spec/unit/align_spec.rb
|
118
|
+
- spec/unit/border/parse_spec.rb
|
117
119
|
- spec/unit/border_spec.rb
|
118
120
|
- spec/unit/frame_spec.rb
|
119
121
|
- spec/unit/padding_spec.rb
|