-
Notifications
You must be signed in to change notification settings - Fork 2
/
join.mojo
320 lines (265 loc) · 10.1 KB
/
join.mojo
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
import math
from external.weave.ansi.ansi import printable_rune_width
from external.gojo.strings import StringBuilder
from .position import Position, top, bottom, left, right, center
from .extensions import repeat
# join_horizontal is a utility function for horizontally joining two
# potentially multi-lined strings along a vertical axis. The first argument is
# the position, with 0 being all the way at the top and 1 being all the way
# at the bottom.
#
# If you just want to align to the left, right or center you may as well just
# use the helper constants Top, Center, and Bottom.
#
# Example:
#
# blockB := "...\n...\n..."
# blockA := "...\n...\n...\n...\n..."
#
# # Join 20% from the top
# str := lipgloss.join_horizontal(0.2, blockA, blockB)
#
# # Join on the top edge
# str := lipgloss.join_horizontal(lipgloss.Top, blockA, blockB)
fn join_horizontal(pos: Position, *strs: String) raises -> String:
if len(strs) == 0:
return ""
if len(strs) == 1:
return strs[0]
# Groups of strings broken into multiple lines
var blocks = List[List[String]](capacity=len(strs))
# Max line widths for the above text blocks
var max_widths = List[Int](capacity=len(strs))
var max_height: Int = 0
# Break text blocks into lines and get max widths for each text block
for i in range(len(strs)):
var s = strs[i]
var lines = s.split("\n")
var widest: Int = 0
for i in range(len(lines)):
var rune_count = printable_rune_width(lines[i])
if rune_count > widest:
widest = rune_count
blocks.append(lines)
max_widths.append(widest)
if len(lines) > max_height:
max_height = len(lines)
# Add extra lines to make each side the same height
for i in range(len(blocks)):
if len(blocks[i]) >= max_height:
continue
var extra_lines = List[String]()
extra_lines.resize(max_height - len(blocks[i]), "")
if pos == top:
blocks[i].extend(extra_lines)
elif pos == bottom:
extra_lines.extend(blocks[i])
blocks[i] = extra_lines
else:
var n = len(extra_lines)
var split = int(n * pos)
var top_point = n - split
var bottom_point = n - top_point
var top_lines = extra_lines[int(top_point) : len(extra_lines)]
var bottom_lines = extra_lines[int(bottom_point) : len(extra_lines)]
top_lines.extend(blocks[i])
blocks[i] = top_lines
blocks[i].extend(bottom_lines)
# Merge lines
var builder = StringBuilder()
# remember, all blocks have the same number of members now
for i in range(len(blocks[0])):
for j in range(len(blocks)):
var block = blocks[j]
_ = builder.write_string(block[i])
# Also make lines the same length
var spaces = repeat(" ", max_widths[j] - printable_rune_width(block[i]))
_ = builder.write_string(spaces)
if i < len(blocks[0]) - 1:
_ = builder.write_string("\n")
return str(builder)
# join_horizontal is a utility function for horizontally joining two
# potentially multi-lined strings along a vertical axis. The first argument is
# the position, with 0 being all the way at the top and 1 being all the way
# at the bottom.
#
# If you just want to align to the left, right or center you may as well just
# use the helper constants Top, Center, and Bottom.
#
# Example:
#
# blockB := "...\n...\n..."
# blockA := "...\n...\n...\n...\n..."
#
# # Join 20% from the top
# str := lipgloss.join_horizontal(0.2, blockA, blockB)
#
# # Join on the top edge
# str := lipgloss.join_horizontal(lipgloss.Top, blockA, blockB)
fn join_horizontal(pos: Position, strs: List[String]) raises -> String:
if len(strs) == 0:
return ""
if len(strs) == 1:
return strs[0]
# Groups of strings broken into multiple lines
var blocks = List[List[String]](capacity=len(strs))
# Max line widths for the above text blocks
var max_widths = List[Int](capacity=len(strs))
var max_height: Int = 0
# Break text blocks into lines and get max widths for each text block
for i in range(len(strs)):
var s = strs[i]
var lines = s.split("\n")
var widest: Int = 0
for i in range(len(lines)):
var rune_count = printable_rune_width(lines[i])
if rune_count > widest:
widest = rune_count
blocks.append(lines)
max_widths.append(widest)
if len(lines) > max_height:
max_height = len(lines)
# Add extra lines to make each side the same height
for i in range(len(blocks)):
if len(blocks[i]) >= max_height:
continue
var extra_lines = List[String]()
extra_lines.resize(max_height - len(blocks[i]), "")
if pos == top:
blocks[i].extend(extra_lines)
elif pos == bottom:
extra_lines.extend(blocks[i])
blocks[i] = extra_lines
else:
var n = len(extra_lines)
var split = int(n * pos)
var top_point = n - split
var bottom_point = n - top_point
var top_lines = extra_lines[int(top_point) : len(extra_lines)]
var bottom_lines = extra_lines[int(bottom_point) : len(extra_lines)]
top_lines.extend(blocks[i])
blocks[i] = top_lines
blocks[i].extend(bottom_lines)
# Merge lines
var builder = StringBuilder()
# remember, all blocks have the same number of members now
for i in range(len(blocks[0])):
for j in range(len(blocks)):
var block = blocks[j]
_ = builder.write_string(block[i])
# Also make lines the same length
var spaces = repeat("", max_widths[j] - printable_rune_width(block[i]))
_ = builder.write_string(spaces)
if i < len(blocks[0]) - 1:
_ = builder.write_string("\n")
return str(builder)
# join_vertical is a utility function for vertically joining two potentially
# multi-lined strings along a horizontal axis. The first argument is the
# position, with 0 being all the way to the left and 1 being all the way to
# the right.
#
# If you just want to align to the left, right or center you may as well just
# use the helper constants Left, Center, and Right.
#
# Example:
#
# blockB := "...\n...\n..."
# blockA := "...\n...\n...\n...\n..."
#
# # Join 20% from the top
# str := lipgloss.join_vertical(0.2, blockA, blockB)
#
# # Join on the right edge
# str := lipgloss.join_vertical(lipgloss.Right, blockA, blockB)
fn join_vertical(pos: Position, *strs: String) raises -> String:
if len(strs) == 0:
return ""
if len(strs) == 1:
return strs[0]
# Groups of strings broken into multiple lines
var blocks = List[List[String]](capacity=len(strs))
# Max line widths for the above text blocks
var max_width: Int = 0
for i in range(len(strs)):
var s = strs[i]
var lines = s.split("\n")
var widest: Int = 0
for i in range(len(lines)):
var rune_count = printable_rune_width(lines[i])
if rune_count > widest:
widest = rune_count
blocks.append(lines)
if widest > max_width:
max_width = widest
var builder = StringBuilder()
var w: Int = 0
for i in range(len(blocks)):
var block = blocks[i]
for j in range(len(block)):
var line = block[j]
w = max_width - printable_rune_width(line)
if pos == left:
_ = builder.write_string(line)
_ = builder.write_string(repeat(" ", w))
elif pos == right:
_ = builder.write_string(repeat(" ", w))
_ = builder.write_string(line)
else:
if w < 1:
_ = builder.write_string(line)
else:
var split = int(w * pos)
var right = w - split
var left = w - right
_ = builder.write_string(repeat(" ", left))
_ = builder.write_string(line)
_ = builder.write_string(repeat(" ", right))
if not (i == len(blocks) - 1 and j == len(block) - 1):
_ = builder.write_string("\n")
return str(builder)
fn join_vertical(pos: Position, strs: List[String]) raises -> String:
if len(strs) == 0:
return ""
if len(strs) == 1:
return strs[0]
# Groups of strings broken into multiple lines
var blocks = List[List[String]](capacity=len(strs))
# Max line widths for the above text blocks
var max_width: Int = 0
for i in range(len(strs)):
var s = strs[i]
var lines = s.split("\n")
var widest: Int = 0
for i in range(len(lines)):
var rune_count = printable_rune_width(lines[i])
if rune_count > widest:
widest = rune_count
blocks.append(lines)
if widest > max_width:
max_width = widest
var builder = StringBuilder()
var w: Int = 0
for i in range(len(blocks)):
var block = blocks[i]
for j in range(len(block)):
var line = block[j]
w = max_width - printable_rune_width(line)
if pos == left:
_ = builder.write_string(line)
_ = builder.write_string(repeat(" ", w))
elif pos == right:
_ = builder.write_string(repeat(" ", w))
_ = builder.write_string(line)
else:
if w < 1:
_ = builder.write_string(line)
else:
var split = int(w * pos)
var right = w - split
var left = w - right
_ = builder.write_string(repeat(" ", left))
_ = builder.write_string(line)
_ = builder.write_string(repeat(" ", right))
if not (i == len(blocks) - 1 and j == len(block) - 1):
_ = builder.write_string("\n")
return str(builder)