Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- return function(a,u)
- local helper = { App = a, Conns = {}, User = u }
- function helper:connect(e,c,t) self.Conns[#self.Conns+1]=self.App:connect(e,c,t or self) end
- function helper:destroy() for i,conn in ipairs(self.Conns) do self.App:disconnect(conn) end end
- function helper:build()
- self.Input = { Value = "", Index = 0, Line = 2 }
- self.User()
- term.setCursorBlink(true)
- term.setTextColor(system:getTextColor())
- term.setBackgroundColor(system:getBackgroundColor())
- term.setCursorPos(1,1)
- term.clear()
- term.write("Username > ")
- term.write(self.User.Username)
- self:connect("char",self.handleInput)
- self:connect("paste",self.handleInput)
- self:connect("key",self.handleKeyPressed)
- self:connect("timer",self.handleTimer)
- self.Timer = os.startTimer(0.25)
- end
- function helper:draw()
- term.setCursorPos(1,self.Input.Line)
- term.clearLine()
- term.write("Password > ")
- local x,_ = term.getCursorPos()
- term.write(string.gsub(self.Input.Value,".","*"))
- term.setCursorPos(x+self.Input.Index,self.Input.Line)
- end
- function helper:handleTimer(c)
- if c ~= self.Timer then return end
- shell.switchTab(1)
- self.Timer = os.startTimer(0.25)
- end
- function helper:handleInput(c)
- if not c then return
- elseif type(c) == "number" then
- local p = self.Input.Index + c
- self.Input.Value = string.sub(self.Input.Value,1,p-1)..string.sub(self.Input.Value,p+1,-1)
- if c <= 0 then self:moveCursor(c-1) end
- else
- self.Input.Value = string.sub(self.Input.Value,1,self.Input.Index)..c..string.sub(self.Input.Value,self.Input.Index+1,-1)
- self:moveCursor(string.len(c))
- end
- end
- function helper:processInput()
- self.Input.Index = 0
- self:draw()
- term.setCursorBlink(false)
- term.setCursorPos(1,self.Input.Line+1)
- if md5:hash(self.Input.Value) == self.User.Password then
- self.App:activate("menu")
- return
- end
- term.write("Incorrect Password.")
- self.Input.Value = ""
- term.setCursorBlink(true)
- end
- function helper:moveCursor(c)
- self.Input.Index = self.Input.Index + c
- local l = string.len(self.Input.Value)
- if self.Input.Index < 0 then self.Input.Index = 0
- elseif self.Input.Index > l then self.Input.Index = l
- end
- end
- function helper:handleKeyPressed(key, held)
- if key == keys.backspace then self:handleInput(0)
- elseif key == keys.delete then self:handleInput(1)
- elseif not held then
- if key == keys.enter then self:processInput()
- elseif key == keys.left then self:moveCursor(-1)
- elseif key == keys.right then self:moveCursor(1)
- end
- end
- end
- return helper
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement