libraryaddict

Untitled

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