FFGFlash

.setup

Sep 14th, 2021 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1. local App = app.new()
  2. App.EventHandler:disconnectAll("terminate")
  3.  
  4. App.Input = ""
  5. App.Bleep = ""
  6. App.Error = nil
  7. App.Index = 1
  8. App.Structure = {
  9.   { Name = "Username", Filter = "[^(%a| )]", Passthrough = function(str)
  10.     os.setComputerLabel(str)
  11.     return str
  12.   end },
  13.   { Name = "Password", Bleep = true, Passthrough = md5.calc }
  14. }
  15. App.User = data.load("/.user")
  16.  
  17. App.EventHandler:connect("key", function(key, held)
  18.   if key == keys.backspace then App:updateInput()
  19.   elseif not held then
  20.     if key == keys.enter then App:processInput()
  21.     end
  22.   end
  23. end)
  24.  
  25. App.EventHandler:connect("char", function(char) App:updateInput(char) end)
  26. App.EventHandler:connect("paste", function(paste) App:updateInput(paste) end)
  27.  
  28. function App:updateInput(str)
  29.   self.Input = not str and string.sub(self.Input,1,-2) or self.Input..str
  30.   self.Bleep = string.gsub(self.Input, ".", "*")
  31. end
  32.  
  33. function App:validateInput()
  34.   if self.Strut.filter then
  35.     local match = string.match(self.Input, self.Strut.Filter)
  36.     if match then return false, "Invalid character '"..match.."' found." end
  37.   end
  38.   return true
  39. end
  40.  
  41. function App:processInput()
  42.   self.Success,self.Error = self:validateInput()
  43.   if not self.Success then return end
  44.   self.User[self.Strut.Name] = self.Strut.Passthrough and self.Strut.Passthrough(self.Input) or self.Input
  45.   self.Index = self.Index + 1
  46.   if self.Index <= #self.Structure then return self:fetchInput() end
  47.   self:stop()
  48. end
  49.  
  50. function App:fetchInput()
  51.   self.Strut = self.Structure[self.Index]
  52.   self.Input = (not self.User[self.Strut.Name] or self.Strut.Passthrough) and "" or self.User[self.Strut.Name]
  53.   self.Bleep = string.gsub(self.Input, ".", "*")
  54.   self.Error = nil
  55. end
  56.  
  57. function App:draw()
  58.   term.setTextColor(_G.TextColor)
  59.   term.setBackgroundColor(_G.BackgroundColor)
  60.   term.clear()
  61.   term.setCursorPos(1,1)
  62.   term.write(self.Strut.Name)
  63.   term.write("> ")
  64.   term.write(self.Strut.Bleep and self.Bleep or self.Input)
  65.   if self.Error then
  66.     term.setTextColor(colors.red)
  67.     term.setCursorPos(1,2)
  68.     term.write(self.Error)
  69.   end
  70. end
  71.  
  72. function App:stopped()
  73.   data.save("/.user", self.User)
  74.   os.reboot()
  75. end
  76.  
  77. App:fetchInput()
  78. App:start()
Add Comment
Please, Sign In to add comment