Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Simple API to protect your program from being terminated by pressing and holding CTRL + T
- local terminated = false
- local useColor = term.isColor ~= nil and term.isColor()
- local terminateEvent
- local function runLocal(parallelFunc, password, func)
- local function pullEvent(...)
- local result = {os.pullEventRaw(...)}
- while result[1] == "terminate" do
- terminateEvent = true
- result = {os.pullEventRaw(...)}
- end
- return unpack(result)
- end
- local function warnTerminate()
- local x, y
- repeat
- x, y = term.getCursorPos()
- term.setCursorPos(1, 2)
- term.write(" ")
- term.setCursorPos(1, 3)
- term.write(" ")
- term.setCursorPos(x, y)
- repeat
- sleep(0.1)
- until terminateEvent or terminated
- if terminated then break end
- terminateEvent = false
- if useColor then
- term.setTextColor(colors.red)
- end
- x, y = term.getCursorPos()
- term.setCursorPos(1, 2)
- term.write("Terminate (CTRL + T) is blocked!")
- term.setCursorPos(1, 3)
- term.write("Please Enter Password!")
- term.setCursorPos(x, y)
- if useColor then
- term.setTextColor(colors.white)
- end
- sleep(2)
- until terminated
- end
- local function passwordCheck()
- local warnWrongPass = false
- repeat
- if warnWrongPass then
- term.clear()
- term.setCursorPos(1, 1)
- if useColor then
- term.setTextColor(colors.red)
- end
- term.write("Wrong Password!")
- sleep(2)
- if useColor then
- term.setTextColor(colors.white)
- end
- else
- warnWrongPass = true
- end
- term.clear()
- term.setCursorPos(1, 1)
- term.write("Enter passwort to terminate: ")
- until read("*") == password
- if useColor then
- term.setTextColor(colors.lime)
- end
- terminated = true
- term.write("Passwort correct!")
- term.setCursorPos(1, 3)
- term.write("Terminating...")
- term.setCursorPos(1, 4)
- if useColor then
- term.setTextColor(colors.white)
- end
- end
- local oldPullEvent = os.pullEvent
- terminateEvent = false
- terminated = false
- os.pullEvent = pullEvent
- parallelFunc(passwordCheck, warnTerminate, func)
- os.pullEvent = oldPullEvent
- end
- function run(password, func)
- runLocal(parallel.waitForAny, password, func)
- end
- function runNotify(password, func)
- runLocal(parallel.waitForAll, password, func)
- end
- function safeRun(func, ...)
- if func == nil then
- error("attempt to call nil", 2)
- end
- local ans
- repeat
- ans = {pcall(func, ...)}
- until ans[1]
- table.remove(ans, 1)
- return unpack(ans)
- end
- function isTerminated()
- return terminated
- end
Advertisement
Add Comment
Please, Sign In to add comment