Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- libraryaddict API
- May be some useless stuff in here. Why not tho xD
- API's:
- event() - Pull event basically
- eventRaw() -- Pull raw event basically.
- send(id, msg) -- Rednet send/broadcast made little easier
- layout(msg, style) -- Centers text and does your style down the side. so | hi! |
- layoutOrder(mess, number, style) -- Centers text and does your style down the side. Also has the text not centered but at one thing
- fillLine(style) -- Fills the line with stuff like "======="
- center(y, mess) -- Centers text and prints it
- slowCenter(y, mess, sec) -- Centers text and prints it slow
- writeCenter(y, mess) -- Centers text and writes it
- centerPrint(mess) -- Center print with no need to say y, Does it after text
- centerWrite(mess) -- Center write with no need to say y, Does it after text
- slowCenterPrint(mess, sec)-- Slow Center print with no need to say y, Does it after text
- cursor(y) -- Set cursor up/down relative. use -1 or 1
- removeline(filename, as many as you like lines.) -- Part of removePerm, This will remove certain lines from files
- removelineNo(filename, unlimited line numbers) -- Part of removePerm, This will remove certain lines from files by the line number
- insertLine(file, lineno, Insertline) -- insert lines into files
- removePerm(Msg) -- removePerm(Msg) - Removes permant stored msgs from perm storage
- compareMsgPerm(Msg)-- compareMsgPerm(Msg) - Check if it exists in perm storage
- compareMsg(Msg)-- compareMsg(Msg) -- check if it exists in the temp storage
- storePerm(Msg) -- storePerm(Msg) - Stores the msg permantly for use any time
- store(Msg, Max) -- store(Msg, Max) - Stored in temp storage. Max is the max amount of msgs to store. default is 5
- pulse(RPside, RPcount, RPperiod) -- Same as redpulse
- printkeycode(times) -- Prints whatever it hears
- space(LinesToSkip) -- Calculates line and goes to the one after
- copyOver(from, to) -- Copys over files from a place to a place
- moveOver(from, to) -- Move file from A. to B.
- Clear() -- Clears screen and sets cursor at 1,1
- rsSet() -- Run before rsGet
- rsGet() -- Run to return what rs input changed. Best to use after all rs events, currently works only for straight redstone. Not bundled.
- checkDisk() -- Checks if disk exist and if it does return the diskside
- writeVari(...) -- Writes variables to a file
- clearLine() -- Clears line it was on and sets the cursor to 1, linenumber
- writeFile(line, filename) -- Receives a string and writes it into a file. Best with sendFile
- sendFile(filename, ID) -- Opens a file and sends it as a single string
- 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
- tag(msg, tag) -- Add a tag
- deTag(msg, tag) -- Remove specific tags
- permVari(variname, contents) -- use to store a permanant variable to stay in usage. variable name, variable contents. make sure you use dofile in startup
- removePermVari(variname) -- Remove from permanant load
- checkTable(table, string) -- Check if string exists in the table
- findFileName(table, string) -- Find the table with the string in it. So "hello" will find [hello there] but not [yo hello]
- log(event) -- Logs the file. You use dofile to use it
- bundle(Side, Color, True/False) -- Sets bundled output
- centerBatch(Msg, MSg, msg, msg etc.) -- Do a bunch of text and it will center it
- testBundle(side, color) -- Check if its on or off
- openRednet() -- Opens rednet wireless. No need to define side.
- monitor(side) -- Changes the screen to that side.
- size(side, number) -- Changes text size on monitor
- ]]
- function send(id, msg) -- Rednet send/broadcast made little easier
- if msg then
- rednet.send(id, msg)
- else
- rednet.broadcast(msg)
- end
- end
- function layout(mess, style) -- Centers text and does your style down the side. so | hi! |
- local style = style
- local mess = mess
- if not style then
- style = "|"
- end
- if not mess then
- mess = ""
- end
- local w,h = term.getSize()
- local middle = (w - mess:len())/2
- local cy,my = term.getCursorPos()
- middle = middle-1
- term.setCursorPos(1, my)
- write(style)
- term.setCursorPos(middle, my)
- write(mess)
- term.setCursorPos(w-1, my)
- print(style)
- end
- function layoutOrder(mess, number, style) -- Centers text and does your style down the side. Also has the text not centered but at one thing
- if not style then
- style = "|"
- end
- if not mess then
- mess = ""
- end
- if not number then
- number = 6
- end
- local w,h = term.getSize()
- local cy,my = term.getCursorPos()
- term.setCursorPos(1, my)
- write(style)
- term.setCursorPos(number, my)
- write(mess)
- term.setCursorPos(w-1, my)
- print(style)
- end
- function fillLine(style) -- Fills the line with stuff like "======="
- local style = style
- if not style then
- style = "|"
- end
- local cy,my = term.getCursorPos()
- local w,h = term.getSize()
- term.setCursorPos(1, my)
- for n=1,w-1 do
- term.write(style)
- end
- print()
- end
- function center(y, mess) -- Centers text and prints it
- local w,h = term.getSize()
- local middle = (w - mess:len())/2 - 1
- term.setCursorPos(middle, y)
- print(mess)
- end
- function slowCenter(y, mess, sec) -- Centers text and prints it slow
- local w,h = term.getSize()
- local middle = (w - mess:len())/2 - 1
- term.setCursorPos(middle, y)
- textutils.slowPrint(mess, sec)
- end
- function writeCenter(y, mess) -- Centers text and writes it
- local w,h = term.getSize()
- local middle = (w - mess:len())/2 - 1
- term.setCursorPos(middle, y)
- write(mess)
- end
- function centerPrint(mess) -- Center print with no need to say y, Does it after text
- local w,h = term.getSize()
- local middle = (w - mess:len())/2 - 1
- local cy,my = term.getCursorPos()
- term.setCursorPos(middle, my)
- print(mess)
- end
- function centerWrite(mess) -- Center write with no need to say y, Does it after text
- local w,h = term.getSize()
- local middle = (w - mess:len())/2 - 1
- local cy,my = term.getCursorPos()
- term.setCursorPos(middle, my)
- write(mess)
- end
- function slowCenterPrint(mess, sec)-- Slow Center print with no need to say y, Does it after text
- local w,h = term.getSize()
- local middle = (w - mess:len())/2 - 1
- local cy,my = term.getCursorPos()
- term.setCursorPos(middle, my)
- textutils.slowPrint(mess, sec)
- end
- function cursor(iy) -- Set cursor up/down relative to one
- local x,y = term.getCursorPos()
- local y = y+iy
- x = 1
- term.setCursorPos(x, y)
- end
- function removeline(filename, ...) -- Part of removePerm, This will remove certain lines from files
- local LinesRemove = {...}
- local fp = io.open( filename, "r" )
- local content = {}
- local i = 1
- for line in fp:lines() do
- content[#content+1] = line
- i = i + 1
- end
- fp:close()
- for n=1,#content do
- for e=1,#LinesRemove do
- if content[n] == LinesRemove[e] then
- table.remove(content,n)
- end
- end
- end
- local fp = io.open( filename, "w" )
- for i = 1, #content do
- fp:write( string.format( "%s\n", content[i] ) )
- end
- fp:close()
- end
- function removeLineNo(filename, ...) -- Say the file you want removed. Then the line numbers of the lines you want removed.
- local LinesRemove = {...}
- local fp = io.open( filename, "r" )
- local content = {}
- local i = 1
- for line in fp:lines() do
- content[#content+1] = line
- i = i + 1
- end
- fp:close()
- for n=1,#LinesRemove do
- table.remove(content,LinesRemove[n])
- for e=1,#LinesRemove do
- LinesRemove[e] = LinesRemove[e]-1
- end
- end
- local fp = io.open( filename, "w" )
- for i = 1, #content do
- fp:write( string.format( "%s\n", content[i] ) )
- end
- fp:close()
- end
- function insertLine(file, lineno, Insertline) -- say the file, Then the line number. Then the line to be inserted.
- local lolol = io.open(file, "r")
- local content = {}
- local i = 1
- for line in lolol:lines() do
- if i == lineno then
- content[#content+1] = Insertline
- i = i + 1
- end
- content[#content+1] = line
- i = i + 1
- end
- lolol:close()
- local lolol = io.open( file, "w" )
- for i = 1, #content do
- lolol:write( string.format( "%s\n", content[i] ) )
- end
- lolol:close()
- end
- function removePerm(Msg) -- removePerm(Msg) - Removes from perm storage
- if fs.exists("PermanantRelayStorage") then
- removeline("PermanantRelayStorage", Msg)
- end
- end
- function compareMsgPerm(Msg)-- compareMsgPerm(Msg) - Check if it exists in perm storage
- if fs.exists("PermanantRelayStorage") then
- local liner = false
- local h = fs.open("PermanantRelayStorage", "r")
- repeat
- local line = h:readLine()
- if line == Msg then
- liner = true
- end
- until not line
- h:close()
- if liner == true then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function compareMsg(Msg)-- compareMsg(Msg) -- check if it exists in the temp storage
- if TempRelayStorage then
- for n=1,#TempRelayStorage do
- if TempRelayStorage[n] == Msg then
- return true
- end
- end
- end
- end
- function storePerm(Msg) -- storePerm(Msg) - Stores the msg permantly for use any time
- local fh = io.open("PermanantRelayStorage", "a")
- fh:write(Msg.."\n")
- fh:close()
- end
- function store(Msg, Max) -- store(Msg, Max) - Stored in temp storage. Max is the max amount of msgs to store. default is 5
- local Max = Max
- if TempRelayStorage == nil then
- TempRelayStorage = {}
- end
- if Max == nil then
- Max = 5
- end
- local tableCurrentNo = 1
- repeat
- if TempRelayStorage[tableCurrentNo] == nil then
- if tableCurrentNo == Max then
- TempRelayStorage[tableCurrentNo] = Msg
- TempRelayStorage[1] = nil
- tableCurrentNo = 1
- else
- TempRelayStorage[tableCurrentNo] = Msg
- TempRelayStorage[tableCurrentNo+1] = nil
- tableCurrentNo = Max+1
- end
- end
- tableCurrentNo = tableCurrentNo+1
- until tableCurrentNo > Max
- end
- function pulse(RPside, RPcount, RPperiod) -- Same as redpulse
- local RPcount = RPcount
- local RPperiod = RPperiod
- if RPcount == nil then
- RPcount = 1
- end
- if RPperiod == nil then
- RPperiod = 0.5
- end
- for n=1,RPcount do
- redstone.setOutput( RPside, true )
- sleep( RPperiod / 2 )
- redstone.setOutput( RPside, false )
- sleep( RPperiod / 2 )
- end
- end
- function printkeycode(times) -- Prints whatever it hears
- local times = times
- if not times then
- times = 1
- end
- for n=1,times do
- local e,p1,p2 = os.pullEvent()
- print(e)
- if p1 then
- print(" "..p1)
- end
- if p2 then
- print(" "..p2)
- end
- end
- end
- function space(LinesToSkip) -- Calculates line and goes to the one after
- local cy,my = term.getCursorPos()
- local y = my+LinesToSkip
- term.setCursorPos(cy,y)
- end
- function copyOver(from, to) -- Copys over files from a place to a place
- if fs.exists(to) then
- error(to.." exists")
- end
- fs.copy(from, to)
- end
- function moveOver(from, to) -- Move file from A. to B.
- if fs.exists(to) then
- error(to.." exists")
- end
- fs.move(from, to)
- end
- function Clear() -- Clears screen and sets cursor at 1,1
- term.clear()
- term.setCursorPos(1,1)
- end
- function rsSet() -- Returns the side that changed
- if rsRemember == nil then
- rsRemember = {}
- end
- for i,sSide in ipairs(rs.getSides()) do
- rsRemember[i] = rs.getInput(sSide)
- end
- end
- function rsGet()
- if rsRemember == nil then
- error("Please run rsSet before calling on this")
- end
- local rsReturn = {}
- for i,sSide in ipairs(rs.getSides()) do
- if rsRemember[i] ~= rs.getInput(sSide) then
- rsReturn[#rsReturn+1] = sSide
- end
- rsRemember[i] = rs.getInput(sSide)
- end
- return unpack(rsReturn)
- end
- function checkDisk() -- Checks if disk exist and if it does return the diskside
- if fs.exists("/disk") then
- local diskside = fs.getDrive("/disk")
- return diskside
- else
- return false
- end
- end
- function writeVari(...) -- Writes variables to a file
- local name = {...}
- local Meh = nil
- if not name[2] then error("For writeVariables use writeVariables(nameoffile, then variable name, variable, variable name, variable)") end
- Meh = io.open(name[1], "w")
- Meh:write(name[2].." = "..[["]]..name[3]..[["]])
- for n=4,#name,2 do
- if name[n+1] then
- Meh:write("\n"..name[n].." = "..[["]]..name[n+1]..[["]])
- else
- error("For writeVariables use writeVariables(nameoffile, then variable name, variable, variable name, variable)")
- end
- end
- Meh:close()
- end
- function clearLine() -- Clears line it was on and sets the cursor to 1, linenumber
- local x,y = term.getCursorPos()
- local x = 1
- term.clearLine()
- term.setCursorPos(x,y)
- end
- function sendFile(filename, ID) -- Opens a file and sends it as a single string
- if fs.exists(filename) then
- local fp = io.open( filename, "r" )
- local content = ""
- for line in fp:lines() do
- content = content..line.."\n"
- end
- fp:close()
- lib.send(ID, content)
- else
- error("Need to define a file name which exists")
- end
- end
- function writeFile(line, filename) -- Receives a string and writes it into a file. Best with sendFile
- local fp = io.open( filename, "w" )
- fp:write(line)
- fp:close()
- end
- function tag(msg, tag)
- local msg = "["..tag.."]"..msg
- return msg
- end
- function tagView(msg)
- returnTag = {}
- local last = 1
- local first = 1
- local ReturnMsg = msg
- if string.find(ReturnMsg, "%[", last) and string.find(ReturnMsg, "%]", last+1) then
- while true do
- first = string.find(ReturnMsg, "%[", last)
- last = string.find(ReturnMsg, "%]", last+1)
- if first and last then
- returnTag[#returnTag+1] = string.sub(ReturnMsg, first, last)
- ReturnMsg = string.sub(ReturnMsg, last+1, string.len(msg))
- if string.sub(ReturnMsg, first, last) == "[msg]" then
- break
- end
- else
- break
- end
- end
- returnTag[#returnTag+1] = ReturnMsg
- return unpack(returnTag)
- else
- return false
- end
- end
- function deTag(msg, tag) -- Remove tags
- return string.gsub(msg, "\["..tag.."\]", "")
- end
- function permVari(variname, contents)
- local varispace = variname.." = "
- local varilen = string.len(varispace)
- local lolol = io.open("Variables", "r")
- local content = {}
- local i = 1
- for line in lolol:lines() do
- if not string.sub(line, 1, varilen) == varispace then
- content[#content+1] = line
- i = i + 1
- end
- end
- lolol:close()
- local lolol = io.open("Variables", "w" )
- lolol:write("\""..variname.."\" = "..contents.."\n")
- for i = 2, #content do
- lolol:write( string.format( "%s\n", content[i] ) )
- end
- lolol:close()
- end
- function removePermVari(variname)
- local varispace = variname.." = "
- local varilen = string.len(varispace)
- local lolol = io.open("Variables", "r")
- local content = {}
- local i = 1
- for line in lolol:lines() do
- if not string.sub(line, 1, varilen) == varispace then
- content[#content+1] = line
- i = i + 1
- end
- end
- lolol:close()
- local lolol = io.open("Variables", "w" )
- for i = 1, #content do
- lolol:write( string.format( "%s\n", content[i] ) )
- end
- lolol:close()
- end
- function checkTable(tablename, check)
- for n, value in pairs(tablename) do
- if value == check then
- return true
- end
- end
- return false
- end
- function findTag(tablename, content)
- local tags = {tagView(tablename)}
- contentlen = string.len(content)
- for n, value in pairs(tags) do
- if string.sub(value, 2, contentlen+1) == content then
- local len = string.len(value)
- return string.sub(value, 2+contentlen, -1)
- end
- end
- end
- function log(msg)
- local i = io.open("Logs", "a")
- i:write("print(\""..msg.."\"\n")
- i:close()
- end
- function delete(file)
- fs.delete(file)
- end
- function bundle(sSide, sColour, sValue)
- local nColour = colors[sColour] or colours[sColour]
- if type(nColour) ~= "number" then
- print( "No such color" )
- return
- end
- if sValue == "true" then
- rs.setBundledOutput( sSide, colors.combine( rs.getBundledOutput( sSide ), nColour ) )
- elseif sValue == "false" then
- rs.setBundledOutput( sSide, colors.subtract( rs.getBundledOutput( sSide ), nColour ) )
- end
- end
- function centerBatch(...) -- Do a bunch of text and it will center it
- local sBatch = {...}
- local sNumber = #sBatch
- local w,h = term.getSize()
- local Height = (h-sNumber)/2
- term.setCursorPos(1, Height)
- for n=1,#sBatch do
- centerPrint(sBatch[n])
- end
- end
- function testBundle(sSide, sColour)
- local nColour = colors[sColour] or colours[sColour]
- if type(nColour) ~= "number" then
- print("No such color")
- return
- end
- if rs.testBundledInput(sSide, nColour) then
- return true
- else
- return false
- end
- end
- function openRednet()
- for _,side in ipairs(rs.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
- rednet.open(side)
- return side
- end
- end
- error("No modem attached")
- end
- function monitor(side)
- if peripheral.isPresent(side) then
- term.redirect(peripheral.wrap(side))
- end
- end
- function size(side, number)
- if peripheral.isPresent(side) then
- peripheral.wrap(side).setTextScale(number)
- end
- end
- function read( _sReplaceChar, _tHistory )
- term.setCursorBlink( true )
- local sLine = ""
- local nHistoryPos = nil
- local nPos = 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- local function redraw()
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- if _sReplaceChar then
- term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- redraw()
- elseif sEvent == "key" then
- if param == 28 then
- -- Enter
- break
- elseif param == 203 then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == 205 then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == 200 or param == 208 then
- -- Up or down
- if _tHistory then
- if param == 200 then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == 14 then
- -- Backspace
- if nPos > 0 then
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- sLine = sLine.." "
- nPos = nPos - 1
- redraw()
- sLine = string.sub( sLine, 1, nPos) .. string.sub( sLine, nPos + 2 )
- end
- end
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- print()
- return sLine
- end
- function readLimit( Limited, _sReplaceChar, _tHistory )
- term.setCursorBlink( true )
- local sLine = ""
- local nHistoryPos = nil
- local nPos = 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- local function redraw()
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- if _sReplaceChar then
- term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- if string.len(sLine) < Limited then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- redraw()
- end
- elseif sEvent == "key" then
- if param == 28 then
- -- Enter
- break
- elseif param == 203 then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == 205 then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == 200 or param == 208 then
- -- Up or down
- if _tHistory then
- if param == 200 then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == 14 then
- -- Backspace
- if nPos > 0 then
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- sLine = sLine.." "
- nPos = nPos - 1
- redraw()
- sLine = string.sub( sLine, 1, nPos) .. string.sub( sLine, nPos + 2 )
- end
- end
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- print()
- return sLine
- end
Advertisement
Add Comment
Please, Sign In to add comment