Advertisement
1x1x1x1IAMbck

anti g/nol

Apr 20th, 2018
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. if #text > character_limit then
  2. text = string.sub(text, 1, character_limit - 3) .. "..."
  3. end
  4. local text_length = #text
  5. local line_length = 0
  6. local i = 0
  7. while i <= text_length do
  8. i = i + 1
  9. local character = string.sub(text, i, i)
  10. if character == "\t" then
  11. local tabulation_size = 4 - line_length % 4
  12. line_length = line_length + tabulation_size
  13. if line_length >= line_length_limit then
  14. tabulation_size = line_length - line_length_limit
  15. line_length = 0
  16. text_length = text_length + tabulation_size
  17. text = string.sub(text, 1, i - 1) .. string.rep(" ", tabulation_size) .. "\n" .. string.sub(text, i + 1)
  18. i = i + tabulation_size + 1
  19. else
  20. text_length = text_length + tabulation_size - 1
  21. text = string.sub(text, 1, i - 1) .. string.rep(" ", tabulation_size) .. string.sub(text, i + 1)
  22. i = i + tabulation_size - 1
  23. end
  24. elseif character == "\n" then
  25. line_length = 0
  26. else
  27. line_length = line_length + 1
  28. if line_length >= line_length_limit then
  29. local k = i - line_length + 1
  30. local success = false
  31. for j = i, k, -1 do
  32. if string.match(string.sub(text, j, j), "[ \t]") then
  33. text = string.sub(text, 1, j - 1) .. "\n" .. string.sub(text, j + 1)
  34. text_length = text_length + 1
  35. success = true
  36. break
  37. end
  38. end
  39. if not success then
  40. text = string.sub(text, 1, i) .. "\n" .. string.sub(text, i + 1)
  41. text_length = text_length + 1
  42. end
  43. i = i + 1
  44. line_length = 0
  45. end
  46. end
  47. end
  48. if #text > character_limit then
  49. text = string.sub(text, 1, character_limit - 3) .. "..."
  50. end
  51. return text
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement