FFGFlash

apps/nekos/helpers/setup.lua

Sep 29th, 2021 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. return function(a,u)
  2.   local helper = { App = a, Conns = {}, User = u, Structure = {
  3.     { Name = "Username", Filter = "[^(%a| )]" },
  4.     { Name = "Password", Replacer = "*", Passthrough = function(s) return md5:hash(s) end }
  5.   } }
  6.   function helper:connect(e,c,t) self.Conns[#self.Conns+1]=self.App:connect(e,c,t or self) end
  7.   function helper:destroy() self.User:save() for i,conn in ipairs(self.Conns) do self.App:disconnect(conn) end end
  8.   function helper:build()
  9.     self.Struct = { Value = nil, Index = 0 }
  10.     self.Input = { Value = "", Index = 0, Line = 1 }
  11.     self.User()
  12.     self:connect("char",self.handleInput)
  13.     self:connect("paste",self.handleInput)
  14.     self:connect("key",self.handleKeyPressed)
  15.     self:connect("timer",self.handleTimer)
  16.     self.Timer = os.startTimer(0.25)
  17.     self:nextStruct()
  18.   end
  19.  
  20.   function helper:nextStruct()
  21.     self.Struct.Index = self.Struct.Index + 1
  22.     self.Struct.Value = self.Structure[self.Struct.Index]
  23.     return self.Struct
  24.   end
  25.  
  26.   function helper:draw()
  27.     term.setCursorPos(1,self.Input.Line)
  28.     term.clearLine()
  29.     term.write(self.Struct.Value.Name.." > ")
  30.     local x,_ = term.getCursorPos()
  31.     local r = self.Struct.Value.Replacer
  32.     term.write(r and string.gsub(self.Input.Value,".",r) or self.Input.Value)
  33.     term.setCursorPos(x+self.Input.Index,self.Input.Line)
  34.   end
  35.  
  36.   function helper:handleTimer(c)
  37.     if c ~= self.Timer then return end
  38.     shell.switchTab(1)
  39.     self.Timer = os.startTimer(0.25)
  40.   end
  41.  
  42.   function helper:handleInput(c)
  43.     if not c then return
  44.     elseif type(c) == "number" then
  45.       local p = self.Input.Index + c
  46.       self.Input.Value = string.sub(self.Input.Value,1,p-1)..string.sub(self.Input.Value,p+1,-1)
  47.       if c <= 0 then self:moveCursor(c-1) end
  48.     else
  49.       self.Input.Value = string.sub(self.Input.Value,1,self.Input.Index)..c..string.sub(self.Input.Value,self.Input.Index+1,-1)
  50.       self:moveCursor(string.len(c))
  51.     end
  52.   end
  53.  
  54.   function helper:processInput()
  55.     self.Input.Index = 0
  56.     local w,h = term.getSize()
  57.     if self.Input.Index + 1 > h then
  58.       term.scroll(h - self.Input.Line)
  59.       self.Input.Line = self.Input.Line-1
  60.     end
  61.     self:draw()
  62.     term.setCursorBlink(false)
  63.     term.setCursorPos(1,self.Input.Line+1)
  64.     term.clearLine()
  65.     local f = self.Struct.Value.Filter
  66.     if f then
  67.       local m = string.match(self.Input.Value, f)
  68.       if m then
  69.         print("Invalid character '"..m.."' found.")
  70.         return
  71.       end
  72.     end
  73.     local p = self.Struct.Value.Passthrough
  74.     self.User[self.Struct.Value.Name] = p and p(self.Input.Value) or self.Input.Value
  75.     self.Input.Value = ""
  76.     _,self.Input.Line = term.getCursorPos()
  77.     term.setCursorBlink(true)
  78.     if self.Struct.Index >= #self.Structure then return self.App:activate("login") end
  79.     self:nextStruct()
  80.   end
  81.  
  82.   function helper:moveCursor(c)
  83.     self.Input.Index = self.Input.Index + c
  84.     local l = string.len(self.Input.Value)
  85.     if self.Input.Index < 0 then self.Input.Index = 0
  86.     elseif self.Input.Index > l then self.Input.Index = l
  87.     end
  88.   end
  89.  
  90.   function helper:handleKeyPressed(key, held)
  91.     if key == keys.backspace then self:handleInput(0)
  92.     elseif key == keys.delete then self:handleInput(1)
  93.     elseif not held then
  94.       if key == keys.enter then self:processInput()
  95.       elseif key == keys.left then self:moveCursor(-1)
  96.       elseif key == keys.right then self:moveCursor(1)
  97.       end
  98.     end
  99.   end
  100.  
  101.   return helper
  102. end
Add Comment
Please, Sign In to add comment