This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
-- desc: Life of a programmer in the Vector
|
-- desc: Life of a programmer in the Vector
|
||||||
-- site: https://github.com/rastasi/mranderson
|
-- site: https://github.com/rastasi/mranderson
|
||||||
-- license: MIT License
|
-- license: MIT License
|
||||||
-- version: 0.8
|
-- version: 0.9
|
||||||
-- script: lua
|
-- script: lua
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
@@ -549,45 +549,36 @@ function UI.update_menu(items, selected_item)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function UI.word_wrap(text, max_chars_per_line)
|
function UI.word_wrap(text, max_chars_per_line)
|
||||||
local result_lines = {}
|
if text == nil then return {""} end
|
||||||
local segments = {}
|
local lines = {}
|
||||||
|
|
||||||
-- Split the input text by explicit newline characters first
|
for input_line in (text .. "\n"):gmatch("(.-)\n") do
|
||||||
for segment in text:gmatch("([^\\n]*)\\n?") do
|
|
||||||
table.insert(segments, segment)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Process each segment for word wrapping
|
|
||||||
for _, segment_text in ipairs(segments) do
|
|
||||||
local current_line = ""
|
local current_line = ""
|
||||||
local words = {}
|
local words_in_line = 0
|
||||||
|
for word in input_line:gmatch("%S+") do
|
||||||
-- Split segment into words
|
words_in_line = words_in_line + 1
|
||||||
for word in segment_text:gmatch("%S+") do
|
|
||||||
table.insert(words, word)
|
|
||||||
end
|
|
||||||
|
|
||||||
local i = 1
|
|
||||||
while i <= #words do
|
|
||||||
local word = words[i]
|
|
||||||
if #current_line == 0 then
|
if #current_line == 0 then
|
||||||
current_line = word
|
current_line = word
|
||||||
elseif #current_line + 1 + #word <= max_chars_per_line then
|
elseif #current_line + #word + 1 <= max_chars_per_line then
|
||||||
current_line = current_line .. " " .. word
|
current_line = current_line .. " " .. word
|
||||||
else
|
else
|
||||||
table.insert(result_lines, current_line)
|
table.insert(lines, current_line)
|
||||||
current_line = word
|
current_line = word
|
||||||
end
|
end
|
||||||
i = i + 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add the last line of the segment if not empty
|
if words_in_line > 0 then
|
||||||
if #current_line > 0 then
|
table.insert(lines, current_line)
|
||||||
table.insert(result_lines, current_line)
|
else
|
||||||
|
table.insert(lines, "")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return result_lines
|
if #lines == 0 then
|
||||||
|
return {""}
|
||||||
|
end
|
||||||
|
|
||||||
|
return lines
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user