9551

Untitled

Jun 23rd, 2021 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. ----strings API-------
  2. --- Various utilities for working with strings and text.
  3. --
  4. -- @module cc.strings
  5. -- @see textutils For additional string related utilities.
  6.  
  7. local expect = require "cc.expect".expect
  8.  
  9. --[[- Wraps a block of text, so that each line fits within the given width.
  10. This may be useful if you want to wrap text before displaying it to a
  11. @{monitor} or @{printer} without using @{_G.print|print}.
  12. @tparam string text The string to wrap.
  13. @tparam[opt] number width The width to constrain to, defaults to the width of
  14. the terminal.
  15. @treturn { string... } The wrapped input string as a list of lines.
  16. @usage Wrap a string and write it to the terminal.
  17.     term.clear()
  18.     local lines = require "cc.strings".wrap("This is a long piece of text", 10)
  19.     for i = 1, #lines do
  20.       term.setCursorPos(1, i)
  21.       term.write(lines[i])
  22.     end
  23. ]]
  24. local function wrap(text, width)
  25.     expect(1, text, "string")
  26.     expect(2, width, "number", "nil")
  27.     width = width or term.getSize()
  28.  
  29.  
  30.     local lines, lines_n, current_line = {}, 0, ""
  31.     local function push_line()
  32.         lines_n = lines_n + 1
  33.         lines[lines_n] = current_line
  34.         current_line = ""
  35.     end
  36.  
  37.     local pos, length = 1, #text
  38.     local sub, match = string.sub, string.match
  39.     while pos <= length do
  40.         local head = sub(text, pos, pos)
  41.         if head == " " or head == "\t" then
  42.             local whitespace = match(text, "^[ \t]+", pos)
  43.             current_line = current_line .. whitespace
  44.             pos = pos + #whitespace
  45.         elseif head == "\n" then
  46.             push_line()
  47.             pos = pos + 1
  48.         else
  49.             local word = match(text, "^[^ \t\n]+", pos)
  50.             pos = pos + #word
  51.             if #word > width then
  52.                 -- Print a multiline word
  53.                 while #word > 0 do
  54.                     local space_remaining = width - #current_line - 1
  55.                     if space_remaining <= 0 then
  56.                         push_line()
  57.                         space_remaining = width
  58.                     end
  59.  
  60.                     current_line = current_line .. sub(word, 1, space_remaining)
  61.                     word = sub(word, space_remaining + 1)
  62.                 end
  63.             else
  64.                 -- Print a word normally
  65.                 if width - #current_line < #word then push_line() end
  66.                 current_line = current_line .. word
  67.             end
  68.         end
  69.     end
  70.  
  71.     push_line()
  72.  
  73.     -- Trim whitespace longer than width.
  74.     for k, line in pairs(lines) do
  75.         line = line:sub(1, width)
  76.         lines[k] = line
  77.     end
  78.  
  79.     return lines
  80. end
  81.  
  82. --- Makes the input string a fixed width. This either truncates it, or pads it
  83. -- with spaces.
  84. --
  85. -- @tparam string line The string to normalise.
  86. -- @tparam[opt] number width The width to constrain to, defaults to the width of
  87. -- the terminal.
  88. --
  89. -- @treturn string The string with a specific width.
  90. -- @usage require "cc.strings".ensure_width("a short string", 20)
  91. -- @usage require "cc.strings".ensure_width("a rather long string which is truncated", 20)
  92. local function ensure_width(line, width)
  93.     expect(1, line, "string")
  94.     expect(2, width, "number", "nil")
  95.     width = width or term.getSize()
  96.  
  97.     line = line:sub(1, width)
  98.     if #line < width then
  99.         line = line .. (" "):rep(width - #line)
  100.     end
  101.  
  102.     return line
  103. end
  104.  
  105. return {
  106.     wrap = wrap,
  107.     ensure_width = ensure_width,
  108. }
Add Comment
Please, Sign In to add comment