mirror of
https://github.com/minetest/minetest.git
synced 2024-12-02 04:23:45 +01:00
Fix core.wrap_text and make its behaviour consistent with the docs
Code based on initial implementation by @dsohler.
This commit is contained in:
parent
e9087d1be7
commit
5b2461c713
@ -308,59 +308,25 @@ function core.formspec_escape(text)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function core.wrap_text(text, charlimit)
|
function core.wrap_text(text, max_length, as_table)
|
||||||
local retval = {}
|
local result = {}
|
||||||
|
local line = {}
|
||||||
local current_idx = 1
|
if #text <= max_length then
|
||||||
|
return as_table and {text} or text
|
||||||
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 = ""
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if last_line ~= "" then
|
for word in text:gmatch('%S+') do
|
||||||
last_line = last_line .. " "
|
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
|
||||||
|
table.insert(line, word)
|
||||||
end
|
end
|
||||||
|
|
||||||
last_line = last_line .. string_sub(text, current_idx, stop - 1)
|
table.insert(result, table.concat(line, ' '))
|
||||||
|
return as_table and result or table.concat(result, '\n')
|
||||||
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
|
|
||||||
end
|
|
||||||
|
|
||||||
return retval
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
@ -250,7 +250,7 @@ end
|
|||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency)
|
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 ..
|
local retval = "textlist[" .. xpos .. "," .. ypos .. ";" .. width ..
|
||||||
"," .. height .. ";" .. tl_name .. ";"
|
"," .. height .. ";" .. tl_name .. ";"
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ local function get_formspec(tabview, name, tabdata)
|
|||||||
if error == nil then
|
if error == nil then
|
||||||
local descriptiontext = descriptionfile:read("*all")
|
local descriptiontext = descriptionfile:read("*all")
|
||||||
|
|
||||||
descriptionlines = core.wrap_text(descriptiontext, 42)
|
descriptionlines = core.wrap_text(descriptiontext, 42, true)
|
||||||
descriptionfile:close()
|
descriptionfile:close()
|
||||||
else
|
else
|
||||||
descriptionlines = {}
|
descriptionlines = {}
|
||||||
|
@ -2128,9 +2128,11 @@ Helper functions
|
|||||||
* e.g. `string:split("a,b", ",") == {"a","b"}`
|
* e.g. `string:split("a,b", ",") == {"a","b"}`
|
||||||
* `string:trim()`
|
* `string:trim()`
|
||||||
* e.g. `string.trim("\n \t\tfoo bar\t ") == "foo bar"`
|
* e.g. `string.trim("\n \t\tfoo bar\t ") == "foo bar"`
|
||||||
* `minetest.wrap_text(str, limit)`: returns a string
|
* `minetest.wrap_text(str, limit, [as_table])`: returns a string or table
|
||||||
* Adds newlines to the string to keep it within the specified character limit
|
* Adds newlines to the string to keep it within the specified character limit
|
||||||
|
Note that returned lines may be longer than the limit since it only splits at word borders.
|
||||||
* limit: Maximal amount of characters in one line
|
* limit: Maximal amount of characters in one line
|
||||||
|
* as_table: optional, if true return table of lines instead of string
|
||||||
* `minetest.pos_to_string({x=X,y=Y,z=Z}, decimal_places))`: returns string `"(X,Y,Z)"`
|
* `minetest.pos_to_string({x=X,y=Y,z=Z}, decimal_places))`: returns string `"(X,Y,Z)"`
|
||||||
* Convert position to a printable string
|
* Convert position to a printable string
|
||||||
Optional: 'decimal_places' will round the x, y and z of the pos to the given decimal place.
|
Optional: 'decimal_places' will round the x, y and z of the pos to the given decimal place.
|
||||||
|
Loading…
Reference in New Issue
Block a user