tty-box 0.4.1 → 0.5.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: 5f6930141848be48d0af21960b3db9857af6aa5b22fa4bab1f3163406c9c1c54
4
- data.tar.gz: c05ed307504d3a16fc4f1aa00d4d622fd7566a27b65ed22a1309397e596d2bb6
3
+ metadata.gz: 4f4d14d40ab0bd1c8030ad5f1cbd706e2dd884ea9eb7f1a251e3b41b3839e7c6
4
+ data.tar.gz: 4236c0e2eca6dab6802360067eb31774abd70f9e82d0bb54e8d46d157338fc31
5
5
  SHA512:
6
- metadata.gz: 3c493c40374897de1b34b1295101ce33ff3ce401005c3fe300c314d31d413ff1b93fdbb1af29a1352fa6cebb12c5f45f46032b58ef68d4f9afe9b6d8892561df
7
- data.tar.gz: db800e05bef214944f5e2d6aff324b9cf9700de701652004ac90113148764079364f4457eed82df1cf7c28f322ff28d9b282a111f6cb9c0578c8ecde57b06008
6
+ metadata.gz: e02a01c723e2d3daa1353a7ba5cec72cbd6f657d26e8e24fc1b7f209b0010a3fa8805292bd3129c4c64c23e857395356d51ada371d0005e8ec6c030e8eb20989
7
+ data.tar.gz: 57296652a8991121efdccdd00bb74c6d2ca5068cb765236ff3f787e05162505b0c63ed27c2ae0282dddef1883330cc809dd31300ed5a4361d9c648cfbe5c472c
@@ -1,5 +1,15 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.5.0] - 2019-10-08
4
+
5
+ ### Added
6
+ * Add ability to create frames without specifying width or height
7
+ * Add #info, #warn, #success, #error ready frames for status messages inspired by conversation with Konstantin Gredeskoul(@kigster)
8
+
9
+ ### Changed
10
+ * Change #frame to accept content as an argument in addition to a block
11
+ * Change to match titles with border styling
12
+
3
13
  ## [v0.4.1] - 2019-08-28
4
14
 
5
15
  ### Added
@@ -42,6 +52,7 @@
42
52
 
43
53
  * Initial implementation and release
44
54
 
55
+ [v0.5.0]: https://github.com/piotrmurach/tty-box/compare/v0.4.1...v0.5.0
45
56
  [v0.4.1]: https://github.com/piotrmurach/tty-box/compare/v0.4.0...v0.4.1
46
57
  [v0.4.0]: https://github.com/piotrmurach/tty-box/compare/v0.3.0...v0.4.0
47
58
  [v0.3.0]: https://github.com/piotrmurach/tty-box/compare/v0.2.1...v0.3.0
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <div align="center">
2
- <a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://cdn.rawgit.com/piotrmurach/tty/master/images/tty.png" alt="tty logo" /></a>
2
+ <a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/blob/master/images/tty.png" alt="tty logo" /></a>
3
3
  </div>
4
4
 
5
5
  # TTY::Box [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
@@ -23,7 +23,7 @@
23
23
 
