theTANCO

LibAppend.lua

Jun 27th, 2022 (edited)
802
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.99 KB | None
  1. -- This is an API to add much needed functions to the global libraries in ComputerCraft Lua. This will only work in ComputerCraft. Add it to your program by including 'require("LibAppend")' at the top of your program.
  2.  
  3. string.cap = function(s) -- Correctly capitalize the first letter in each sentence in a string.
  4.     for a = 1, s:len() do
  5.         if a == 1 or s:sub(a-2, a-1) == ". " or s:sub(a-2, a-1) == "? " or s:sub(a-2, a-1) == "! " then
  6.             s = s:sub(1, a-1)..s:sub(a, a):upper()..s:sub(a+1, s:len())
  7.         end
  8.     end
  9.     return s
  10. end
  11.  
  12. string.capAll = function(s) -- Capitalizes all words.
  13.     for a = 1, s:len() do
  14.         if a == 1 or s:sub(a-1, a-1) == " " then
  15.             s = s:sub(1, a-1)..s:sub(a, a):upper()..s:sub(a+1, s:len())
  16.         end
  17.     end
  18.     return s
  19. end
  20.  
  21. textutils.slowBlit = function(s, t, b, d) -- A combination of texutils.slowWrite and term.blit.
  22.     d = d or 20
  23.     if type(s) ~= "string" then
  24.         error("bad argument #1 (expected string, got "..type(s)..")", 2)
  25.     end
  26.     if type(t) ~= "string" then
  27.         error("bad argument #2 (expected string, got "..type(t)..")", 2)
  28.     end
  29.     if type(b) ~= "string" then
  30.         error("bad argument #3 (expected string, got "..type(b)..")", 2)
  31.     end
  32.     if string.len(s) ~= string.len(t) or string.len(s) ~= string.len(b) or string.len(t) ~= string.len(b) then
  33.         error("Arguments #1-3 must be the same length ("..string.len(s)..", "..string.len(t)..", "..string.len(b)..")", 2)
  34.     end
  35.     if not tonumber(d) then
  36.         error("bad argument #4 (expected number, got "..type(d)..")", 2)
  37.     end
  38.     if d <= 0 then
  39.         error("Text speed must be greater than 0", 2)
  40.     end
  41.  
  42.     local lastSpace = 0
  43.     local lastNL = 0
  44.     local xPos, yPos = term.getCursorPos()
  45.     local xSize, ySize = term.getSize()
  46.     for i = 1, s:len() do
  47.         if s:sub(i, i) == " " then
  48.             lastSpace = i
  49.         end
  50.         if s:sub(i, i) == "\n" then
  51.             lastNL = i
  52.             lastSpace = i
  53.         end
  54.         if xPos+i-lastNL-1 > xSize then
  55.             s = s:sub(1, lastSpace-1).."\n"..s:sub(lastSpace+1, s:len())
  56.             lastNL = lastSpace
  57.             xPos = 1
  58.         end
  59.     end
  60.  
  61.     local currentTextColor = term.getTextColor()
  62.     local currentBackgroundColor = term.getBackgroundColor()
  63.  
  64.     for i = 1, s:len() do
  65.         sleep(1/d)
  66.         term.setTextColor(2^tonumber(t:sub(i, i), 16))
  67.         term.setBackgroundColor(2^tonumber(b:sub(i, i), 16))
  68.         write(s:sub(i, i))
  69.     end
  70.     term.setTextColor(currentTextColor)
  71.     term.setBackgroundColor(currentBackgroundColor)
  72. end
  73.  
  74. math.round = function(n)
  75.     if n%1 < 0.5 then return math.floor(n)
  76.     else return math.ceil(n) end
  77. end
  78.  
  79. Vector2 = function(x, y) -- Creates a 2D vector.
  80.     return {x = x or 0, y = y or 0}
  81. end
  82.  
  83. bugCheck = function(x, y, t, s)
  84.     term.setCursorPos(x, y)
  85.     term.setBackgroundColor(colors.black)
  86.     term.setTextColor(colors.red)
  87.     term.write(s)
  88.     if t > 0 then sleep(t) end
  89. end
RAW Paste Data Copied