Advertisement
FFGFlash

apps/nekos/helpers/login.lua

Sep 29th, 2021 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.71 KB | None | 0 0
  1. return function(a,u)
  2.   local helper = { App = a, Conns = {}, User = u }
  3.   function helper:connect(e,c,t) self.Conns[#self.Conns+1]=self.App:connect(e,c,t or self) end
  4.   function helper:destroy() for i,conn in ipairs(self.Conns) do self.App:disconnect(conn) end end
  5.   function helper:build()
  6.     self.Input = { Value = "", Index = 0, Line = 2 }
  7.     self.User()
  8.     term.setCursorBlink(true)
  9.     term.setTextColor(system:getTextColor())
  10.     term.setBackgroundColor(system:getBackgroundColor())
  11.     term.setCursorPos(1,1)
  12.     term.clear()
  13.     term.write("Username > ")
  14.     term.write(self.User.Username)
  15.     self:connect("char",self.handleInput)
  16.     self:connect("paste",self.handleInput)
  17.     self:connect("key",self.handleKeyPressed)
  18.     self:connect("timer",self.handleTimer)
  19.     self.Timer = os.startTimer(0.25)
  20.   end
  21.  
  22.   function helper:draw()
  23.     term.setCursorPos(1,self.Input.Line)
  24.     term.clearLine()
  25.     term.write("Password > ")
  26.     local x,_ = term.getCursorPos()
  27.     term.write(string.gsub(self.Input.Value,".","*"))
  28.     term.setCursorPos(x+self.Input.Index,self.Input.Line)
  29.   end
  30.  
  31.   function helper:handleTimer(c)
  32.     if c ~= self.Timer then return end
  33.     shell.switchTab(1)
  34.     self.Timer = os.startTimer(0.25)
  35.   end
  36.  
  37.   function helper:handleInput(c)
  38.     if not c then return
  39.     elseif type(c) == "number" then
  40.       local p = self.Input.Index + c
  41.       self.Input.Value = string.sub(self.Input.Value,1,p-1)..string.sub(self.Input.Value,p+1,-1)
  42.       if c <= 0 then self:moveCursor(c-1) end
  43.     else
  44.       self.Input.Value = string.sub(self.Input.Value,1,self.Input.Index)..c..string.sub(self.Input.Value,self.Input.Index+1,-1)
  45.       self:moveCursor(string.len(c))
  46.     end
  47.   end
  48.  
  49.   function helper:processInput()
  50.     self.Input.Index = 0
  51.     self:draw()
  52.     term.setCursorBlink(false)
  53.     term.setCursorPos(1,self.Input.Line+1)
  54.     if md5:hash(self.Input.Value) == self.User.Password then
  55.       self.App:activate("menu")
  56.       return
  57.     end
  58.     term.write("Incorrect Password.")
  59.     self.Input.Value = ""
  60.     term.setCursorBlink(true)
  61.   end
  62.  
  63.   function helper:moveCursor(c)
  64.     self.Input.Index = self.Input.Index + c
  65.     local l = string.len(self.Input.Value)
  66.     if self.Input.Index < 0 then self.Input.Index = 0
  67.     elseif self.Input.Index > l then self.Input.Index = l
  68.     end
  69.   end
  70.  
  71.   function helper:handleKeyPressed(key, held)
  72.     if key == keys.backspace then self:handleInput(0)
  73.     elseif key == keys.delete then self:handleInput(1)
  74.     elseif not held then
  75.       if key == keys.enter then self:processInput()
  76.       elseif key == keys.left then self:moveCursor(-1)
  77.       elseif key == keys.right then self:moveCursor(1)
  78.       end
  79.     end
  80.   end
  81.  
  82.   return helper
  83. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement