/
boxes.jl
504 lines (431 loc) · 10.5 KB
/
boxes.jl
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
module Boxes
import Term: rint, chars, join_lines, loop_last, textlen, get_lr_widths, str_trunc
import ..Style: apply_style
import ..Segments: Segment
export get_row, get_title_row
export BOXES
# ---------------------------------------------------------------------------- #
# BOX #
# ---------------------------------------------------------------------------- #
"""
BoxLine
Stores the characters for a line of a `Box` object.
"""
struct BoxLine
left::Char
mid::Char
vertical::Char
right::Char
end
"""
Box
Defines characters to render boxes.
Row names:
┌─┬┐ top
│ ││ head
├─┼┤ head_row
│ ││ mid
├─┼┤ row
├─┼┤ foot_row
│ ││ foot
└─┴┘ bottom
each row is an instance of `BoxLine`
"""
struct Box
name::String
top::BoxLine
head::BoxLine
head_row::BoxLine
mid::BoxLine
row::BoxLine
foot_row::BoxLine
foot::BoxLine
bottom::BoxLine
end
"""
Box(string)
Construct a `Box` objet out of a box string.
"""
function Box(box_name::String, box::String)
top, head, head_row, mid, row, foot_row, foot, bottom = split(box, "\n")
return Box(
box_name,
BoxLine(chars(top)...),
BoxLine(chars(head)...),
BoxLine(chars(head_row)...),
BoxLine(chars(mid)...),
BoxLine(chars(row)...),
BoxLine(chars(foot_row)...),
BoxLine(chars(foot)...),
BoxLine(chars(bottom)...),
)
end
function Base.show(io::IO, box::Box)
if io ≡ stdout
print(io, "Box ($(box.name))\n$(fit(box, [1, 3, 1]))")
else
print(io, "Box\e[2m($(box.name))\e[0m")
end
end
"""
get_row(box, [1, 2, 3], :row)
Gets characters for a row of a Box object.
The level Symbol can be used to specify the box level (:top, :footer...).
The total width will be the sum of the widths +2
"""
function get_row(box::Box, widths::Vector{Int}, level::Symbol)::String
# get the correct level of the box
level = getfield(box, level)
line = level.left
for (last, w) in loop_last(widths)
segment = last ? level.mid^w * level.right : level.mid^w * level.vertical
line *= segment
end
return line
end
"""
get_row(box::Box, width::Int, level::Symbol)::String
Get a box's row of given width.
"""
function get_row(box::Box, width::Int, level::Symbol)::String
level = getfield(box, level)
return level.left * level.mid^(width - 2) * level.right
end
"""
get_lrow(box::Box, width::Int, level::Symbol)::String
Get a box's row's left part (no righ char)
Get a box's row's right part (no left char)
See also [`get_row`](@ref), [`get_rrow`](@ref).
"""
function get_lrow(box::Box, width::Int, level::Symbol; with_left = true)::String
level = getfield(box, level)
return if with_left
level.left * level.mid^(width - 1)
else
level.mid^(width - 1)
end
end
"""
get_rrow(box::Box, width::Int, level::Symbol)::String
Get a box's row's right part (no left char)
See also [`get_row`](@ref), [`get_lrow`](@ref).
"""
function get_rrow(box::Box, width::Int, level::Symbol; with_right = true)::String
level = getfield(box, level)
return if with_right
level.mid^(width - 1) * level.right
else
level.mid^(width - 1)
end
end
"""
get_title_row(row::Symbol, box::Box, title::Union{Nothing, AbstractString}; <keyword arguments>)
Create a box row with a title string.
Can create both titles in the top and bottom row to produce subtitles.
#Arguments:
- width::Int: width of line
- style::String: style of line
- title_style::String: style of title string
- justify::Symbol=:left: position of title string
See also [`get_row`](@ref).
"""
function get_title_row(
row::Symbol,
box, # ::Box,
title::Union{Nothing,String};
width::Int = default_width(),
style::String = "default",
title_style::Union{Nothing,String} = nothing,
justify::Symbol = :left,
)::Segment
# if no title or no space, just return a row
if isnothing(title) || width < 12
return Segment("\e[0m{$style}" * get_row(box, width, row) * "{/$(style)}\e[0m")
else
title = apply_style(title)
title =
(textlen(title) < width - 12 || textlen(title) <= 4) ? title :
str_trunc(title, max(1, width - 15))
end
# compose title line
boxline = getfield(box, row)
open, close, space = "{" * style * "}", "{/" * style * "}\e[0m", " "
topen, tclose = "\e[0m", open
if !isnothing(title_style)
topen, tclose = if style == "hidden"
"\e[28m" * "{" * title_style * "}", "{/" * title_style * "}" * open * "\e[8m"
else
"{" * title_style * "}", "{/" * title_style * "}" * open
end
end
# get the title
title = space * topen * title * tclose * space
# only add the title if the box is wide enough
if width < 6
line = open * boxline.left * boxline.mid^(width - 2) * boxline.right * close
else
# close the box
if justify ≡ :left
line = open * boxline.left * boxline.mid^4 * title
# check if the title is too long
if width - textlen(line) - 1 < 1
line = open * boxline.left * boxline.mid^2 * title
end
line *= boxline.mid^(max(1, width - textlen(line) - 1)) * boxline.right * close
elseif justify ≡ :right
pre_len = width - textlen(title) - 4
line = open * get_lrow(box, pre_len, row)
line *= title * boxline.mid^3 * boxline.right * close
else # justify :center
tl, tr = get_lr_widths(textlen(title))
lw, rw = get_lr_widths(width)
line =
open *
get_lrow(box, lw - tl, row) *
title *
get_rrow(box, rw - tr, row) *
close
return Segment(line)
end
end
return Segment(line)
end
"""
fit(box::Box, widths::Vector{Int})::String
Creates a box.
The box has one of each level type with columns
widths specified by a vector of widhts.
"""
fit(box::Box, widths::Vector{Int}) = join_lines([
get_row(box, widths, :top),
get_row(box, widths, :head),
get_row(box, widths, :head_row),
get_row(box, widths, :mid),
get_row(box, widths, :row),
get_row(box, widths, :foot_row),
get_row(box, widths, :foot),
get_row(box, widths, :bottom),
])
# ---------------------------------------------------------------------------- #
# Box types #
# ---------------------------------------------------------------------------- #
const BOXES = (
NONE = Box(
"NONE",
"""
""",
),
ASCII = Box(
"ASCII",
"""
+--+
| ||
|-+|
| ||
|-+|
|-+|
| ||
+--+
""",
),
ASCII2 = Box(
"ASCII2",
"""
+-++
| ||
+-++
| ||
+-++
+-++
| ||
+-++
""",
),
ASCII_DOUBLE_HEAD = Box(
"ASCII_DOUBLE_HEAD",
"""
+-++
| ||
+=++
| ||
+-++
+-++
| ||
+-++
""",
),
SQUARE = Box(
"SQUARE",
"""
┌─┬┐
│ ││
├─┼┤
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘
""",
),
SQUARE_DOUBLE_HEAD = Box(
"SQUARE_DOUBLE_HEAD",
"""
┌─┬┐
│ ││
╞═╪╡
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘
""",
),
MINIMAL = Box(
"MINIMAL",
"""
╷
│
╶─┼╴
│
╶─┼╴
╶─┼╴
│
╵
""",
),
MINIMAL_HEAVY_HEAD = Box(
"MINIMAL_HEAVY_HEAD",
"""
╷
│
╺━┿╸
│
╶─┼╴
╶─┼╴
│
╵
""",
),
MINIMAL_DOUBLE_HEAD = Box(
"MINIMAL_DOUBLE_HEAD",
"""
╷
│
═╪
│
─┼
─┼
│
╵
""",
),
SIMPLE = Box(
"SIMPLE",
"""
──
──
""",
),
SIMPLE_HEAD = Box(
"SIMPLE_HEAD",
"""
──
""",
),
SIMPLE_HEAVY = Box(
"SIMPLE_HEAVY",
"""
━━
━━
""",
),
HORIZONTALS = Box(
"HORIZONTALS",
"""
──
──
──
──
──
""",
),
ROUNDED = Box(
"ROUNDED",
"""
╭─┬╮
│ ││
├─┼┤
│ ││
├─┼┤
├─┼┤
│ ││
╰─┴╯
""",
),
HEAVY = Box(
"HEAVY",
"""
┏━┳┓
┃ ┃┃
┣━╋┫
┃ ┃┃
┣━╋┫
┣━╋┫
┃ ┃┃
┗━┻┛
""",
),
HEAVY_EDGE = Box(
"HEAVY_EDGE",
"""
┏━┯┓
┃ │┃
┠─┼┨
┃ │┃
┠─┼┨
┠─┼┨
┃ │┃
┗━┷┛
""",
),
HEAVY_HEAD = Box(
"HEAVY_HEAD",
"""
┏━┳┓
┃ ┃┃
┡━╇┩
│ ││
├─┼┤
├─┼┤
│ ││
└─┴┘
""",
),
DOUBLE = Box(
"DOUBLE",
"""
╔═╦╗
║ ║║
╠═╬╣
║ ║║
╠═╬╣
╠═╬╣
║ ║║
╚═╩╝
""",
),
DOUBLE_EDGE = Box(
"DOUBLE_EDGE",
"""
╔═╤╗
║ │║
╟─┼╢
║ │║
╟─┼╢
╟─┼╢
║ │║
╚═╧╝
""",
),
)
end