24
24
  **TTY::Box** provides box drawing component for [TTY](https://github.com/piotrmurach/tty) toolkit.
25
25
 
26
- ![Box drawing](https://cdn.rawgit.com/piotrmurach/tty-box/master/assets/tty-box-drawing.png)
26
+ ![Box drawing](https://github.com/piotrmurach/tty-box/blob/master/assets/tty-box-drawing.png)
27
27
 
28
28
  ## Installation
29
29
 
@@ -52,49 +52,92 @@ Or install it yourself as:
52
52
  * [2.5 border](#25-border)
53
53
  * [2.6 styling](#26-styling)
54
54
  * [2.7 formatting](#27-formatting)
55
+ * [2.8 messages](#28-messages)
56
+ * [2.8.1 info](#281-info)
57
+ * [2.8.2 warn](#282-warn)
58
+ * [2.8.3 success](#283-success)
59
+ * [2.8.4 error](#284-error)
55
60
 
56
61
  ## 1. Usage
57
62
 
58
63
  Using the `frame` method, you can draw a box in a terminal emulator:
59
64
 
60
65
  ```ruby
61
- box = TTY::Box.frame(
62
- width: 30,
63
- height: 10,
64
- align: :center,
65
- padding: 3
66
- ) do
67
- "Drawing a box in terminal emulator"
68
- end
69
-
66
+ box = TTY::Box.frame "Drawing a box in", "terminal emulator", padding: 3, align: :center
70
67
  ```
71
68
 
69
+ And then print:
70
+
72
71
  ```ruby
73
72
  print box
74
73
  # =>
75
- # ┌────────────────────────────┐
76
- # │
77
- # │
78
- # │
79
- # │ Drawing a box in
80
- # │ terminal emulator
81
- # │
82
- # │
83
- # │
84
- # └────────────────────────────┘
74
+ # ┌───────────────────────┐
75
+ # │
76
+ # │
77
+ # │
78
+ # │ Drawing a box in
79
+ # │ terminal emulator
80
+ # │
81
+ # │
82
+ # │
83
+ # └───────────────────────┘
85
84
  ```
86
85
 
87
86
  ## 2. Interface
88
87
 
89
88
  ### 2.1 frame
90
89
 
91
- You can draw a box in the top left corner of your terminal window by using the `frame` method and providing at the very minimum the height and the width:
90
+ You can draw a box in your terminal window by using the `frame` method and passing a content to display. By default the box will be drawn around the content.
91
+
92
+ ```ruby
93
+ print TTY::Box.frame "Hello world!"
94
+ # =>
95
+ # ┌────────────┐
96
+ # │Hello world!│
97
+ # └────────────┘
98
+ ```
99
+
100
+ You can also provide multi line content as separate arguments.
101
+
102
+ ```ruby
103
+ print TTY::Box.frame "Hello", "world!"
104
+ # =>
105
+ # ┌──────┐
106
+ # │Hello │
107
+ # │world!│
108
+ # └──────┘
109
+ ```
110
+
111
+ Alternatively, provide a multi line content using newline chars in a single argument:
112
+
113
+ ```ruby
114
+ print TTY::Box.frame "Hello\nworld!"
115
+ # =>
116
+ # ┌──────┐
117
+ # │Hello │
118
+ # │world!│
119
+ # └──────┘
120
+ ```
121
+
122
+ Finally, you can use a block to specify content:
123
+
124
+ ```ruby
125
+ print TTY::Box.frame do
126
+ "Hello world!"
127
+ end
128
+ # =>
129
+ # ┌────────────┐
130
+ # │Hello world!│
131
+ # └────────────┘
132
+ ```
133
+
134
+ You can also enforce a given box size without any content and use [tty-cursor](https://github.com/piotrmurach/tty-cursor) to position content whatever you like.
92
135
 
93
136
  ```ruby
94
137
  box = TTY::Box.frame(width: 30, height: 10)
95
138
  ```
96
139
 
97
- which when printed will prodcue the following output in your terminal:
140
+ When printed will produce the following output in your terminal:
98
141
 
99
142
  ```ruby
100
143
  print box
@@ -111,8 +154,6 @@ print box
111
154
  # └────────────────────────────┘
112
155
  ```
113
156
 
114
- Then you can use [tty-cursor](https://github.com/piotrmurach/tty-cursor) to directly manipulate content to be displayed inside the box.
115
-
116
157
  Alternatively, you can also pass a block to provide a content for the box:
117
158
 
118
159
  ```ruby
@@ -121,7 +162,7 @@ box = TTY::Box.frame(width: 30, height: 10) do
121
162
  end
122
163
  ```
123
164
 
124
- which when printed will produce the following output in your terminal:
165
+ When printed will produce the following output in your terminal:
125
166
 
126
167
  ```ruby
127
168
  print box
@@ -357,6 +398,90 @@ print box
357
398
  #
358
399
  ```
359
400
 
401
+ ### 2.8 messages
402
+
403
+ ![Box messages](https://github.com/piotrmurach/tty-box/blob/master/assets/tty-box-messages.png)
404
+
405
+ #### 2.8.1 info
406
+
407
+ To draw an information type box around your content use `info`:
408
+
409
+ ```ruby
410
+ box = TTY::Box.info("Deploying application")
411
+ ```
412
+
413
+ And then print:
414
+
415
+ ```ruby
416
+ print box
417
+ # =>
418
+ # ╔ ℹ INFO ═══════════════╗
419
+ # ║ ║
420
+ # ║ Deploying application ║
421
+ # ║ ║
422
+ # ╚═══════════════════════╝
423
+ ```
424
+
425
+ #### 2.8.2 warn
426
+
427
+ To draw a warning type box around your content use `warn`:
428
+
429
+ ```ruby
430
+ box = TTY::Box.warn("Deploying application")
431
+ ```
432
+
433
+ And then print:
434
+
435
+ ```ruby
436
+ print box
437
+ # =>
438
+ # ╔ ⚠ WARNING ════════════╗
439
+ # ║ ║
440
+ # ║ Deploying application ║
441
+ # ║ ║
442
+ # ╚═══════════════════════╝
443
+ ```
444
+
445
+ #### 2.8.3 success
446
+
447
+ To draw a success type box around your content use `success`:
448
+
449
+ ```ruby
450
+ box = TTY::Box.success("Deploying application")
451
+ ```
452
+
453
+ And then print:
454
+
455
+ ```ruby
456
+ print box
457
+ # =>
458
+ # ╔ ✔ OK ═════════════════╗
459
+ # ║ ║
460
+ # ║ Deploying application ║
461
+ # ║ ║
462
+ # ╚═══════════════════════╝
463
+ ```
464
+
465
+ #### 2.8.4 error
466
+
467
+ To draw an error type box around your content use `error`:
468
+
469
+ ```ruby
470
+ box = TTY::Box.error("Deploying application")
471
+ ```
472
+
473
+ And then print:
474
+
475
+ ```ruby
476
+ print box
477
+ # =>
478
+ # ╔ ⨯ ERROR ══════════════╗
479
+ # ║ ║
480
+ # ║ Deploying application ║
481
+ # ║ ║
482
+ # ╚═══════════════════════╝
483
+ ```
484
+
360
485
  ## Development
361
486
 
362
487
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../lib/tty-box"
4
+
5
+ box = TTY::Box.frame "Drawing a box in", "terminal emulator",
6
+ padding: 3, align: :center
7
+
8
+ puts box
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../lib/tty-box"
4
+
5
+ puts TTY::Cursor.clear_screen
6
+ puts TTY::Box.info "Deploying application", top: 2, left: 2
7
+ puts TTY::Box.success "Deploying application", top: 2, left: 29
8
+ puts TTY::Box.warn "Deploying application", top: 8, left: 2
9
+ puts TTY::Box.error "Deploying application", top: 8, left: 29
10
+
11
+ puts
@@ -73,13 +73,123 @@ module TTY
73
73
  @color ||= Pastel.new
74
74
  end
75
75
 
76
+ # A frame for info type message
77
+ #
78
+ # @param [String] message
79
+ # the message to display
80
+ #
81
+ # @api public
82
+ def info(message, **opts)
83
+ new_opts = {
84
+ title: { top_left: " ℹ INFO " },
85
+ border: { type: :thick },
86
+ padding: 1,
87
+ style: {
88
+ fg: :black,
89
+ bg: :bright_blue,
90
+ border: {
91
+ fg: :black,
92
+ bg: :bright_blue
93
+ }
94
+ }
95
+ }.merge(opts)
96
+ frame(**new_opts) { message }
97
+ end
98
+
99
+ # A frame for warning type message
100
+ #
101
+ # @param [String] message
102
+ # the message to display
103
+ #
104
+ # @api public
105
+ def warn(message, **opts)
106
+ new_opts = {
107
+ title: { top_left: " ⚠ WARNING " },
108
+ border: { type: :thick },
109
+ padding: 1,
110
+ style: {
111
+ fg: :black,
112
+ bg: :bright_yellow,
113
+ border: {
114
+ fg: :black,
115
+ bg: :bright_yellow
116
+ }
117
+ }
118
+ }.merge(opts)
119
+ frame(**new_opts) { message }
120
+ end
121
+
122
+ # A frame for for success type message
123
+ #
124
+ # @param [String] message
125
+ # the message to display
126
+ #
127
+ # @api public
128
+ def success(message, **opts)
129
+ new_opts = {
130
+ title: { top_left: " ✔ OK " },
131
+ border: { type: :thick },
132
+ padding: 1,
133
+ style: {
134
+ fg: :black,
135
+ bg: :bright_green,
136
+ border: {
137
+ fg: :black,
138
+ bg: :bright_green,
139
+ }
140
+ }
141
+ }.merge(opts)
142
+ frame(**new_opts) { message }
143
+ end
144
+
145
+ # A frame for error type message
146
+ #
147
+ # @param [String] message
148
+ # the message to display
149
+ #
150
+ # @api public
151
+ def error(message, **opts)
152
+ new_opts = {
153
+ title: { top_left: " ⨯ ERROR " },
154
+ border: { type: :thick },
155
+ padding: 1,
156
+ style: {
157
+ fg: :bright_white,
158
+ bg: :red,
159
+ border: {
160
+ fg: :bright_white,
161
+ bg: :red
162
+ }
163
+ }
164
+ }.merge(opts)
165
+ frame(**new_opts) { message }
166
+ end
167
+
76
168
  # Create a frame
77
169
  #
170
+ # @param [Integer] top
171
+ # the offset from the terminal top
172
+ # @param [Integer] left
173
+ # the offset from the terminal left
174
+ # @param [Integer] width
175
+ # the width of the box
176
+ # @param [Integer] height
177
+ # the height of the box
178
+ # @param [Symbol] align
179
+ # the content alignment
180
+ # @param [Integer,Array[Integer]] padding
181
+ # the padding around content
182
+ # @param [Hash] title
183
+ # the title for top or bottom border
184
+ # @param [Hash, Symbol] border
185
+ # the border type
186
+ # @param [Hash] style
187
+ # the styling for the front and background
188
+ #
78
189
  # @api public
79
- def frame(top: nil, left: nil, width: 35, height: 3, align: :left, padding: 0,
80
- title: {}, border: :light, style: {})
190
+ def frame(*content, top: nil, left: nil, width: nil, height: nil, align: :left,
191
+ padding: 0, title: {}, border: :light, style: {})
81
192
  output = []
82
- content = []
83
193
  sep = NEWLINE
84
194
  position = top && left
85
195
 
@@ -91,10 +201,22 @@ module TTY
91
201
 
92
202
  if block_given?
93
203
  str = yield
94
- sep = str[LINE_BREAK] || NEWLINE # infer line break
95
- content = format(str, width, padding, align)
204
+ else
205
+ str = case content.size
206
+ when 0 then ""
207
+ when 1 then content[0]
208
+ else content.join(NEWLINE)
209
+ end
96
210
  end
97
211
 
212
+ sep = str[LINE_BREAK] || NEWLINE # infer line break
213
+ lines = str.split(sep)
214
+ # infer dimensions
215
+ dimensions = infer_dimensions(lines, padding)
216
+ width ||= left_size + dimensions[0] + right_size
217
+ height ||= top_size + dimensions[1] + bottom_size
218
+ content = format(str, width, padding, align) # adjust content
219
+ # infer styling
98
220
  fg, bg = *extract_style(style)
99
221
  border_fg, border_bg = *extract_style(style[:border] || {})
100
222
 
@@ -117,7 +239,7 @@ module TTY
117
239
  content_size -= size
118
240
  end
119
241
  if style[:fg] || style[:bg] || !position # something to color
120
- output << bg.(fg.(' ' * content_size))
242
+ output << bg.(fg.(" " * content_size))
121
243
  end
122
244
 
123
245
  if border.right?
@@ -138,12 +260,31 @@ module TTY
138
260
  output.join
139
261
  end
140
262
 
263
+ # Infer box dimensions based on content lines and padding
264
+ #
265
+ # @param [Array[String]] lines
266
+ # @param [Array[Integer]|Integer] padding
267
+ #
268
+ # @return [Array[Integer]]
269
+ #
270
+ # @api private
271
+ def infer_dimensions(lines, padding)
272
+ pad = Strings::Padder.parse(padding)
273
+ content_height = lines.size
274
+ content_width = lines.empty? ? 1 : lines.max_by(&:length).length
275
+ width = pad.left + content_width + pad.right
276
+ height = pad.top + content_height + pad.bottom
277
+ [width, height]
278
+ end
279
+
141
280
  # Format content
142
281
  #
143
282
  # @return [Array[String]]
144
283
  #
145
284
  # @api private
146
285
  def format(content, width, padding, align)
286
+ return "" if content.to_s.empty?
287
+
147
288
  pad = Strings::Padder.parse(padding)
148
289
  total_width = width - 2 - (pad.left + pad.right)
149
290
  sep = content[LINE_BREAK] || NEWLINE # infer line break
@@ -185,11 +326,11 @@ module TTY
185
326
 
186
327
  [
187
328
  bg.(fg.(top_left)),
188
- bg.(title[:top_left].to_s),
329
+ bg.(fg.(title[:top_left].to_s)),
189
330
  bg.(fg.(line_char(border.type) * top_space_before)),
190
- bg.(title[:top_center].to_s),
331
+ bg.(fg.(title[:top_center].to_s)),
191
332
  bg.(fg.(line_char(border.type) * top_space_after)),
192
- bg.(title[:top_right].to_s),
333
+ bg.(fg.(title[:top_right].to_s)),
193
334
  bg.(fg.(top_right))
194
335
  ].join('')
195
336
  end
@@ -215,11 +356,11 @@ module TTY
215
356
 
216
357
  [
217
358
  bg.(fg.(bottom_left)),
218
- bg.(title[:bottom_left].to_s),
359
+ bg.(fg.(title[:bottom_left].to_s)),
219
360
  bg.(fg.(line_char(border.type) * bottom_space_before)),
220
- bg.(title[:bottom_center].to_s),
361
+ bg.(fg.(title[:bottom_center].to_s)),
221
362
  bg.(fg.(line_char(border.type) * bottom_space_after)),
222
- bg.(title[:bottom_right].to_s),
363
+ bg.(fg.(title[:bottom_right].to_s)),
223
364
  bg.(fg.(bottom_right))
224
365
  ].join('')
225
366
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  module Box
5
- VERSION = "0.4.1"
5
+ VERSION = "0.5.0"
6
6
  end # Box
7
7
  end # TTY
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe TTY::Box, ':align option' do
2
4
  it "aligns content without positioning" do
3
5
  box = TTY::Box.frame(width: 26, height: 4, align: :center) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe TTY::Box, ':border option' do
2
4
  it "creates frame with double lines and no position" do
3
5
  box = TTY::Box.frame(
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe TTY::Box, "custom frames" do
4
+ it "draws info type message" do
5
+ box = TTY::Box.info("Hello world!")
6
+
7
+ expect(box).to eq([
8
+ "\e[104m\e[30m╔\e[0m\e[0m\e[104m\e[30m ℹ INFO \e[0m\e[0m\e[104m\e[30m═══\e[0m\e[0m\e[104m\e[30m═══\e[0m\e[0m\e[104m\e[30m╗\e[0m\e[0m\n",
9
+ "\e[104m\e[30m║\e[0m\e[0m\e[104m\e[30m \e[0m\e[0m\e[104m\e[30m║\e[0m\e[0m\n",
10
+ "\e[104m\e[30m║\e[0m\e[0m\e[104m\e[30m Hello world! \e[0m\e[0m\e[104m\e[30m║\e[0m\e[0m\n",
11
+ "\e[104m\e[30m║\e[0m\e[0m\e[104m\e[30m \e[0m\e[0m\e[104m\e[30m║\e[0m\e[0m\n",
12
+ "\e[104m\e[30m╚\e[0m\e[0m\e[104m\e[30m═══════\e[0m\e[0m\e[104m\e[30m═══════\e[0m\e[0m\e[104m\e[30m╝\e[0m\e[0m\n"
13
+ ].join)
14
+ end
15
+
16
+ it "draws warning type message" do
17
+ box = TTY::Box.warn("Hello world!")
18
+
19
+ expect(box).to eq([
20
+ "\e[103m\e[30m╔\e[0m\e[0m\e[103m\e[30m ⚠ WARNING \e[0m\e[0m\e[103m\e[30m═\e[0m\e[0m\e[103m\e[30m══\e[0m\e[0m\e[103m\e[30m╗\e[0m\e[0m\n",
21
+ "\e[103m\e[30m║\e[0m\e[0m\e[103m\e[30m \e[0m\e[0m\e[103m\e[30m║\e[0m\e[0m\n",
22
+ "\e[103m\e[30m║\e[0m\e[0m\e[103m\e[30m Hello world! \e[0m\e[0m\e[103m\e[30m║\e[0m\e[0m\n",
23
+ "\e[103m\e[30m║\e[0m\e[0m\e[103m\e[30m \e[0m\e[0m\e[103m\e[30m║\e[0m\e[0m\n",
24
+ "\e[103m\e[30m╚\e[0m\e[0m\e[103m\e[30m═══════\e[0m\e[0m\e[103m\e[30m═══════\e[0m\e[0m\e[103m\e[30m╝\e[0m\e[0m\n"
25
+ ].join)
26
+ end
27
+
28
+ it "draws success type message" do
29
+ box = TTY::Box.success("Hello world!")
30
+
31
+ expect(box).to eq([
32
+ "\e[102m\e[30m╔\e[0m\e[0m\e[102m\e[30m ✔ OK \e[0m\e[0m\e[102m\e[30m════\e[0m\e[0m\e[102m\e[30m════\e[0m\e[0m\e[102m\e[30m╗\e[0m\e[0m\n",
33
+ "\e[102m\e[30m║\e[0m\e[0m\e[102m\e[30m \e[0m\e[0m\e[102m\e[30m║\e[0m\e[0m\n",
34
+ "\e[102m\e[30m║\e[0m\e[0m\e[102m\e[30m Hello world! \e[0m\e[0m\e[102m\e[30m║\e[0m\e[0m\n",
35
+ "\e[102m\e[30m║\e[0m\e[0m\e[102m\e[30m \e[0m\e[0m\e[102m\e[30m║\e[0m\e[0m\n",
36
+ "\e[102m\e[30m╚\e[0m\e[0m\e[102m\e[30m═══════\e[0m\e[0m\e[102m\e[30m═══════\e[0m\e[0m\e[102m\e[30m╝\e[0m\e[0m\n"
37
+ ].join)
38
+ end
39
+
40
+ it "draws error type message" do
41
+ box = TTY::Box.error("Hello world!")
42
+
43
+ expect(box).to eq([
44
+ "\e[41m\e[97m╔\e[0m\e[0m\e[41m\e[97m ⨯ ERROR \e[0m\e[0m\e[41m\e[97m══\e[0m\e[0m\e[41m\e[97m═══\e[0m\e[0m\e[41m\e[97m╗\e[0m\e[0m\n",
45
+ "\e[41m\e[97m║\e[0m\e[0m\e[41m\e[97m \e[0m\e[0m\e[41m\e[97m║\e[0m\e[0m\n",
46
+ "\e[41m\e[97m║\e[0m\e[0m\e[41m\e[97m Hello world! \e[0m\e[0m\e[41m\e[97m║\e[0m\e[0m\n",
47
+ "\e[41m\e[97m║\e[0m\e[0m\e[41m\e[97m \e[0m\e[0m\e[41m\e[97m║\e[0m\e[0m\n",
48
+ "\e[41m\e[97m╚\e[0m\e[0m\e[41m\e[97m═══════\e[0m\e[0m\e[41m\e[97m═══════\e[0m\e[0m\e[41m\e[97m╝\e[0m\e[0m\n"
49
+ ].join)
50
+ end
51
+ end
@@ -1,6 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe TTY::Box, "#frame" do
4
+ it "creates frame without content & width & height values" do
5
+ output = TTY::Box.frame
6
+
7
+ expect(output).to eq([
8
+ "┌─┐\n",
9
+ "└─┘\n"
10
+ ].join)
11
+ end
12
+
13
+ it "creates frame around content without width & height values" do
14
+ output = TTY::Box.frame "Hello world!"
15
+
16
+ expect(output).to eq([
17
+ "┌────────────┐\n",
18
+ "│Hello world!│\n",
19
+ "└────────────┘\n"
20
+ ].join)
21
+ end
22
+
23
+ it "creates frame around content without width & height values" do
24
+ output = TTY::Box.frame "Hello\nworld!"
25
+
26
+ expect(output).to eq([
27
+ "┌──────┐\n",
28
+ "│Hello │\n",
29
+ "│world!│\n",
30
+ "└──────┘\n"
31
+ ].join)
32
+ end
33
+
34
+ it "creates frame based on multiline content without width & height values" do
35
+ output = TTY::Box.frame "Hello", "world!"
36
+
37
+ expect(output).to eq([
38
+ "┌──────┐\n",
39
+ "│Hello │\n",
40
+ "│world!│\n",
41
+ "└──────┘\n"
42
+ ].join)
43
+ end
44
+
45
+ it "creates frame around block content without width & height values" do
46
+ output = TTY::Box.frame do
47
+ "Hello world!"
48
+ end
49
+
50
+ expect(output).to eq([
51
+ "┌────────────┐\n",
52
+ "│Hello world!│\n",
53
+ "└────────────┘\n"
54
+ ].join)
55
+ end
4
56
  it "creates frame with only width & height values" do
5
57
  output = TTY::Box.frame(width: 35, height: 4)
6
58
 
@@ -1,6 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- RSpec.describe TTY::Box, ':padding option' do
3
+ RSpec.describe TTY::Box, ":padding option" do
4
+ it "padds internal content without width and height" do
5
+ box = TTY::Box.frame(padding: 1) do
6
+ "Drawing a box in terminal emulator"
7
+ end
8
+
9
+ expect(box).to eq([
10
+ "┌────────────────────────────────────┐\n",
11
+ "│ │\n",
12
+ "│ Drawing a box in terminal emulator │\n",
13
+ "│ │\n",
14
+ "└────────────────────────────────────┘\n"
15
+ ].join)
16
+ end
17
+
4
18
  it "padds internal content without position arguments" do
5
19
  box = TTY::Box.frame(width: 30, height: 6, padding: 1) do
6
20
  "Drawing a box in terminal emulator"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe TTY::Box, ':top, :left options' do
2
4
  it "skips positioning when no top & left values provided" do
3
5
  output = TTY::Box.frame(width: 35, height: 4)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe TTY::Box, ":style option" do
2
4
  it "applies styling to content and border" do
3
5
  box = TTY::Box.frame(
@@ -22,7 +24,7 @@ RSpec.describe TTY::Box, ":style option" do
22
24
  end
23
25
 
24
26
  expect(box).to eq([
25
- "\e[1;1H\e[44m\e[93m╔\e[0m\e[0m\e[44m file1 \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",
27
+ "\e[1;1H\e[44m\e[93m╔\e[0m\e[0m\e[44m\e[93m file1 \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",
26
28
  "\e[2;1H\e[44m\e[93m║\e[0m\e[0m\e[44m\e[93mMidnight Commander \e[0m\e[0m\e[2;30H\e[44m\e[93m║\e[0m\e[0m",
27
29
  "\e[3;1H\e[44m\e[93m║\e[0m\e[0m\e[44m\e[93mis the best \e[0m\e[0m\e[3;30H\e[44m\e[93m║\e[0m\e[0m",
28
30
  "\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"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe TTY::Box, ':title option' do
2
4
  it "allows to specify top border titles" do
3
5
  output = TTY::Box.frame(
@@ -12,13 +12,13 @@ Gem::Specification.new do |spec|
12
12
  spec.homepage = "https://piotrmurach.github.io/tty"
13
13
  spec.license = "MIT"
14
14
 
15
- spec.files = Dir['{lib,spec,examples}/**/*.rb']
16
- spec.files += Dir['{bin,tasks}/*', 'tty-box.gemspec']
17
- spec.files += Dir['README.md', 'CHANGELOG.md', 'LICENSE.txt', 'Rakefile']
15
+ spec.files = Dir["{lib,spec,examples}/**/*.rb"]
16
+ spec.files += Dir["{bin,tasks}/*", "tty-box.gemspec"]
17
+ spec.files += Dir["README.md", "CHANGELOG.md", "LICENSE.txt", "Rakefile"]
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.required_ruby_version = '>= 2.0.0'
21
+ spec.required_ruby_version = ">= 2.0.0"
22
22
 
23
23
  spec.add_dependency "pastel", "~> 0.7.2"
24
24
  spec.add_dependency "tty-cursor", "~> 0.7"
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.1
4
+ version: 0.5.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: 2019-08-28 00:00:00.000000000 Z
11
+ date: 2019-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pastel
@@ -107,8 +107,10 @@ files:
107
107
  - Rakefile
108
108
  - bin/console
109
109
  - bin/setup
110
+ - examples/basic.rb
110
111
  - examples/commander.rb
111
112
  - examples/connected.rb
113
+ - examples/messages.rb
112
114
  - examples/newline.rb
113
115
  - lib/tty-box.rb
114
116
  - lib/tty/box.rb
@@ -118,6 +120,7 @@ files:
118
120
  - spec/unit/align_spec.rb
119
121
  - spec/unit/border/parse_spec.rb
120
122
  - spec/unit/border_spec.rb
123
+ - spec/unit/custom_frame_spec.rb
121
124
  - spec/unit/frame_spec.rb
122
125
  - spec/unit/padding_spec.rb
123
126
  - spec/unit/position_spec.rb