aboutsummaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
Diffstat (limited to 'builtin')
-rw-r--r--builtin/common/misc_helpers.lua66
-rw-r--r--builtin/mainmenu/common.lua2
-rw-r--r--builtin/mainmenu/tab_mods.lua2
3 files changed, 18 insertions, 52 deletions
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua
index 5fc589b72..87561726e 100644
--- a/builtin/common/misc_helpers.lua
+++ b/builtin/common/misc_helpers.lua
@@ -308,59 +308,25 @@ function core.formspec_escape(text)
end
-function core.wrap_text(text, charlimit)
- local retval = {}
-
- local current_idx = 1
-
- local start,stop = string_find(text, " ", current_idx)
- local nl_start,nl_stop = string_find(text, "\n", current_idx)
- local gotnewline = false
- if nl_start ~= nil and (start == nil or nl_start < start) then
- start = nl_start
- stop = nl_stop
- gotnewline = true
- end
- local last_line = ""
- while start ~= nil do
- if string.len(last_line) + (stop-start) > charlimit then
- retval[#retval + 1] = last_line
- last_line = ""
+function core.wrap_text(text, max_length, as_table)
+ local result = {}
+ local line = {}
+ if #text <= max_length then
+ return as_table and {text} or text
+ end
+
+ for word in text:gmatch('%S+') do
+ local cur_length = #table.concat(line, ' ')
+ if cur_length > 0 and cur_length + #word + 1 >= max_length then
+ -- word wouldn't fit on current line, move to next line
+ table.insert(result, table.concat(line, ' '))
+ line = {}
end
-
- if last_line ~= "" then
- last_line = last_line .. " "
- end
-
- last_line = last_line .. string_sub(text, current_idx, stop - 1)
-
- if gotnewline then
- retval[#retval + 1] = last_line
- last_line = ""
- gotnewline = false
- end
- current_idx = stop+1
-
- start,stop = string_find(text, " ", current_idx)
- nl_start,nl_stop = string_find(text, "\n", current_idx)
-
- if nl_start ~= nil and (start == nil or nl_start < start) then
- start = nl_start
- stop = nl_stop
- gotnewline = true
- end
- end
-
- --add last part of text
- if string.len(last_line) + (string.len(text) - current_idx) > charlimit then
- retval[#retval + 1] = last_line
- retval[#retval + 1] = string_sub(text, current_idx)
- else
- last_line = last_line .. " " .. string_sub(text, current_idx)
- retval[#retval + 1] = last_line
+ table.insert(line, word)
end
- return retval
+ table.insert(result, table.concat(line, ' '))
+ return as_table and result or table.concat(result, '\n')
end
--------------------------------------------------------------------------------
diff --git a/builtin/mainmenu/common.lua b/builtin/mainmenu/common.lua
index fa7ae583b..7eb941775 100644
--- a/builtin/mainmenu/common.lua
+++ b/builtin/mainmenu/common.lua
@@ -250,7 +250,7 @@ end
--------------------------------------------------------------------------------
function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
- local textlines = core.wrap_text(text, textlen)
+ local textlines = core.wrap_text(text, textlen, true)
local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
"," .. height .. ";" .. tl_name .. ";"
diff --git a/builtin/mainmenu/tab_mods.lua b/builtin/mainmenu/tab_mods.lua
index d829c2cce..e74fb442c 100644
--- a/builtin/mainmenu/tab_mods.lua
+++ b/builtin/mainmenu/tab_mods.lua
@@ -66,7 +66,7 @@ local function get_formspec(tabview, name, tabdata)
if error == nil then
local descriptiontext = descriptionfile:read("*all")
- descriptionlines = core.wrap_text(descriptiontext, 42)
+ descriptionlines = core.wrap_text(descriptiontext, 42, true)
descriptionfile:close()
else
descriptionlines = {}