string - Lua Carrying over a value through line wrap code -
previously, received in following link:
lua line wrapping excluding characters
short description of above looking way able run line wrap function while ignoring character count of characters.
now i've come across issue. want able carry last colour code on new line. example:
if line @rwere on 79 characters, want @breturn last known colour code @yon line break.
running function have in mind result in:
if line @rwere on 79 characters, want @breturn last known @bcolour code @yon line break.
instead of
if line @rwere on 79 characters, want @breturn last known colour code @yon line break.
i wish because in many cases, mud default @w colour code, make colourizing text rather difficult.
i've figured easiest way reverse match, i've written reverse_text function:
function reverse_text(str) local text = {} word in str:gmatch("[^%s]+") table.insert(text, 1, word) end return table.concat(text, " ") end
and turns:
@gthis @yis @ba @mtest.
to
@mtest. @ba @yis @gthis
the issue i'm running creating string.match fact colour codes can in 1 of 2 formats:
@%a or @x%d%d%d
additionally, don't want return colour code doesn't colour, indicated as:
@@%a or @@x%d%d%d
what's best way accomplish end goal without compromising requirements?
function wrap(str, limit, indent, indent1) indent = indent or "" indent1 = indent1 or indent limit = limit or 79 local here = 1-#indent1 local last_color = '' return indent1..str:gsub("(%s+)()(%s+)()", function(sp, st, word, fi) local delta = 0 local color_before_current_word = last_color word:gsub('()@([@%a])', function(pos, c) if c == '@' delta = delta + 1 elseif c == 'x' delta = delta + 5 last_color = word:sub(pos, pos+4) else delta = delta + 2 last_color = word:sub(pos, pos+1) end end) here = here + delta if fi-here > limit here = st - #indent + delta return "\n"..indent..color_before_current_word..word end end) end
Comments
Post a Comment