Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Auto Updater
- --pastebin.com/3K8mAXvf
- --[[TODO LIST
- 01. TODO check file for corruption. Check size and type of values.
- 02. TODO set a limit for the number of programs that can be managed.
- 03. TODO fix excess characters after read()/event.keypress() functions.
- 04. TODO include optional monitor functionality.
- 05. TODO embed some clearer instructions.
- 06. TODO write some comments in this code :\ .
- 07. TODO transfer Menu instructions to a separate screen to make more room for the list
- ]]
- function Main()
- --check for a save file
- if fs.exists("UpdateList.txt") == false then --If no program list exists then...
- UpdateProgram("viF3LUGT", "UpdateList.txt") --... download it
- end
- --wait for user input
- if CountdownBoolPrompt("Do you wish to view or change the settings?",5) then --..ask if the user wants to change the settings
- SaveCSV(Menu(LoadCSV("UpdateList.txt")), "UpdateList.txt") --load the old list, run the menu, save the new settings
- end
- --Open and download the list of programs
- programsList = LoadCSV("UpdateList.txt")
- --update programs
- term.clear() term.setCursorPos(1,1)
- for program = 1,#programsList do --for all the listed programs...
- if programsList[program][3] == true then --if the program is marked for update...
- UpdateProgram(programsList[program][1], programsList[program][2])
- end
- end
- --run boot programs
- for program = 1,#programsList do --for all the listed programs...
- if programsList[program][4] == true then --if the program is marked for run...
- RunProgram(programsList[program][2])
- end
- end
- end
- function Menu(menuTable) --allows user to edit settings. returns a table of programs and settings
- --find current selection
- local currentSelection = 1
- --if #menuTable == 0 then --if there are no menu items..
- -- menuTable[1] = GetNewPaste() --..get one
- --end
- local menuLength = #menuTable
- repeat
- --print the menu items
- term.clear() term.setCursorPos(1,1)
- print("Arrow keys to navigate. 'H' for help. 'Enter' to exit.")
- local firstItemCol, firstItemRow = term.getCursorPos()
- for menuRow = 1, menuLength do
- --term.setCursorPos(1, menuRow+firstItemRow-1)
- local printString = " " .. menuTable[menuRow][1] .. " " .. menuTable[menuRow][2]
- if menuTable[menuRow][3] == true then
- printString = printString .. " (U)"
- end
- if menuTable[menuRow][4] == true then
- printString = printString .. " (B)"
- end
- print(printString)
- end
- --print the indicator arrow
- term.setCursorPos(1, currentSelection+firstItemRow-1)
- print(">")
- --Receive and process keyboard input
- sleep(0.2)
- local eventtype, key = os.pullEvent() --get keyboard input
- if key == keys.up then --up key
- currentSelection = LoopVariable(currentSelection, menuLength, "-")
- elseif key == keys.down then --down key
- currentSelection = LoopVariable(currentSelection, menuLength, "+")
- elseif key == keys.a then --add a new item. 'a' key
- menuTable[menuLength+1] = GetNewPaste()
- menuLength = menuLength + 1
- elseif key == keys.r then --remove the currently selected item. 'r' key
- for i = currentSelection,menuLength-1 do
- menuTable[i] = menuTable[i+1]
- end
- menuTable[menuLength] = nil
- menuLength = menuLength - 1
- elseif key == keys.u then --toggle update
- menuTable[currentSelection][3] = not menuTable[currentSelection][3]
- elseif key == keys.b then --toggle run on boot
- menuTable[currentSelection][4] = not menuTable[currentSelection][4]
- elseif key == keys.m then
- local tempRow = menuTable[currentSelection]
- for i = currentSelection,menuLength-1 do
- menuTable[i] = menuTable[i+1]
- end
- menuTable[menuLength] = tempRow
- elseif key == keys.h then
- HelpScreen()
- end
- until key == keys.enter
- --return result
- term.clear()
- print("Saving settings...") sleep(2)
- return menuTable
- end
- function SaveCSV(dataTable, fileName) --saves a table as a CSV
- --this routine will create/overwrite a text document and save the link and program name
- local saveFile = fs.open(fileName,"w")
- --iterate through each row of the table
- for tableRow = 1, #dataTable do
- local line = ""
- --iterate through each column of a row
- for tableCol = 1, #dataTable[tableRow] do
- line = line .. tostring(dataTable[tableRow][tableCol]) .. ","
- end
- saveFile.writeLine(line)
- end
- saveFile.close()
- end
- function LoadCSV(fileName) --loads a CSV and returns it as a table
- print("Loading data from existing file..")
- local file = fs.open(fileName,"r") --open the file
- local lineString = file.readLine() --read the first line from the file
- local dataMatrix = {} --create a matrix to store the values in
- local lineNumber = 0
- while lineString ~= null do --until the line is empty
- lineNumber = lineNumber + 1
- dataMatrix[lineNumber] = {} --declare a new row in the matrix
- local startChar = 1 --initialize start position for the string scan
- local endChar = string.find(lineString,",",startChar) --initialize end position for the scan
- while endChar ~= nil do
- dataMatrix[lineNumber][#dataMatrix[lineNumber]+1] = ToBoolean(string.sub(lineString,startChar,endChar-1))
- startChar = endChar + 1
- endChar = string.find(lineString,",",startChar) --look in the string for "," starting from X
- end
- lineString = file.readLine() --read the next line
- end
- file:close()
- return dataMatrix
- end
- function UpdateProgram(pasteLink, programName) --updates a program from pastebin if possile
- print("\n>Updating " .. programName) --display message
- shell.run("rename", programName, "Old" .. programName) --rename old program
- if shell.run("pastebin", "get", pasteLink, programName) then --attempt to download a new version
- shell.run("delete", "Old" .. programName) --if success, delete the old program
- else
- shell.run("rename", "Old" .. programName, programName) --if fail, restore old version
- print("Failed to update. Old version kept.") --print error message
- end
- sleep(1)
- end
- function HelpScreen() --shows a guide for program use.
- term.clear() term.setCursorPos(1,1)
- print("HELP 1 of 2")
- print("1. Navigate the menu unsing UP and DOWN keys.")
- print("2. Use 'A' to add a program to the end of the list.")
- print("3. Use 'R' to remove a program from the list.")
- print("4. Toggle 'Update file' with 'U'. Programs tagged with (U) will be updated from Pastebin on boot.")
- read()
- term.clear() term.setCursorPos(1,1)
- print("HELP 2 of 2")
- print("5. Toggle 'Run on boot' with 'B'. Programs tagged with (B) will be run on boot.")
- print("6. Use 'M' to move a program to the end of the list.")
- print("Note: Updating and Running is done sequentially, top to bottom.")
- print("Hit 'enter' to return to the menu.")
- read()
- end
- function RunProgram(programName) --runs a program in a shell if possible
- print("\n>Running " .. programName) --display message
- if not shell.run(programName) then --run program. if unsuccessful...
- print("Could not run " .. programName)
- sleep(1)
- end
- end
- function GetNewPaste() --gets a new paste. LINK, NAME, FALSE, FALSE
- term.clear() term.setCursorPos(1,1)
- local link, progName
- --get paste link
- repeat
- print("- Add a new program to the list.")
- print("Enter the paste ID of the program you wish to add.\nMust be 8 characters long.")
- print("")
- link = tostring(read())
- term.clear() term.setCursorPos(1,1)
- if string.len(link) ~= 8 then
- print("Link is wrong length. " .. string.len(link))
- end
- until string.len(link) == 8
- term.clear() term.setCursorPos(1,1)
- --get program name
- repeat
- print("- Add a new program to the list.")
- print("Enter program name. Must be over 3 characters")
- print("")
- progName = tostring(read())
- term.clear() term.setCursorPos(1,1)
- if string.len(progName) <= 3 then
- print("Name is wrong length. " .. string.len(progName))
- end
- until string.len(progName) > 3
- term.clear() term.setCursorPos(1,1)
- --return the link, name, do not update, do not run on boot
- shell.run("pastebin", "get", link, progName)
- return {link, progName, false, false}
- end
- function ToBoolean(text) --converts a string to it's boolean if 'true' or 'false'
- if string.lower(text) == "true" then return true end
- if string.lower(text) == "false" then return false end
- return text
- end
- function LoopVariable(i, maximum, direction) --loops variable from 1->maximum->1
- if direction == "+" and i >= maximum then return 1 end
- if direction == "+" and i < maximum then return i+1 end
- if direction == "-" and i <= 1 then return maximum end
- if direction == "-" and i > 1 then return i-1 end
- end
- function CountdownBoolPrompt(text, promptDuration) --prints text and counts down waiting for Y/N
- for timeLeft = promptDuration,0,-1 do --for the numer of seconds specified...
- os.startTimer(1) --start a 1s timer
- term.clear() term.setCursorPos(1,1)
- print(text.."\n(Y/N) Program will continue in "..timeLeft.."s") --print a prompt
- local event, result = os.pullEvent() --detect key press for processing below
- if result == keys.enter or result == keys.y then return true
- elseif result == keys.n then return false
- end
- end
- return false
- end
- term.clear() term.setCursorPos(1,1)
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement