Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if #text > character_limit then
- text = string.sub(text, 1, character_limit - 3) .. "..."
- end
- local text_length = #text
- local line_length = 0
- local i = 0
- while i <= text_length do
- i = i + 1
- local character = string.sub(text, i, i)
- if character == "\t" then
- local tabulation_size = 4 - line_length % 4
- line_length = line_length + tabulation_size
- if line_length >= line_length_limit then
- tabulation_size = line_length - line_length_limit
- line_length = 0
- text_length = text_length + tabulation_size
- text = string.sub(text, 1, i - 1) .. string.rep(" ", tabulation_size) .. "\n" .. string.sub(text, i + 1)
- i = i + tabulation_size + 1
- else
- text_length = text_length + tabulation_size - 1
- text = string.sub(text, 1, i - 1) .. string.rep(" ", tabulation_size) .. string.sub(text, i + 1)
- i = i + tabulation_size - 1
- end
- elseif character == "\n" then
- line_length = 0
- else
- line_length = line_length + 1
- if line_length >= line_length_limit then
- local k = i - line_length + 1
- local success = false
- for j = i, k, -1 do
- if string.match(string.sub(text, j, j), "[ \t]") then
- text = string.sub(text, 1, j - 1) .. "\n" .. string.sub(text, j + 1)
- text_length = text_length + 1
- success = true
- break
- end
- end
- if not success then
- text = string.sub(text, 1, i) .. "\n" .. string.sub(text, i + 1)
- text_length = text_length + 1
- end
- i = i + 1
- line_length = 0
- end
- end
- end
- if #text > character_limit then
- text = string.sub(text, 1, character_limit - 3) .. "..."
- end
- return text
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement