libraryaddict

Untitled

Apr 23rd, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 22.70 KB | None | 0 0
  1. --[[
  2. libraryaddict API
  3. May be some useless stuff in here. Why not tho xD
  4. API's:
  5.  
  6. event() - Pull event basically
  7. eventRaw() -- Pull raw event basically.
  8. send(id, msg) -- Rednet send/broadcast made little easier
  9. layout(msg, style) -- Centers text and does your style down the side. so |    hi!   |
  10. layoutOrder(mess, number, style) -- Centers text and does your style down the side. Also has the text not centered but at one thing
  11. fillLine(style) -- Fills the line with stuff like "======="
  12. center(y, mess) -- Centers text and prints it
  13. slowCenter(y, mess, sec) -- Centers text and prints it slow
  14. writeCenter(y, mess) -- Centers text and writes it
  15. centerPrint(mess) -- Center print with no need to say y, Does it after text
  16. centerWrite(mess) -- Center write with no need to say y, Does it after text
  17. slowCenterPrint(mess, sec)-- Slow Center print with no need to say y, Does it after text
  18. cursor(y) -- Set cursor up/down relative. use -1 or 1
  19. removeline(filename, as many as you like lines.) -- Part of removePerm, This will remove certain lines from files
  20. removelineNo(filename, unlimited line numbers) -- Part of removePerm, This will remove certain lines from files by the line number
  21. insertLine(file, lineno, Insertline) -- insert lines into files
  22. removePerm(Msg) -- removePerm(Msg) - Removes permant stored msgs from perm storage
  23. compareMsgPerm(Msg)-- compareMsgPerm(Msg) - Check if it exists in perm storage
  24. compareMsg(Msg)-- compareMsg(Msg) -- check if it exists in the temp storage
  25. storePerm(Msg) -- storePerm(Msg) - Stores the msg permantly for use any time
  26. store(Msg, Max) -- store(Msg, Max) - Stored in temp storage. Max is the max amount of msgs to store. default is 5
  27. pulse(RPside, RPcount, RPperiod) -- Same as redpulse
  28. printkeycode(times)  -- Prints whatever it hears
  29. space(LinesToSkip) -- Calculates line and goes to the one after
  30. copyOver(from, to) -- Copys over files from a place to a place
  31. moveOver(from, to) -- Move file from A. to B.
  32. Clear() -- Clears screen and sets cursor at 1,1
  33. rsSet() -- Run before rsGet
  34. rsGet() -- Run to return what rs input changed. Best to use after all rs events, currently works only for straight redstone. Not bundled.
  35. checkDisk() -- Checks if disk exist and if it does return the diskside
  36. writeVari(...) -- Writes variables to a file
  37. clearLine() -- Clears line it was on and sets the cursor to 1, linenumber
  38. writeFile(line, filename) -- Receives a string and writes it into a file. Best with sendFile
  39. sendFile(filename, ID) -- Opens a file and sends it as a single string
  40. tagView(msg) -- returns the msg as [RELAY] and so on. Strips [SOMETHING] away then returns it as a single thing. Also returns the msg destripped
  41. tag(msg, tag) -- Add a tag
  42. deTag(msg, tag) -- Remove specific tags
  43. permVari(variname, contents) -- use to store a permanant variable to stay in usage. variable name, variable contents. make sure you use dofile in startup
  44. removePermVari(variname) -- Remove from permanant load
  45. checkTable(table, string) -- Check if string exists in the table
  46. findFileName(table, string) -- Find the table with the string in it. So "hello" will find [hello there] but not [yo hello]
  47. log(event) -- Logs the file. You use dofile to use it
  48. bundle(Side, Color, True/False) -- Sets bundled output
  49. centerBatch(Msg, MSg, msg, msg etc.) -- Do a bunch of text and it will center it
  50. testBundle(side, color) -- Check if its on or off
  51. openRednet() -- Opens rednet wireless. No need to define side.
  52. monitor(side) -- Changes the screen to that side.
  53. size(side, number) -- Changes text size on monitor
  54. ]]
  55.  
  56. function send(id, msg) -- Rednet send/broadcast made little easier
  57.   if msg then
  58.     rednet.send(id, msg)
  59.   else
  60.     rednet.broadcast(msg)
  61.   end
  62. end
  63.  
  64. function layout(mess, style) -- Centers text and does your style down the side. so |    hi!   |
  65.   local style = style
  66.   local mess = mess
  67.   if not style then
  68.     style = "|"
  69.   end
  70.   if not mess then
  71.     mess = ""
  72.   end
  73.   local w,h = term.getSize()
  74.   local middle = (w - mess:len())/2
  75.   local cy,my = term.getCursorPos()
  76.   middle = middle-1
  77.   term.setCursorPos(1, my)
  78.   write(style)
  79.   term.setCursorPos(middle, my)
  80.   write(mess)
  81.   term.setCursorPos(w-1, my)
  82.   print(style)
  83. end
  84.  
  85. function layoutOrder(mess, number, style) -- Centers text and does your style down the side. Also has the text not centered but at one thing
  86.   if not style then
  87.     style = "|"
  88.   end
  89.   if not mess then
  90.     mess = ""
  91.   end
  92.   if not number then
  93.     number = 6
  94.   end
  95.   local w,h = term.getSize()
  96.   local cy,my = term.getCursorPos()
  97.   term.setCursorPos(1, my)
  98.   write(style)
  99.   term.setCursorPos(number, my)
  100.   write(mess)
  101.   term.setCursorPos(w-1, my)
  102.   print(style)
  103. end
  104.  
  105. function fillLine(style) -- Fills the line with stuff like "======="
  106.   local style = style
  107.   if not style then
  108.     style = "|"
  109.   end
  110.   local cy,my = term.getCursorPos()
  111.   local w,h = term.getSize()
  112.   term.setCursorPos(1, my)
  113.   for n=1,w-1 do
  114.     term.write(style)
  115.   end
  116.   print()
  117. end
  118.  
  119. function center(y, mess) -- Centers text and prints it
  120.   local w,h = term.getSize()
  121.   local middle = (w - mess:len())/2 - 1
  122.   term.setCursorPos(middle, y)
  123.   print(mess)
  124. end
  125.  
  126. function slowCenter(y, mess, sec) -- Centers text and prints it slow
  127.   local w,h = term.getSize()
  128.   local middle = (w - mess:len())/2 - 1
  129.   term.setCursorPos(middle, y)
  130.   textutils.slowPrint(mess, sec)
  131. end
  132.  
  133. function writeCenter(y, mess) -- Centers text and writes it
  134.   local w,h = term.getSize()
  135.   local middle = (w - mess:len())/2 - 1
  136.   term.setCursorPos(middle, y)
  137.   write(mess)
  138. end
  139.  
  140. function centerPrint(mess) -- Center print with no need to say y, Does it after text
  141.   local w,h = term.getSize()
  142.   local middle = (w - mess:len())/2 - 1
  143.   local cy,my = term.getCursorPos()
  144.   term.setCursorPos(middle, my)
  145.   print(mess)
  146. end
  147.  
  148. function centerWrite(mess) -- Center write with no need to say y, Does it after text
  149.   local w,h = term.getSize()
  150.   local middle = (w - mess:len())/2 - 1
  151.   local cy,my = term.getCursorPos()
  152.   term.setCursorPos(middle, my)
  153.   write(mess)
  154. end
  155.  
  156. function slowCenterPrint(mess, sec)-- Slow Center print with no need to say y, Does it after text
  157.   local w,h = term.getSize()
  158.   local middle = (w - mess:len())/2 - 1
  159.   local cy,my = term.getCursorPos()
  160.   term.setCursorPos(middle, my)
  161.   textutils.slowPrint(mess, sec)
  162. end
  163.  
  164. function cursor(iy) -- Set cursor up/down relative to one
  165.   local x,y = term.getCursorPos()
  166.   local y = y+iy
  167.   x = 1
  168.   term.setCursorPos(x, y)
  169. end
  170.  
  171.  
  172. function removeline(filename, ...) -- Part of removePerm, This will remove certain lines from files
  173.   local LinesRemove = {...}
  174.   local fp = io.open( filename, "r" )
  175.   local content = {}
  176.   local i = 1
  177.   for line in fp:lines() do
  178.     content[#content+1] = line
  179.     i = i + 1
  180.   end
  181.   fp:close()
  182.   for n=1,#content do
  183.     for e=1,#LinesRemove do
  184.       if content[n] == LinesRemove[e] then
  185.         table.remove(content,n)
  186.       end
  187.     end
  188.   end
  189.   local fp = io.open( filename, "w" )
  190.   for i = 1, #content do
  191.     fp:write( string.format( "%s\n", content[i] ) )
  192.   end
  193.   fp:close()
  194. end
  195.  
  196. function removeLineNo(filename, ...) -- Say the file you want removed. Then the line numbers of the lines you want removed.
  197.   local LinesRemove = {...}
  198.   local fp = io.open( filename, "r" )
  199.   local content = {}
  200.   local i = 1
  201.   for line in fp:lines() do
  202.     content[#content+1] = line
  203.     i = i + 1
  204.   end
  205.   fp:close()
  206.   for n=1,#LinesRemove do
  207.     table.remove(content,LinesRemove[n])
  208.     for e=1,#LinesRemove do
  209.       LinesRemove[e] = LinesRemove[e]-1
  210.     end
  211.   end
  212.   local fp = io.open( filename, "w" )
  213.   for i = 1, #content do
  214.     fp:write( string.format( "%s\n", content[i] ) )
  215.   end
  216.   fp:close()
  217. end
  218.  
  219. function insertLine(file, lineno, Insertline) -- say the file, Then the line number. Then the line to be inserted.
  220.   local lolol = io.open(file, "r")
  221.   local content = {}
  222.   local i = 1
  223.   for line in lolol:lines() do
  224.     if i == lineno then
  225.       content[#content+1] = Insertline
  226.       i = i + 1
  227.     end
  228.     content[#content+1] = line
  229.     i = i + 1
  230.   end
  231.   lolol:close()
  232.   local lolol = io.open( file, "w" )
  233.   for i = 1, #content do
  234.     lolol:write( string.format( "%s\n", content[i] ) )
  235.   end
  236.   lolol:close()
  237. end
  238.  
  239. function removePerm(Msg) -- removePerm(Msg) - Removes from perm storage
  240.   if fs.exists("PermanantRelayStorage") then
  241.     removeline("PermanantRelayStorage", Msg)
  242.   end
  243. end
  244.  
  245. function compareMsgPerm(Msg)-- compareMsgPerm(Msg) - Check if it exists in perm storage
  246.   if fs.exists("PermanantRelayStorage") then
  247.     local liner = false
  248.     local h = fs.open("PermanantRelayStorage", "r")
  249.     repeat
  250.       local line = h:readLine()
  251.       if line == Msg then
  252.         liner = true
  253.       end
  254.     until not line
  255.     h:close()
  256.     if liner == true then
  257.       return true
  258.     else
  259.       return false
  260.     end
  261.   else
  262.   return false
  263.   end
  264. end
  265.  
  266. function compareMsg(Msg)-- compareMsg(Msg) -- check if it exists in the temp storage
  267.   if TempRelayStorage then
  268.     for n=1,#TempRelayStorage do
  269.       if TempRelayStorage[n] == Msg then
  270.         return true
  271.       end
  272.     end
  273.   end
  274. end
  275.  
  276. function storePerm(Msg) -- storePerm(Msg) - Stores the msg permantly for use any time
  277.   local fh = io.open("PermanantRelayStorage", "a")
  278.   fh:write(Msg.."\n")
  279.   fh:close()
  280. end
  281.  
  282. function store(Msg, Max) -- store(Msg, Max) - Stored in temp storage. Max is the max amount of msgs to store. default is 5
  283.   local Max = Max
  284.   if TempRelayStorage == nil then
  285.     TempRelayStorage = {}
  286.   end
  287.   if Max == nil then
  288.     Max = 5
  289.   end
  290.   local tableCurrentNo = 1
  291.   repeat
  292.     if TempRelayStorage[tableCurrentNo] == nil then
  293.       if tableCurrentNo == Max then
  294.         TempRelayStorage[tableCurrentNo] = Msg
  295.         TempRelayStorage[1] = nil
  296.         tableCurrentNo = 1
  297.       else
  298.         TempRelayStorage[tableCurrentNo] = Msg
  299.         TempRelayStorage[tableCurrentNo+1] = nil
  300.         tableCurrentNo = Max+1
  301.       end
  302.     end
  303.     tableCurrentNo = tableCurrentNo+1
  304.   until tableCurrentNo > Max
  305. end
  306.  
  307. function pulse(RPside, RPcount, RPperiod) -- Same as redpulse
  308.   local RPcount = RPcount
  309.   local RPperiod = RPperiod
  310.   if RPcount == nil then
  311.   RPcount = 1
  312.  end
  313.   if RPperiod == nil then
  314.   RPperiod = 0.5
  315.  end
  316.   for n=1,RPcount do
  317.     redstone.setOutput( RPside, true )
  318.   sleep( RPperiod / 2 )
  319.    redstone.setOutput( RPside, false )
  320.   sleep( RPperiod / 2 )
  321.  end
  322. end
  323.  
  324. function printkeycode(times)  -- Prints whatever it hears
  325.   local times = times
  326.   if not times then
  327.   times = 1
  328.   end
  329.   for n=1,times do
  330.     local e,p1,p2 = os.pullEvent()
  331.     print(e)
  332.     if p1 then
  333.       print(" "..p1)
  334.     end
  335.     if p2 then
  336.       print(" "..p2)
  337.     end
  338.   end
  339. end
  340.  
  341. function space(LinesToSkip) -- Calculates line and goes to the one after
  342.   local cy,my = term.getCursorPos()
  343.   local y = my+LinesToSkip
  344.   term.setCursorPos(cy,y)
  345. end
  346.  
  347. function copyOver(from, to) -- Copys over files from a place to a place
  348.   if fs.exists(to) then
  349.     error(to.." exists")
  350.   end
  351.   fs.copy(from, to)
  352. end
  353.  
  354. function moveOver(from, to) -- Move file from A. to B.
  355.   if fs.exists(to) then
  356.     error(to.." exists")
  357.   end
  358.   fs.move(from, to)
  359. end
  360.  
  361. function Clear() -- Clears screen and sets cursor at 1,1
  362.   term.clear()
  363.   term.setCursorPos(1,1)
  364. end
  365.  
  366. function rsSet() -- Returns the side that changed
  367.   if rsRemember == nil then
  368.     rsRemember = {}
  369.   end
  370.   for i,sSide in ipairs(rs.getSides()) do
  371.     rsRemember[i] = rs.getInput(sSide)
  372.   end
  373. end
  374.  
  375. function rsGet()
  376.   if rsRemember == nil then
  377.     error("Please run rsSet before calling on this")
  378.   end
  379.   local rsReturn = {}
  380.   for i,sSide in ipairs(rs.getSides()) do
  381.     if rsRemember[i] ~= rs.getInput(sSide) then
  382.       rsReturn[#rsReturn+1] = sSide
  383.     end
  384.     rsRemember[i] = rs.getInput(sSide)
  385.   end
  386.   return unpack(rsReturn)
  387. end
  388.  
  389.  
  390. function checkDisk() -- Checks if disk exist and if it does return the diskside
  391.   if fs.exists("/disk") then
  392.     local diskside = fs.getDrive("/disk")
  393.     return diskside
  394.   else
  395.     return false
  396.   end
  397. end
  398.  
  399. function writeVari(...) -- Writes variables to a file
  400.   local name = {...}
  401.   local Meh = nil
  402.   if not name[2] then error("For writeVariables use writeVariables(nameoffile, then variable name, variable, variable name, variable)") end
  403.   Meh = io.open(name[1], "w")
  404.     Meh:write(name[2].." = "..[["]]..name[3]..[["]])
  405.   for n=4,#name,2 do
  406.     if name[n+1] then
  407.       Meh:write("\n"..name[n].." = "..[["]]..name[n+1]..[["]])
  408.     else
  409.       error("For writeVariables use writeVariables(nameoffile, then variable name, variable, variable name, variable)")
  410.     end
  411.   end
  412.   Meh:close()
  413. end
  414.  
  415. function clearLine() -- Clears line it was on and sets the cursor to 1, linenumber
  416.   local x,y = term.getCursorPos()
  417.   local x = 1
  418.   term.clearLine()
  419.   term.setCursorPos(x,y)
  420. end
  421.  
  422. function sendFile(filename, ID) -- Opens a file and sends it as a single string
  423.   if fs.exists(filename) then
  424.     local fp = io.open( filename, "r" )
  425.     local content = ""
  426.     for line in fp:lines() do
  427.       content = content..line.."\n"
  428.     end
  429.     fp:close()
  430.     lib.send(ID, content)
  431.   else  
  432.     error("Need to define a file name which exists")
  433.   end
  434. end
  435.  
  436. function writeFile(line, filename) -- Receives a string and writes it into a file. Best with sendFile
  437.   local fp = io.open( filename, "w" )
  438.   fp:write(line)
  439.   fp:close()
  440. end
  441.  
  442. function tag(msg, tag)
  443.   local msg = "["..tag.."]"..msg
  444.   return msg
  445. end
  446.  
  447. function tagView(msg)
  448.   returnTag = {}
  449.   local last = 1
  450.   local first = 1
  451.   local ReturnMsg = msg
  452.   if string.find(ReturnMsg, "%[", last) and string.find(ReturnMsg, "%]", last+1) then
  453.     while true do
  454.       first = string.find(ReturnMsg, "%[", last)
  455.       last = string.find(ReturnMsg, "%]", last+1)
  456.       if first and last then
  457.         returnTag[#returnTag+1] = string.sub(ReturnMsg, first+1, last-1)
  458.         ReturnMsg = string.sub(ReturnMsg, last+1, string.len(msg))
  459.         if string.sub(ReturnMsg, first, last) == "[msg]" then
  460.           break
  461.         end
  462.       else
  463.         break
  464.       end
  465.     end
  466.     returnTag[#returnTag+1] = ReturnMsg
  467.     return unpack(returnTag)
  468.   else
  469.     return false
  470.   end
  471. end
  472.  
  473. function deTag(msg, tag) -- Remove tags
  474.   return string.gsub(msg, "\["..tag.."\]", "")
  475. end
  476.  
  477. function permVari(variname, contents)
  478.   local varispace = variname.." = "
  479.   local varilen = string.len(varispace)
  480.   local lolol = io.open("Variables", "r")
  481.   local content = {}
  482.   local i = 1
  483.   for line in lolol:lines() do
  484.     if not string.sub(line, 1, varilen) == varispace then
  485.       content[#content+1] = line
  486.       i = i + 1
  487.     end
  488.   end
  489.   lolol:close()
  490.   local lolol = io.open("Variables", "w" )
  491.   lolol:write("\""..variname.."\" = "..contents.."\n")
  492.   for i = 2, #content do
  493.     lolol:write( string.format( "%s\n", content[i] ) )
  494.   end
  495.   lolol:close()
  496. end
  497.  
  498. function removePermVari(variname)
  499.   local varispace = variname.." = "
  500.   local varilen = string.len(varispace)
  501.   local lolol = io.open("Variables", "r")
  502.   local content = {}
  503.   local i = 1
  504.   for line in lolol:lines() do
  505.     if not string.sub(line, 1, varilen) == varispace then
  506.       content[#content+1] = line
  507.       i = i + 1
  508.     end
  509.   end
  510.   lolol:close()
  511.   local lolol = io.open("Variables", "w" )
  512.   for i = 1, #content do
  513.     lolol:write( string.format( "%s\n", content[i] ) )
  514.   end
  515.   lolol:close()
  516. end
  517.  
  518. function checkTable(tablename, check)
  519.   for n, value in pairs(tablename) do
  520.     if value == check then
  521.       return true
  522.     end
  523.   end
  524.   return false
  525. end
  526.  
  527. function findTag(tablename, content)
  528.   local tags = {tagView(tablename)}
  529.   contentlen = string.len(content)
  530.   for n, value in pairs(tags) do
  531.     if string.sub(value, 2, contentlen+1) == content then
  532.       local len = string.len(value)
  533.       return string.sub(value, 2+contentlen, -1)
  534.     end
  535.   end
  536. end
  537.  
  538. function log(msg)
  539.   local i = io.open("Logs", "a")
  540.   i:write("print(\""..msg.."\"\n")
  541.   i:close()
  542. end
  543.  
  544. function delete(file)
  545.   fs.delete(file)
  546. end
  547.  
  548. function bundle(sSide, sColour, sValue)
  549.   local nColour = colors[sColour] or colours[sColour]
  550.   if type(nColour) ~= "number" then
  551.     print( "No such color" )
  552.     return
  553.   end
  554.   if sValue == "true" then
  555.     rs.setBundledOutput( sSide, colors.combine( rs.getBundledOutput( sSide ), nColour ) )
  556.   elseif sValue == "false" then
  557.     rs.setBundledOutput( sSide, colors.subtract( rs.getBundledOutput( sSide ), nColour ) )
  558.   end
  559. end
  560.  
  561. function centerBatch(...) -- Do a bunch of text and it will center it
  562.   local sBatch = {...}
  563.   local sNumber = #sBatch
  564.   local w,h = term.getSize()
  565.   local Height = (h-sNumber)/2
  566.   term.setCursorPos(1, Height)
  567.   for n=1,#sBatch do
  568.     centerPrint(sBatch[n])
  569.   end
  570. end
  571.  
  572. function testBundle(sSide, sColour)
  573.   local nColour = colors[sColour] or colours[sColour]  
  574.   if type(nColour) ~= "number" then  
  575.     print("No such color")
  576.     return  
  577.   end
  578.   if rs.testBundledInput(sSide, nColour) then
  579.     return true
  580.   else
  581.     return false
  582.   end
  583. end
  584.  
  585. function openRednet()
  586.   for _,side in ipairs(rs.getSides()) do
  587.     if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  588.       rednet.open(side)
  589.       return side
  590.     end
  591.   end
  592.   error("No modem attached")
  593. end
  594.  
  595. function monitor(side)
  596.     if peripheral.isPresent(side) then
  597.         term.redirect(peripheral.wrap(side))
  598.     end
  599. end
  600.  
  601. function size(side, number)
  602.   if peripheral.isPresent(side) then
  603.       peripheral.wrap(side).setTextScale(number)
  604.   end
  605. end
  606.  
  607. function read( _sReplaceChar, _tHistory )  
  608.   term.setCursorBlink( true )
  609.  
  610.   local sLine = ""
  611.   local nHistoryPos = nil
  612.   local nPos = 0
  613.   if _sReplaceChar then
  614.     _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  615.   end
  616.  
  617.   local w, h = term.getSize()
  618.   local sx, sy = term.getCursorPos()  
  619.  
  620.   local function redraw()
  621.     local nScroll = 0
  622.     if sx + nPos >= w then
  623.       nScroll = (sx + nPos) - w
  624.     end
  625.  
  626.     term.setCursorPos( sx, sy )
  627.     if _sReplaceChar then
  628.       term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  629.     else
  630.       term.write( string.sub( sLine, nScroll + 1 ) )
  631.     end
  632.     term.setCursorPos( sx + nPos - nScroll, sy )
  633.   end
  634.  
  635.   while true do
  636.     local sEvent, param = os.pullEvent()
  637.     if sEvent == "char" then
  638.       sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  639.       nPos = nPos + 1
  640.       redraw()
  641.      
  642.     elseif sEvent == "key" then
  643.         if param == 28 then
  644.         -- Enter
  645.         break
  646.        
  647.       elseif param == 203 then
  648.         -- Left
  649.         if nPos > 0 then
  650.           nPos = nPos - 1
  651.           redraw()
  652.         end
  653.        
  654.       elseif param == 205 then
  655.         -- Right        
  656.         if nPos < string.len(sLine) then
  657.           nPos = nPos + 1
  658.           redraw()
  659.         end
  660.      
  661.       elseif param == 200 or param == 208 then
  662.                 -- Up or down
  663.         if _tHistory then
  664.           if param == 200 then
  665.             -- Up
  666.             if nHistoryPos == nil then
  667.               if #_tHistory > 0 then
  668.                 nHistoryPos = #_tHistory
  669.               end
  670.             elseif nHistoryPos > 1 then
  671.               nHistoryPos = nHistoryPos - 1
  672.             end
  673.           else
  674.             -- Down
  675.             if nHistoryPos == #_tHistory then
  676.               nHistoryPos = nil
  677.             elseif nHistoryPos ~= nil then
  678.               nHistoryPos = nHistoryPos + 1
  679.             end            
  680.           end
  681.  
  682.           if nHistoryPos then
  683.                       sLine = _tHistory[nHistoryPos]
  684.                       nPos = string.len( sLine )
  685.                     else
  686.             sLine = ""
  687.             nPos = 0
  688.           end
  689.           redraw()
  690.                 end
  691.       elseif param == 14 then
  692.         -- Backspace
  693.         if nPos > 0 then
  694.           sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  695.           sLine = sLine.." "
  696.           nPos = nPos - 1          
  697.           redraw()
  698.           sLine = string.sub( sLine, 1, nPos) .. string.sub( sLine, nPos + 2 )
  699.         end
  700.       end
  701.     end
  702.   end
  703.  
  704.   term.setCursorBlink( false )
  705.   term.setCursorPos( w + 1, sy )
  706.   print()
  707.  
  708.   return sLine
  709. end
  710.  
  711. function readLimit( Limited, _sReplaceChar, _tHistory )  
  712.   term.setCursorBlink( true )
  713.  
  714.   local sLine = ""
  715.   local nHistoryPos = nil
  716.   local nPos = 0
  717.   if _sReplaceChar then
  718.     _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  719.   end
  720.  
  721.   local w, h = term.getSize()
  722.   local sx, sy = term.getCursorPos()  
  723.  
  724.   local function redraw()
  725.     local nScroll = 0
  726.     if sx + nPos >= w then
  727.       nScroll = (sx + nPos) - w
  728.     end
  729.  
  730.     term.setCursorPos( sx, sy )
  731.     if _sReplaceChar then
  732.       term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
  733.     else
  734.       term.write( string.sub( sLine, nScroll + 1 ) )
  735.     end
  736.     term.setCursorPos( sx + nPos - nScroll, sy )
  737.   end
  738.  
  739.   while true do
  740.     local sEvent, param = os.pullEvent()
  741.     if sEvent == "char" then
  742.       if string.len(sLine) < Limited then
  743.       sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  744.       nPos = nPos + 1
  745.       redraw()
  746.       end
  747.      
  748.     elseif sEvent == "key" then
  749.         if param == 28 then
  750.         -- Enter
  751.         break
  752.        
  753.       elseif param == 203 then
  754.         -- Left
  755.         if nPos > 0 then
  756.           nPos = nPos - 1
  757.           redraw()
  758.         end
  759.        
  760.       elseif param == 205 then
  761.         -- Right        
  762.         if nPos < string.len(sLine) then
  763.           nPos = nPos + 1
  764.           redraw()
  765.         end
  766.      
  767.       elseif param == 200 or param == 208 then
  768.                 -- Up or down
  769.         if _tHistory then
  770.           if param == 200 then
  771.             -- Up
  772.             if nHistoryPos == nil then
  773.               if #_tHistory > 0 then
  774.                 nHistoryPos = #_tHistory
  775.               end
  776.             elseif nHistoryPos > 1 then
  777.               nHistoryPos = nHistoryPos - 1
  778.             end
  779.           else
  780.             -- Down
  781.             if nHistoryPos == #_tHistory then
  782.               nHistoryPos = nil
  783.             elseif nHistoryPos ~= nil then
  784.               nHistoryPos = nHistoryPos + 1
  785.             end            
  786.           end
  787.  
  788.           if nHistoryPos then
  789.                       sLine = _tHistory[nHistoryPos]
  790.                       nPos = string.len( sLine )
  791.                     else
  792.             sLine = ""
  793.             nPos = 0
  794.           end
  795.           redraw()
  796.                 end
  797.       elseif param == 14 then
  798.         -- Backspace
  799.         if nPos > 0 then
  800.           sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  801.           sLine = sLine.." "
  802.           nPos = nPos - 1          
  803.           redraw()
  804.           sLine = string.sub( sLine, 1, nPos) .. string.sub( sLine, nPos + 2 )
  805.         end
  806.       end
  807.     end
  808.   end
  809.  
  810.   term.setCursorBlink( false )
  811.   term.setCursorPos( w + 1, sy )
  812.   print()
  813.  
  814.   return sLine
  815. end
Advertisement
Add Comment
Please, Sign In to add comment