Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- HPWebcamAble Presents...
- File Manager
- === Description ====
- This program adds a user interface for file interaction
- ==== Documentation ====
- Youtube Video:
- https://www.youtube.com/watch?v=pdaWStx-rwA
- ==== Installation and Use ====
- Pastebin Code: uz2f7Xbe
- Only works on Advanced Computers (Not pocket, sorry)
- pastebin get uz2f7Xbe fm
- Then run 'fm' (Or what you called it)
- ==== Update History ====
- The pastebin will always have the most recent version
- |1.2.2| <-- This program
- -Updated desciption/version info/other stuff
- |1.2.1|
- -Fixed a bug that cause folders and files to be the same color
- |1.2|
- -Tweaked rename and move functions
- -CraftOS 1.6 MAY(?) work...I didn't do anything :/ but havent seen any bugs
- -A few other minor changes
- *HDD is now C:
- *I PLAN to make disk drives other letters
- |1.1|
- -Centered buttons
- -Added a separate rename function
- -Version and name added to the default path
- -root is now HDD
- -Minor bug fixes
- |1.0|
- -Release
- ]]
- --Variables--
- local version = "1.2"
- w,h = term.getSize()
- local dir = ""
- shell.setDir(dir)
- local tProgram = shell.getRunningProgram()
- local buttonAPI = "button"
- local selected = 0
- local menuS = false
- local input
- local path
- local ypos
- local tempvar
- filePos = {
- [1] = {3,3},
- [2] = {15,3},
- [3] = {27,3},
- [4] = {39,3},
- [5] = {3,7},
- [6] = {15,7},
- [7] = {27,7},
- [8] = {39,7},
- [9] = {3,11},
- [10] = {15,11},
- [11]= {27,11},
- [12]= {39,11},
- [13] = {3,15},
- [14]= {15,15},
- [15]= {27,15},
- [16]= {39,15}
- }
- local cPage = 1
- local nPage = 1
- local run = true
- --Checks
- if not term.isColor() then
- print("This program can only run on an advanced computer")
- return
- --[[elseif os.version() ~= "CraftOS 1.5" then
- print("This program is designed for CraftOS 1.5")
- print("Your version is "..os.version())
- print("Newer or older version may not work")
- print("Run anyways?")
- print("y/n")
- while true do
- event,par1,par2,par3 = os.pullEvent()
- if par1 == keys.y then run = true break end
- if par1 == keys.n then run = false break end
- end]]
- end
- if not run then os.pullEvent() return end
- --Functions--
- function clear(color)
- if color then
- term.setBackgroundColor(color)
- end
- term.clear()
- term.setCursorPos(1,1)
- end
- function sColor(color)
- term.setBackgroundColor(color)
- end
- function drawS(x,y,state)
- if state then sColor(colors.cyan) else sColor(colors.white) end
- term.setCursorPos(x-1,y-1)
- print(string.rep(" ",13))
- term.setCursorPos(x-1,y)
- term.write(" ")
- term.setCursorPos(x-1,y+1)
- term.write(" ")
- term.setCursorPos(x-1,y+2)
- term.write(" ")
- term.setCursorPos(x-1,y+3)
- print(string.rep(" ",13))
- term.setCursorPos(x+11,y)
- term.write(" ")
- term.setCursorPos(x+11,y+1)
- term.write(" ")
- term.setCursorPos(x+11,y+2)
- term.write(" ")
- term.setCursorPos(x+11,y+3)
- term.write(" ")
- end
- function printC(text,y)
- if not y then
- error("printC:No Y value specified")
- end
- term.setCursorPos(math.ceil(w/2-#tostring(text)/2),y)
- term.write(text)
- end
- function window(_w,_h,head,ypos)
- if not _w or not _h then
- error("Window:Missing width (w) or height (h)")
- end
- if not ypos or type(ypos) ~= "number" then ypos = 5 end
- sColor(colors.white)
- term.setCursorPos(1,h)
- term.write(string.rep(" ",w))
- sColor(colors.blue)
- term.setCursorPos(w,1)
- term.write(" ")
- printC(string.rep(" ",_w),ypos)
- term.setTextColor(colors.white)
- if head then
- printC(head,ypos)
- end
- sColor(colors.gray)
- for i = 1, _h do
- printC(string.rep(" ",_w),i+ypos)
- end
- end
- function bar()
- sColor(colors.blue)
- term.setCursorPos(1,1)
- term.write(string.rep(" ",w-1))
- sColor(colors.red)
- term.setTextColor(colors.white)
- term.write("X")
- end
- function testSpot(x,y)
- if y > 1 then
- if #cDir < 17 then
- _rep = #cDir
- else
- _rep = 16
- end
- for _c = 1, _rep do
- if x >= filePos[_c][1] and x <= filePos[_c][1]+10 and y >= filePos[_c][2] and y <= filePos[_c][2]+2 then
- return _c
- end
- end
- end
- return false
- end
- function drawDir()
- clear(colors.white)
- bar()
- sColor(colors.blue)
- cDir = fs.list(shell.dir())
- if shell.dir() == "" then
- printC("C: - File Manager "..version,1)
- else
- printC("/"..shell.dir(),1)
- end
- for i = 1, #cDir do
- local num = i
- local a = i+16*(cPage-1)
- local name = tostring(cDir[a])
- if name == "nil" then break end
- if #name > 9 then
- text = string.sub(name,1,6).."..."
- else
- text = name
- end
- if fs.isDir(shell.dir().."/"..name) then
- sColor(colors.yellow)
- else
- sColor(colors.lightGray)
- end
- if shell.dir() == "" then
- location = "C:/"..name
- else
- location = tostring("C:/"..shell.dir().."/"..name)
- end
- if i > 16 then break end
- x = filePos[i][1]
- y = filePos[i][2]
- term.setCursorPos(x,y)
- term.write(string.rep(" ",11))
- term.setCursorPos(x,y+1)
- term.write(string.rep(" ",math.floor(5.5-#tostring(text)/2)))
- term.write(text)
- term.write(string.rep(" ",math.ceil(5.5-#tostring(text)/2)))
- term.setCursorPos(x,y+2)
- term.write(string.rep(" ",11))
- end
- if #cDir > 16 then
- nPage = math.ceil(#cDir/16)
- term.setTextColor(colors.black)
- sColor(colors.white)
- printC("Page "..cPage.." of "..nPage,2)
- nSelect = cDir[selected+16*(cPage-1)]
- else
- nSelect = cDir[selected]
- end
- if selected > 0 then
- drawS(filePos[selected][1],filePos[selected][2],true)
- sType = fs.isDir(shell.resolve(nSelect))
- menuS = true
- else
- menuS = false
- end
- end
- function confirm(text)
- if not text or #text <= 8 then
- window(8,3,text)
- else
- window(#text+2,3,text)
- end
- term.setCursorPos(23,7)
- sColor(colors.lime)
- term.write("Yes")
- term.setCursorPos(27,7)
- sColor(colors.red)
- term.write("No")
- while true do
- event,par1,par2,par3 = os.pullEvent("mouse_click")
- if par3 == 7 then
- if par2 >= 23 and par2 <=25 then
- return true
- elseif par2 >= 27 and par2 <= 28 then
- return false
- end
- end
- end
- end
- function changeDir()
- window(26,4,"Change Directory")
- printC("Use '/' for root",8)
- sColor(colors.black)
- repeat
- printC(string.rep(" ",18),7)
- term.setCursorPos(17,7)
- input = read()
- until input ~= ""
- path = shell.resolve(input)
- if fs.exists(path) and fs.isDir(path) then
- shell.setDir(path)
- else
- sColor(colors.gray)
- term.setTextColor(colors.red)
- printC("Doesn't Exist!",9)
- sleep(1)
- end
- end
- function info(_path)
- if not _path then
- error("No path")
- end
- name = cDir[_path]
- clear(colors.white)
- term.setTextColor(colors.white)
- sColor(colors.blue)
- term.write(string.rep(" ",w))
- printC(name,1)
- term.setCursorPos(w,1)
- sColor(colors.red)
- print("X")
- term.setCursorPos(1,2)
- term.setTextColor(colors.black)
- sColor(colors.white)
- print(" ")
- term.write("File Path:")
- path = shell.resolve(name)
- print("/"..path)
- term.write("Type:")
- if fs.isDir(path) then
- print("Folder")
- else
- print("File")
- print("Size:"..fs.getSize(path))
- end
- if fs.isReadOnly(shell.resolve(nSelect)) then
- print("This is a read-only item")
- end
- if shell.resolve(nSelect) == tProgram then
- term.setTextColor(colors.yellow)
- print("This program is currently running")
- end
- while true do
- event,par1,par2,par3 = os.pullEvent()
- if event == "mouse_click" then
- if par2 == w and par3 == 1 then
- break
- end
- end
- end
- end
- function addItem()
- window(8,7,"Add...")
- term.setTextColor(colors.white)
- sColor(colors.lime)
- printC("Folder",7)
- printC("File",9)
- sColor(colors.red)
- printC("Cancel",11)
- local choice = nil
- while true do
- event,par1,par2,par3 = os.pullEvent("mouse_click")
- if par3 == 7 and par2 >= 23 and par2 <= 28 then
- choice = 1
- break
- elseif par3 == 9 and par2 >= 24 and par2 <= 27 then
- choice = 2
- break
- elseif par3 == 11 and par2 >= 23 and par2 <=28 then
- choice = 3
- break
- end
- end
- if choice ~= 3 then
- window(26,3,"Enter name")
- sColor(colors.black)
- repeat
- printC(string.rep(" ",22),7)
- term.setCursorPos(15,7)
- input = read()
- until input ~= ""
- if fs.exists(shell.resolve(input)) then
- sColor(colors.gray)
- term.setTextColor(colors.red)
- printC("Already Exists!",8)
- sleep(2)
- elseif choice == 1 then
- fs.makeDir(shell.dir().."/"..input)
- elseif choice == 2 then
- f = fs.open(shell.dir().."/"..input,"w")
- f.close()
- end
- end
- end
- function move(item)
- path = shell.resolve(item)
- if #path <= 26 then
- window(26,5,"Move item")
- else
- window(#path+2,5,"Move item")
- end
- printC("Current path:",6)
- printC(path,7)
- printC("New path:",8)
- printC("Use 'cancel' to cancel",10)
- sColor(colors.black)
- repeat
- printC(string.rep(" ",22),9)
- term.setCursorPos(15,9)
- input = read()
- until input ~= ""
- sColor(colors.gray)
- printC(string.rep(" ",26),10)
- term.setTextColor(colors.red)
- if fs.exists(input) then
- printC("Already Exists!",10)
- sleep(2)
- elseif input == "cancel" then
- printC("Canceled",10)
- sleep(2)
- else
- fs.move(path,input)
- term.setTextColor(colors.lime)
- printC("Success",10)
- sleep(2)
- end
- end
- function cRename(item)
- path = shell.resolve(item)
- if #path <= 26 then
- window(26,5,"Rename item")
- else
- window(#path+2,5,"Rename item")
- end
- printC("Current name:",6)
- printC(item,7)
- printC("New name:",8)
- printC("Use 'cancel' to cancel",10)
- sColor(colors.black)
- repeat
- printC(string.rep(" ",22),9)
- term.setCursorPos(15,9)
- input = read()
- until input ~= ""
- sColor(colors.gray)
- printC(string.rep(" ",26),10)
- term.setTextColor(colors.red)
- if fs.exists(shell.resolve(input)) then
- printC("Already Exists!",10)
- sleep(2)
- elseif string.find(input,"/") then
- printC("Cannot contain '/'",10)
- sleep(2)
- elseif input == "cancel" then
- printC("Canceled",10)
- sleep(2)
- else
- fs.move(path,input)
- term.setTextColor(colors.lime)
- printC("Success",10)
- sleep(2)
- end
- end
- --Main loop program--
- function main()
- while true do
- drawDir()
- if menuS then
- term.setTextColor(colors.black)
- sColor(colors.white)
- printC(nSelect,h-1)
- if sType then --If the item is a folder
- if fs.isReadOnly(shell.resolve(nSelect)) then --If the folder is read-only
- printC("This is a read-only folder O:Open Tab:Info",h)
- else --Normal folder
- printC("O:Open D:Delete M:Move N:Rename Tab:Info",h)
- end
- else --If the item is a file
- if shell.resolve(nSelect) == tProgram then --If the file is this program
- printC("This program is currently running Tab:Info",h)
- elseif fs.isReadOnly(shell.resolve(nSelect)) then --If the file is read-only
- printC("This is a read-only file R:Run P:Preview Tab:Info",h)
- else --Normal file
- printC("R:Run E:Edit D:Delete M:Move N:Rename Tab:Info",h)
- end
- end
- term.setTextColor(colors.white)
- else
- term.setTextColor(colors.black)
- sColor(colors.white)
- if fs.isReadOnly(shell.dir()) then --If folder is read--only
- if #cDir > 16 then
- printC("B:Back Tab:Enter Path Page:PgUp/Down",h)
- else
- printC("B:Back Tab:Enter Path",h)
- cPage = 1
- end
- else --Normal Folder
- if #cDir > 16 then
- printC("C:Create... B:Back Tab:Enter path Page:PgUp/Down",h)
- else
- printC("C:Create... B:Back Tab:Enter path",h)
- cPage = 1
- end
- end
- term.setTextColor(colors.white)
- end
- event,par1,par2,par3,par4 = os.pullEvent() --Main pullEvent--
- if event == "mouse_click" then
- if par2 == w and par3 == 1 then
- break
- else
- click = testSpot(par2,par3)
- if click then
- selected = click
- else
- selected = 0
- end
- end
- elseif event == "key" then
- if selected > 0 then --If an item is selected
- if par1 == keys.tab then
- info(selected)
- end
- if shell.resolve(nSelect) ~= tProgram then
- if par1 == keys.d and not fs.isReadOnly(shell.resolve(nSelect)) then
- if confirm(" Delete? ") then
- fs.delete(shell.resolve(nSelect))
- selected = 0
- end
- elseif par1 == keys.m and not fs.isReadOnly(shell.resolve(nSelect)) then
- os.pullEvent()
- move(nSelect)
- selected = 0
- elseif par1 == keys.n and not fs.isReadOnly(shell.resolve(nSelect)) then
- os.pullEvent()
- cRename(nSelect)
- selected = 0
- end
- if sType then --If the item is a directory
- if par1 == keys.o then
- shell.setDir(shell.resolve(nSelect))
- selected = 0
- end
- else --If the item is a file
- if par1 == keys.r then
- clear(colors.black)
- print("Begin program:")
- state,err = pcall(function() shell.run(nSelect) end )
- if err then
- clear(colors.white)
- bar()
- printC("Program Error Report",1)
- term.setCursorPos(1,3)
- term.setTextColor(colors.black)
- print("An error occured while running "..nSelect)
- print(err)
- while true do
- event,par1,par2,par3 = os.pullEvent("mouse_click")
- if par2 == w and par3 == 1 then
- break
- end
- end
- else
- term.setTextColor(colors.white)
- print("End of program")
- print("Press any key...")
- term.setTextColor(colors.black)
- print("End of program")
- print("Press any key...")
- os.pullEvent("key")
- end
- selected = 0
- elseif par1 == keys.e or par1 == keys.p then
- os.pullEvent()
- shell.run("edit "..nSelect)
- selected = 0
- end
- end
- end
- else --If nothing is selected
- if par1 == keys.tab then
- changeDir()
- elseif par1 == keys.c and not fs.isReadOnly(shell.resolve(shell.dir())) then
- addItem()
- elseif par1 == keys.b then
- shell.run("cd ..")
- elseif par1 == 209 then
- if cPage > 1 then cPage = cPage-1 end
- elseif par1 == 201 then
- if cPage < nPage then cPage = cPage+1 end
- end
- end
- end
- end
- end
- --Program--
- state,err = pcall(function() main() end)
- if err then
- if not string.find("Terminated",err) then
- term.setTextColor(colors.white)
- sColor(colors.black)
- print("CRITICAL ERROR:")
- print(err)
- print("Press any key to continue")
- os.pullEvent("key")
- end
- end
- --after program ends
- clear(colors.black)
- term.setTextColor(colors.white)
- shell.setDir("")
- printC("Thank you for using File Manager",1)
- printC("Version "..version,2)
- print("")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement