Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local w, h = term.getSize()
- local sbc = term.setBackgroundColor
- local stc = term.setTextColor
- local scp = term.setCursorPos
- local clr = term.clear
- local clrln = term.clearLine
- local shell = {}
- local str = ""
- local active = true
- local inputindex = #str
- local cursorX = #str
- local viewX = 1
- local function path_goback(path,amm)
- pathtbl={}
- s=""
- pathsize=0
- -- Divide string and count the number of
- -- divisions.
- for str in string.gmatch(path, "([^/]+)") do
- pathtbl[#pathtbl+1] = str
- end
- for k, v in pairs(pathtbl) do
- pathsize=k
- end
- pathsize = pathsize - amm
- -- Split string into words based on seperator.
- pathtbl={}
- for str in string.gmatch(path, "([^/]+)") do
- pathtbl[#pathtbl+1] = str
- end
- -- Based on how large the user wants the string to be
- -- add only the string bits that led up to the user defined
- -- size.
- for k, v in pairs(pathtbl) do
- if(k <= pathsize)then s = s..pathtbl[k].."/" end
- end
- return s
- end
- local function path_getsize(path)
- pathtbl={}
- pathsize=0
- -- Divide string and count the number of
- -- divisions.
- for str in string.gmatch(path, "([^/]+)") do
- pathtbl[#pathtbl+1] = str
- end
- for k, v in pairs(pathtbl) do
- pathsize=k
- end
- return pathsize
- end
- function newpath(name,stuckindex)
- if(name == nil or type(name) ~= "string")then error("PathAPI: Name must be provided for new paths. ") end
- if(string.sub(name,#name,#name) ~= "/")then
- name = name .. "/"
- end
- if(type(stuckindex) ~= "number")then stuckindex = 0 end
- local pathobj = {
- path = name,
- stuckind = stuckindex,
- getsize = function(self)
- return path_getsize(self.path)
- end,
- goback = function(self,amm)
- if(self:getsize() - amm >= self.stuckind)then
- self.path = path_goback(self.path,amm)
- return self.path
- else
- return false
- end
- end,
- getraw = function(self)
- return self.path
- end,
- add = function(self,new)
- self.path = self.path .. new .. "/"
- return self.path
- end,
- set = function(self,npath)
- self.path = npath
- return self.path
- end,
- lockpath = function(self)
- self.stuckind = self:getsize()
- end,
- unlockpath = function(self)
- self.stuckind = 0
- end,
- }
- return pathobj
- end
- ------------------------------------------------
- --]] futils - API
- ------------------------------------------------
- function file_readAll(file,s)
- local f = fs.open(file,"r")
- local cont = f.readAll()
- f.close()
- if(s)then return textutils.unserialize(s) end
- return cont
- end
- function file_readLine(file,s)
- local f = fs.open(file,"r")
- local cont = f.readLine()
- f.close()
- if(s)then return textutils.unserialize(s) end
- return cont
- end
- function file_write(file,data,s)
- local f = fs.open(file,"w")
- if(s)then f.write(textutils.serialize(data)) else
- f.write(data) end
- f.close()
- end
- function file_writeline(file,data,s)
- local f = fs.open(file,"w")
- if(s)then f.writeLine(textutils.serialize(data)) else
- f.writeLine(data) end
- f.close()
- end
- local function formatpath(path)
- local pathtbl={}
- local s=""
- local pathsize=0
- -- Divide string and count the number of
- -- divisions.
- for str in string.gmatch(path, "([^/]+)") do
- pathtbl[#pathtbl+1] = str
- end
- table.remove(pathtbl,1)
- for i = 1, #pathtbl do
- s = s .. pathtbl[i] .. "/"
- end
- return s
- end
- local function getcontent(data,wdata,ctbl)
- -- Returns a table --
- for k, v in pairs(fs.list(data)) do
- if(fs.isDir(data.."/"..v) )then
- getcontent(data..v.."/",v.."/",ctbl)
- else
- --print(data..v)
- local f = fs.open(data..v,"r")
- ctbl[formatpath(data..v)] = {content=f.readAll()}
- f.close()
- end
- end
- return textutils.serialize(ctbl)
- end
- function archive(data,export)
- local content = {}
- content = getcontent('/' .. data .. "/","/",content)
- file_write(export,content)
- return true
- end
- function extract(file,to)
- local cont = textutils.unserialize(file_readAll(file))
- for k,v in pairs(cont) do
- file_write(to .. "/" .. k,v.content)
- end
- end
- -----------------------------------------------------------------------
- --]] Read - API, HUGE thanks to Valithor for pretty much re-writing it
- -----------------------------------------------------------------------
- local function setStartStr(nstr)
- str = nstr
- end
- function novaread(e,maxlen,mslen,x,y)
- term.setCursorBlink(active)
- local function drawstr()
- term.setCursorPos(x,y)
- write(string.rep(" ",mslen+1))
- term.setCursorPos(x,y)
- term.write(str:sub(viewX,viewX+mslen))
- if(cursorX ~= #str and (cursorX+1) < mslen)then
- term.setCursorPos((cursorX)+x,y)
- else
- term.setCursorPos((cursorX)+x,y)
- end
- end
- local function undrawstr()
- term.setCursorPos(x,y)
- write(string.rep(" ",mslen))
- end
- local function updatestr()
- inputindex = inputindex + 1
- drawstr()
- end
- if(active)then
- if(e[1] == "char" and #str < maxlen)then
- if cursorX < mslen and cursorX < #str then
- cursorX = cursorX+1
- end
- if cursorX == mslen and viewX <= #str-mslen then
- viewX = viewX+1
- end
- str = str:sub(1,inputindex) .. tostring(e[2]) .. str:sub(inputindex+1,#str)
- updatestr()
- end
- if(e[1] == "key")then
- local key = e[2]
- if(key == keys.enter)then
- term.setCursorBlink(false)
- active = false
- return true
- end
- if(key == keys.backspace and inputindex > 0)then
- undrawstr()
- if cursorX > 0 then
- cursorX = cursorX-1
- end
- if cursorX == 0 and viewX > 0 then
- viewX = viewX-1
- end
- str = string.sub( str, 1, inputindex - 1 ) .. string.sub( str, inputindex + 1 )
- inputindex = inputindex - 1
- drawstr()
- end
- if(key == keys.left and inputindex > 0)then
- inputindex = inputindex - 1 --
- if cursorX > 0 then
- cursorX = cursorX-1
- end
- if cursorX == 0 and viewX > 0 then
- viewX = viewX-1
- end
- drawstr()
- end
- if(key == keys.right and inputindex < #str)then
- inputindex = inputindex + 1
- if cursorX < mslen and viewX+cursorX<#str+x then
- cursorX = cursorX+1
- end
- if cursorX == mslen and viewX < #str-mslen then
- viewX = viewX+1
- end
- drawstr()
- end
- if(key == keys.delete and inputindex < #str)then
- if cursorX > mslen then
- cursorX = cursorX+1
- end
- undrawstr()
- str = string.sub( str, 1, inputindex ) .. string.sub( str, inputindex + 2 )
- drawstr()
- end
- end
- end
- if(e[1] == "mouse_click" and (e[4] ~= y or e[3] < x or e[3] > x+maxlen) )then
- active = false
- term.setCursorBlink(false)
- elseif(e[1] == "mouse_click" and e[4] == y)then
- if(not active)then
- active = true
- term.setCursorPos(x+inputindex,y)
- term.setCursorBlink(true)
- drawstr()
- end
- if(e[3]-x >= 0 and e[3]<=#str) then
- -- Broken until furthur notice - Lewisk --
- --inputindex = (viewX+e[3])-x
- --cursorX = e[3]-x
- --drawstr()
- end
- end
- end
- function getInput()
- return str
- end
- function resetInput()
- str = ""
- inputindex = #str
- cursorX = #str
- end
- function setInput(nstr)
- str = nstr
- inputindex = #str
- cursorX = #str
- end
- function setActive(act)
- active = act
- end
- function getActive()
- return active
- end
- function readEx(mlen,scroll,startw)
- setActive(false)
- if(startw == nil)then startw = "" end
- setInput(startw)
- local x, y = term.getCursorPos()
- local readxoff = x
- if(startw ~= "")then
- scp(readxoff,y)
- write(getInput())
- end
- local oinp = getInput()
- local ininp = true
- if(scroll)then
- mslen = 1000
- else
- mslen = mlen+1
- end
- term.setCursorBlink(true)
- while ininp do
- local e = {os.pullEvent()}
- local inp = novaread(e,mslen,mlen-1,readxoff,y)
- setActive(true)
- if(inp)then
- ininp = false
- setActive(false)
- local inp = getInput()
- setInput(oinp)
- if(inp == startw)then
- inp = ""
- end
- return inp
- end
- -- Dont let the user out of the read!
- if(getActive() == "false")then setActive(true) end
- end
- end
- ------------------------------------------------
- --]] Utils - API
- ------------------------------------------------
- function init(nshell)
- shell = nshell
- end
- function openReadDialog(title,clrfunc,startread)
- local bw, bh = 30,0
- local sx, sy = w/2-math.ceil(bw/2), (h/2)-math.ceil(bh/2)-1
- local ex, ey = w/2+math.ceil(bw/2), (h/2)+math.floor(bh/2)-1
- paintutils.drawFilledBox(sx+1,sy,ex+2,ey+2,colors.black)
- paintutils.drawFilledBox(sx,sy,ex,ey,colors.lightGray)
- paintutils.drawBox(sx-1,sy-1,ex+1,ey+1,colors.gray)
- scp(sx,sy-1)
- stc(colors.white)
- write(title)
- scp(sx,sy)
- sbc(colors.lightGray)
- local inp = readEx(30,true,startread)
- clrfunc()
- return inp
- end
- function notify(title,txt,clrfunc)
- local bw, bh = 30,5
- local sx, sy = w/2-math.ceil(bw/2), (h/2)-math.ceil(bh/2)+2
- local ex, ey = w/2+math.ceil(bw/2), (h/2)+math.floor(bh/2)+1
- paintutils.drawLine(sx,sy-1,ex,sy-1,colors.gray)
- paintutils.drawFilledBox(sx+1,sy+1,ex+1,ey+1,colors.black)
- paintutils.drawFilledBox(sx,sy,ex,ey,colors.lightGray)
- scp(sx,sy-1)
- stc(colors.white)
- sbc(colors.gray)
- write(title)
- sbc(colors.lightGray)
- local msg = {}
- for str in string.gmatch(txt, "([^*]+)") do
- msg[#msg+1] = str
- end
- for i = 1, #msg do
- sbc(colors.lightGray)
- colorwrite(" " .. msg[i],45,sx,sy+i)
- end
- colorwrite("&4Click&r to close. ",45,sx+8,sy+#msg+2)
- os.pullEvent("mouse_click")
- clrfunc()
- end
- function openYesNo(title,txt,clrfunc,y,n)
- if(y == nil or n == nil)then
- y = "Yes"
- n = "No"
- end
- local ynm = {y, n}
- local inynm = true
- local bw, bh = 30,5
- local opinc = 9
- local sx, sy = w/2-math.ceil(bw/2), (h/2)-math.ceil(bh/2)+2
- local ex, ey = w/2+math.ceil(bw/2), (h/2)+math.floor(bh/2)+1
- paintutils.drawLine(sx,sy-1,ex,sy-1,colors.gray)
- paintutils.drawFilledBox(sx+1,sy+1,ex+1,ey+1,colors.black)
- paintutils.drawFilledBox(sx,sy,ex,ey,colors.lightGray)
- scp(sx,sy-1)
- stc(colors.white)
- sbc(colors.gray)
- write(title)
- sbc(colors.lightGray)
- local msg = {}
- for str in string.gmatch(txt, "([^*]+)") do
- msg[#msg+1] = str
- end
- for i = 1, #msg do
- sbc(colors.lightGray)
- colorwrite(" " .. msg[i],45,sx,sy+i)
- end
- stc(colors.white)
- local function drawm()
- for i = 1, #ynm do
- sbc(colors.red)
- if(i==1)then
- sbc(colors.green)
- end
- scp(sx+(i*opinc),ey-1)
- write(ynm[i])
- end
- end
- local function drawmopt(id,clk)
- sbc(colors.red)
- if(i==1)then
- sbc(colors.green)
- end
- if(clk)then sbc(colors.gray) end
- scp(sx+(id*opinc),ey-1)
- write(ynm[id])
- sleep(0.2)
- end
- local function mupdate()
- while inynm do
- local ev = {os.pullEvent()}
- if(ev[1] == "mouse_click")then
- local x, y = ev[3], ev[4]
- for i = 1, #ynm do
- if( x >= sx+(i*opinc)-1 and x <= sx+(i*opinc)+#ynm[i] and y == math.floor(ey-1) )then
- inynm = false
- drawmopt(i,true)
- return i
- end
- end
- end
- end
- end
- drawm()
- local res = mupdate()
- clrfunc()
- return res
- end
- function isFileImage(file)
- local isImg = false
- local f = fs.open(file,"r")
- if(f)then
- if(tonumber(f.readAll():gsub("%s+",""),16) ~= nil) then
- isImg = true
- end
- f.close()
- end
- return isImg
- end
- function getApisFrom(loc,apis)
- local inloc = false
- if(fs.isDir(loc))then
- inloc = true
- for i = 1, #apis do
- if(fs.exists("CrossBow/apis/"..apis[i]))then
- apis[i] = "CrossBow/apis/" ..apis[i]
- else
- error("Program had to Quit: CrossBow install outdated or malformed. ")
- end
- end
- end
- for i = 1, #apis do
- if(inloc)then
- os.loadAPI((apis[i]))
- else
- os.loadAPI(shell.resolveProgram((apis[i])))
- end
- end
- return inloc
- end
- function colorwrite(str,smax,x,y,special)
- scp(x,y)
- local colorencode = {
- ['&r'] = "black",
- ['&1'] = "blue",
- ['&2'] = "green",
- ['&3'] = "cyan",
- ['&4'] = "red",
- ['&5'] = "purple",
- ['&6'] = "brown",
- ['&7'] = "lightGray",
- ['&8'] = "gray",
- ['&9'] = "lightBlue",
- ['&a'] = "lime",
- ['&b'] = "orange",
- ['&c'] = "pink",
- ['&d'] = "magenta",
- ['&e'] = "yellow",
- ['&f'] = "white",
- }
- if(special)then sbc(special) end
- for a = 1, #str do
- if(string.sub(str,a,a) == "&")then
- if(colorencode[string.sub(str,a,a+1)] ~= nil)then
- stc(colors[colorencode[string.sub(str,a,a+1)]])
- end
- str = string.sub(str,1,a-1) .. string.sub(str,a+1,#str)
- else
- write(string.sub(str,a,a))
- end
- end
- sbc(colors.white)
- end
- ------------------------------------------------
- --]] Zip - API
- ------------------------------------------------
- local function formatpath(path)
- local pathtbl={}
- local s=""
- local pathsize=0
- -- Divide string and count the number of
- -- divisions.
- for str in string.gmatch(path, "([^/]+)") do
- pathtbl[#pathtbl+1] = str
- end
- for k, v in pairs(pathtbl) do
- pathsize=k
- end
- pathsize = pathsize - 1
- -- Based on how large the user wants the string to be
- -- add only the string bits that led up to the user defined
- -- size.
- for k, v in pairs(pathtbl) do
- if(k <= pathsize)then s = s..pathtbl[k].."/" end
- end
- return s
- end
- local function pathlen(path)
- local pathtbl={}
- local s=""
- local pathsize=0
- -- Divide string and count the number of
- -- divisions.
- for str in string.gmatch(path, "([^/]+)") do
- pathtbl[#pathtbl+1] = str
- end
- return #pathtbl
- end
- local function filterdirpath(dir,path)
- local pathtbl={}
- local s=""
- local pathsize=0
- -- Divide string and count the number of
- -- divisions.
- local la = 0
- for str in string.gmatch(path, "([^/]+)") do
- if(la >= pathlen(dir))then
- pathtbl[#pathtbl+1] = str
- end
- la = la + 1
- end
- for i = 1, #pathtbl do
- s = s .. pathtbl[i] .. "/"
- end
- return s
- end
- local function comparesubpath(flpath,subpath)
- return (formatpath(subpath) == flpath) or (subpath:find(flpath) and pathlen(formatpath(subpath)) == pathlen(flpath)+1 )
- end
- local function comparefolderpath(flpath,subpath)
- return
- end
- local function getend(path)
- local pathtbl={}
- local s=""
- local pathsize=0
- -- Divide string and count the number of
- -- divisions.
- for str in string.gmatch(path, "([^/]+)") do
- pathtbl[#pathtbl+1] = str
- end
- return pathtbl[#pathtbl]
- end
- function iszip(file)
- local f = fs.open(file,"r")
- local c = f.readAll()
- f.close()
- return type(c) == "table"
- end
- local function intable(tbl,am)
- for i = 1, #tbl do
- if(tbl[i].n == am)then
- return true
- end
- end
- return false
- end
- function ziplist(zip,dir)
- local folders = {}
- local files = {}
- local list = {}
- local startls = false
- local f = fs.open(zip,"r")
- local c = (f.readAll())
- c = textutils.unserialize(c)
- f.close()
- for k, v in pairs(c) do
- if(comparesubpath(dir,k))then
- if(pathlen(formatpath(k)) == pathlen(dir))then
- files[getend(k)] = {t="file",c=v.content}
- --print("File:" .. getend(k))
- --print("------------------")
- --os.pullEvent("key")
- elseif(pathlen(formatpath(k)) < 2+pathlen(dir) and
- not intable(folders,filterdirpath(dir,formatpath(k))))then
- folders[#folders+1] = {n=filterdirpath(dir,formatpath(k)),t="folder",c=v.content}
- --print("Folder: " .. filterdirpath(dir,formatpath(k)))
- --os.pullEvent("key")
- end
- end
- end
- for k, v in pairs(files) do
- folders[#folders+1] = {n=k,type=v.t,data=v.c}
- end
- for i = 1, #folders do
- list[i] = {n=folders[i].n,type=folders[i].t,data=folders[i].c}
- end
- return list
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement