Advertisement
NanoBob

computercraft misc utils

Jun 21st, 2016
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. function split(sourceString,splittingChar,index)
  2.     results={}
  3.     currentWord=""
  4.     for i=1,string.len(sourceString) do
  5.         letter=string.sub(sourceString,i,i)
  6.         if letter==splittingChar then
  7.             results[#results+1]=currentWord
  8.             currentWord=""
  9.         else
  10.             currentWord=currentWord..letter
  11.         end
  12.     end
  13.     results[#results+1]=currentWord
  14.     return results[index]
  15. end
  16.  
  17. function getSplits(sourceString,splittingChar)
  18.     results={}
  19.     currentWord=""
  20.     for i=1,string.len(sourceString) do
  21.         letter=string.sub(sourceString,i,i)
  22.         if letter==splittingChar then
  23.             results[#results+1]=currentWord
  24.             currentWord=""
  25.         else
  26.             currentWord=currentWord..letter
  27.         end
  28.     end
  29.     results[#results+1]=currentWord
  30.     return results
  31. end
  32.  
  33.  
  34. if fs.exists("event")==false then shell.run("pastebin get UKPy4iiE event") end
  35. if os.loadAPI("event")==false then error("Failed to load event API") end
  36.  
  37. local timers = {}
  38.  
  39. function setTimer(func,timeOut,count,...)
  40.     local id = os.startTimer(timeOut)
  41.     timers[id] = {func = func, args = { ... }, timeOut = timeOut, count = count, executed = 0, id = id }
  42.     return id
  43. end
  44.  
  45. function handleTimers(timer)
  46.     if timers[timer] == nil then return end
  47.     local tableTimer = timers[timer]
  48.     tableTimer.func(table.unpack(table.args or {}))
  49.     tableTimer.executed = tableTimer.executed + 1
  50.     if tableTimer.executed < tableTimer.count or tableTimer.count == 0 then
  51.         timers[os.startTimer(tableTimer.timeOut)] = tableTimer
  52.     end
  53.     timers[timer] = nil
  54. end
  55. event.addHandler("timer",handleTimers)
  56.  
  57.  
  58. function killTimer(id)
  59.     for i,timer in pairs(timers) do
  60.         if timer.id == id then
  61.             timers[i] = nil
  62.         end
  63.     end
  64. end
  65.  
  66. local isReading = false
  67. local readCache = ""
  68. local readPos = { x = 0, y = y }
  69.  
  70. function read(length)
  71.     isReading = true
  72.     readCache = ""
  73.     event.addHandler("key",handleReadKey)
  74.     event.addHandler("char",handleReadChar)
  75.     event.addHandler("paste",handleReadPaste)
  76.     readPos.x, readPos.y = term.getCursorPos()
  77.     readPos.length = length or term.getSize()
  78.     term.setCursorBlink(true)
  79.     writeReadString()
  80.     while isReading do
  81.         event.handleCCEvents()
  82.     end
  83.     cancelRead()
  84.     return readCache
  85. end
  86.  
  87. function handleReadKey(key,held)
  88.     if key == keys.backspace then
  89.         readCache = string.sub(readCache,0,string.len(readCache) - 1 )
  90.     elseif key == keys.enter then
  91.         isReading = false
  92.     end
  93.     writeReadString()
  94. end
  95.  
  96. function handleReadChar(typedLetter)
  97.     readCache = readCache .. typedLetter
  98.     writeReadString()
  99. end
  100.  
  101. function handleReadPaste(pastedString)
  102.     readCache = readCache .. pastedString
  103.     writeReadString()
  104. end
  105.  
  106. function getReadString()
  107.     return readCache
  108. end
  109.  
  110. function writeReadString()
  111.     event.trigger("onReadUpdate",readCache)
  112.     term.setCursorPos(readPos.x,readPos.y)
  113.     term.setBackgroundColor(colors.white)
  114.     term.setTextColor(colors.black)
  115.     local readCache = readCache
  116.     if string.len(readCache) > readPos.length then
  117.         readCache = string.sub(readCache,string.len(readCache) - readPos.length +1, string.len(readCache) +1 )
  118.     end
  119.     term.write(readCache)
  120.     if string.len(readCache) -  1 < readPos.length then
  121.         local x,y = term.getCursorPos()
  122.         for i=string.len(readCache), readPos.length do
  123.             term.write(" ")
  124.         end
  125.         term.setCursorPos(x,y)
  126.     end
  127. end
  128.  
  129. function cancelRead()
  130.     isReading = false
  131.     term.setCursorBlink(false)
  132.     event.removeHandler("key",handleReadKey)
  133.     event.removeHandler("char",handleReadChar)
  134.     event.removeHandler("paste",handleReadPaste)
  135.     return readCache
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement