thislooksfun

text

Nov 24th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.88 KB | None | 0 0
  1. --[[tlfOS Text API]]--
  2.  
  3. function writeCentered(str, h, startPos, endPos)
  4.   if h == nil then
  5.     _, h = term.getCursorPos()
  6.   end
  7.   if startPos == nil then
  8.     local w = term.getSize()
  9.     term.setCursorPos((w - #str)/2,h)
  10.   else
  11.     w = endPos - startPos
  12.     term.setCursorPos((w - #str)/2 + startPos,h)
  13.   end
  14.  
  15.   write(str)
  16. end
  17.  
  18. function printCentered(str, h, startPos, endPos)
  19.   writeCentered(str, h, startPos, endPos)
  20.   print()
  21. end
  22.  
  23. function getLongestString(t)
  24.   local length = 0
  25.  
  26.   for i = 1, #t do
  27.     if #t[i] > length then
  28.       length = #t[i]
  29.     end
  30.   end
  31.  
  32.   return length
  33. end
  34.  
  35. function readWithBoundaries(xposStart, xposEnd, ypos, charReplace)
  36.   local t = {}
  37.   term.setCursorPos(xposStart, ypos)
  38.   term.setCursorBlink(true)
  39.   local diff = xposEnd - xposStart
  40.  
  41.   if diff < 1 then
  42.     error("The second number must be bigger than the first")
  43.   end
  44.  
  45.   local function refreshLine()
  46.     term.setCursorPos(xposStart, ypos)
  47.     for i = 1, diff do
  48.       write(" ")
  49.     end
  50.     term.setCursorPos(xposStart, ypos)
  51.     if charReplace ~= nil and charReplace ~= "" then
  52.       if #t > diff then
  53.         for i = 1, diff do
  54.           write(charReplace)
  55.         end
  56.       else
  57.         for i = 1, #t do
  58.           write(charReplace)
  59.         end
  60.       end
  61.     else
  62.       if #t > diff then
  63.         for i = 1, diff do
  64.           write(t[#t - diff + i])
  65.         end
  66.       else
  67.         for i = 1, #t do
  68.           write(t[i])
  69.         end
  70.       end
  71.     end
  72.   end
  73.  
  74.   while true do
  75.     local p1, p2 = os.pullEvent()
  76.    
  77.     if p1 == "char" then
  78.       table.insert(t, p2)
  79.     elseif p1 == "key" and p2 == 28 then
  80.       str = ""
  81.       for i = 1, #t do
  82.         str = str..t[i]
  83.       end
  84.       term.setCursorBlink(false)
  85.       return str
  86.     elseif p1 == "key" and p2 == 14 then
  87.       table.remove(t, #t)
  88.     end
  89.     refreshLine()
  90.   end
  91. end
Advertisement
Add Comment
Please, Sign In to add comment