Skip to content

Commit

Permalink
Colored text now behaves properly with text interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajusa committed Jan 3, 2021
1 parent ce5d2f3 commit 1e5bb53
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/text.moon
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ word_wrap = (text, max_width) ->
line = ""
for i=2, #colored, 2 -- Skip over the colors themselves
words = split(colored[i], " ")
line = line..words[1]
last_color = colored[i-1]
for j=2, #words
tmp = line.." "..words[j]
if font\getWidth(tmp) > max_width
table.insert(list[l], last_color)
table.insert(list[l], line)
l += 1
table.insert(list, {})
line = words[j]
else line = tmp
if #words > 0
line = line..words[1]
last_color = colored[i-1]
for j=2, #words
tmp = line.." "..words[j]
if font\getWidth(tmp) > max_width
table.insert(list[l], last_color)
table.insert(list[l], line)
l += 1
table.insert(list, {})
line = words[j]
else line = tmp
if #words > 1 then line = line.." "
table.insert(list[l], last_color)
table.insert(list[l], line)
line = ""
Expand Down
7 changes: 5 additions & 2 deletions src/text_color.moon
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ color_map = {
"x1b[37;1m": rgb(236, 239, 244)
}
colorify = (str, i=1, last_color=reset, result={}) ->
s, e = str\find("x1b%[%d*;*%dm", i)
s, e = str\find("\\*x1b%[%d*;*%dm", i)
if s == nil
table.insert(result, last_color)
table.insert(result, str\sub(i, -1))
return result
color = color_map[str\sub(s, e)]
offset = 0
if str\sub(s, s) == "\\" --remove the backslash
offset = 1
color = color_map[str\sub(s + offset, e)]
if i != s
table.insert(result, last_color)
table.insert(result, str\sub(i, s - 1))
Expand Down

0 comments on commit 1e5bb53

Please sign in to comment.