Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local App = app.new()
- App.EventHandler:disconnectAll("terminate")
- App.Input = ""
- App.Bleep = ""
- App.Error = nil
- App.Index = 1
- App.Structure = {
- { Name = "Username", Filter = "[^(%a| )]", Passthrough = function(str)
- os.setComputerLabel(str)
- return str
- end },
- { Name = "Password", Bleep = true, Passthrough = md5.calc }
- }
- App.User = data.load("/.user")
- App.EventHandler:connect("key", function(key, held)
- if key == keys.backspace then App:updateInput()
- elseif not held then
- if key == keys.enter then App:processInput()
- end
- end
- end)
- App.EventHandler:connect("char", function(char) App:updateInput(char) end)
- App.EventHandler:connect("paste", function(paste) App:updateInput(paste) end)
- function App:updateInput(str)
- self.Input = not str and string.sub(self.Input,1,-2) or self.Input..str
- self.Bleep = string.gsub(self.Input, ".", "*")
- end
- function App:validateInput()
- if self.Strut.filter then
- local match = string.match(self.Input, self.Strut.Filter)
- if match then return false, "Invalid character '"..match.."' found." end
- end
- return true
- end
- function App:processInput()
- self.Success,self.Error = self:validateInput()
- if not self.Success then return end
- self.User[self.Strut.Name] = self.Strut.Passthrough and self.Strut.Passthrough(self.Input) or self.Input
- self.Index = self.Index + 1
- if self.Index <= #self.Structure then return self:fetchInput() end
- self:stop()
- end
- function App:fetchInput()
- self.Strut = self.Structure[self.Index]
- self.Input = (not self.User[self.Strut.Name] or self.Strut.Passthrough) and "" or self.User[self.Strut.Name]
- self.Bleep = string.gsub(self.Input, ".", "*")
- self.Error = nil
- end
- function App:draw()
- term.setTextColor(_G.TextColor)
- term.setBackgroundColor(_G.BackgroundColor)
- term.clear()
- term.setCursorPos(1,1)
- term.write(self.Strut.Name)
- term.write("> ")
- term.write(self.Strut.Bleep and self.Bleep or self.Input)
- if self.Error then
- term.setTextColor(colors.red)
- term.setCursorPos(1,2)
- term.write(self.Error)
- end
- end
- function App:stopped()
- data.save("/.user", self.User)
- os.reboot()
- end
- App:fetchInput()
- App:start()
Add Comment
Please, Sign In to add comment