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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b0c9524c939271bcc5168d3cc467b790657109218f53284a6328db8173225b8
4
- data.tar.gz: cfe75b8c4b2f3567e03bd511ecd5a914fcda3180ab4339fb82412255cfd24206
3
+ metadata.gz: de83dc517a44cecc9c6936beafcc7a9e92bbef44b9f835ce8c5f537a34fcfb99
4
+ data.tar.gz: 4e5a152bf222a4a1a267fa97699b8f20cbe33be48c9d7847b83172dc26708c2a
5
5
  SHA512:
6
- metadata.gz: 0c5988a4893aa1c2411429c40d88445ad51a3a3306ecde927125868aa2741686835eabbebe538ea8338639981cf0985318d48e27f586057eb73b492b7797996d
7
- data.tar.gz: ce051a4e7751ecd3a8caaa50ff626fb4d7929bdb348025c41ed2324c74cd5b055225112d85bb2ffdb90420d05c36b8308131e4756f652eefb4755781e7bd94bc
6
+ metadata.gz: 8979a9ad2c06c9ff1762005feabfbdf28456e75a496c3a39a4276b7fe73a31fc9bb69c8941d4470efb8c2a7277bdbdce62de7c788f95622a24c299cd015a2e72
7
+ data.tar.gz: 99f56b7376c5d96b933e1deb3f7282d37e7b7ce10130240516dc3408387feb38edb567de17ee72a96e9c1e5de49b3cde2c3f2d35fd0ff6cfc22767e1a010fb9b
@@ -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 two types of border `:light` and `:thick`. By default the `:light` border is used. This can be changed using the `:border` keyword:
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 switch off border sides by passing options hash. For example to remove bottom border do:
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.new(width: 30, height: 10, border: {type: :thick, bottom: false})
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
@@ -1,4 +1,6 @@
1
- require 'tty-box'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/tty-box'
2
4
 
3
5
  print TTY::Cursor.clear_screen
4
6
 
@@ -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
@@ -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 line_char(border = :light)
20
- BOX_CHARS[border][8]
20
+ def corner_bottom_right_char(border = :light)
21
+ BOX_CHARS[border][0]
21
22
  end
22
23
 
23
- def pipe_char(border = :light)
24
- BOX_CHARS[border][9]
24
+ def corner_top_right_char(border = :light)
25
+ BOX_CHARS[border][1]
25
26
  end
26
27
 
27
- def cross_char(border = :light)
28
- BOX_CHARS[border][10]
28
+ def corner_top_left_char(border = :light)
29
+ BOX_CHARS[border][2]
29
30
  end
30
31
 
31
- def left_divider_char(border = :light)
32
- BOX_CHARS[border][7]
32
+ def corner_bottom_left_char(border = :light)
33
+ BOX_CHARS[border][3]
33
34
  end
34
35
 
35
- def right_divider_char(border = :light)
36
+ def divider_left_char(border = :light)
36
37
  BOX_CHARS[border][4]
37
38
  end
38
39
 
39
- def top_left_char(border = :light)
40
- BOX_CHARS[border][2]
40
+ def divider_up_char(border = :light)
41
+ BOX_CHARS[border][5]
41
42
  end
42
43
 
43
- def top_divider_char(border = :light)
44
+ def divider_down_char(border = :light)
44
45
  BOX_CHARS[border][6]
45
46
  end
46
47
 
47
- def top_right_char(border = :light)
48
- BOX_CHARS[border][1]
48
+ def divider_right_char(border = :light)
49
+ BOX_CHARS[border][7]
49
50
  end
50
51
 
51
- def bottom_left_char(border = :light)
52
- BOX_CHARS[border][3]
52
+ def line_char(border = :light)
53
+ BOX_CHARS[border][8]
53
54
  end
54
55
 
55
- def bottom_divider_char(border = :light)
56
- BOX_CHARS[border][5]
56
+ def pipe_char(border = :light)
57
+ BOX_CHARS[border][9]
57
58
  end
58
59
 
59
- def bottom_right_char(border = :light)
60
- BOX_CHARS[border][0]
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.type, style)
96
+ output << top_border(title, width, border, style)
92
97
  output << "\n" unless position
93
98
  end
94
- (height - 2).times do |i|
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
- if content[i].nil? && (style[:fg] || style[:bg] || !position)
100
- output << bg.(fg.(' ' * (width - 2)))
101
- else
105
+
106
+ content_size = width - left_size - right_size
107
+ unless content[i].nil?
102
108
  output << bg.(fg.(content[i]))
103
- if style[:fg] || style[:bg]
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
- output << cursor.move_to(left + width - 1, top + i + 1) if position
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 - 1) if position
115
- output << bottom_border(title, width, border.type, style)
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
- top_space_left = width - top_titles_size -
160
- top_left_char.size - top_right_char.size
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 = top_space_left / 2 + top_space_left % 2
175
+ top_space_after = top_space_left / 2 + top_space_left % 2
163
176
 
164
177
  [
165
- bg.(fg.(top_left_char(border))),
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.(top_right_char(border)))
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
- bottom_left_char.size - bottom_right_char.size
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.(bottom_left_char(border))),
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.(bottom_right_char(border)))
214
+ bg.(fg.(bottom_right))
199
215
  ].join('')
200
216
  end
201
217
  end # TTY
@@ -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.fetch(:type, :light),
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, true, true, true, true)
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, :bottom
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, top, left, right, bottom)
31
- @type = type
32
- @top = top
33
- @left = left
34
- @right = right
35
- @bottom = bottom
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  module Box
5
- VERSION = "0.2.1"
5
+ VERSION = "0.3.0"
6
6
  end # Box
7
7
  end # TTY
@@ -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
@@ -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: 35, height: 4,
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[2;1H║Hello Piotr! \e[2;35H║",
44
- "\e[3;1H║\e[3;35H║",
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: 35, height: 4,
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
- "Hello Piotr! ",
61
- "\e[4;1H└─────────────────────────────────┘"
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])
@@ -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.2.1
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-09-10 00:00:00.000000000 Z
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