FFGFlash

apps/nekos/helpers/term.lua

Sep 29th, 2021 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.88 KB | None | 0 0
  1. return function(app)
  2.   local helper = { App = app, Conns = {} }
  3.   function helper:build()
  4.     self.Completion = { List = {}, Index = 1 }
  5.     self.Input = { Value = "", Index = 0, Line = 1 }
  6.     term.setCursorBlink(true)
  7.     term.setTextColor(system:getTextColor())
  8.     term.setBackgroundColor(system:getBackgroundColor())
  9.     term.setCursorPos(1,1)
  10.     term.clear()
  11.     self:connect("char", self.handleInput)
  12.     self:connect("paste", self.handleInput)
  13.     self:connect("key", self.handleKeyPressed)
  14.     self:connect("terminate", self.handleTerminate)
  15.   end
  16.  
  17.   function helper:handleTerminate()
  18.     self.App:activate("menu")
  19.   end
  20.  
  21.   function helper:connect(e,c,t) self.Conns[#self.Conns+1]=self.App:connect(e,c,t or self) end
  22.  
  23.   function helper:destroy()
  24.     for i,conn in ipairs(self.Conns) do self.App:disconnect(conn) end
  25.     term.setCursorBlink(false)
  26.   end
  27.  
  28.   function helper:draw()
  29.     local c = self:getCompletion()
  30.     term.setCursorPos(1,self.Input.Line)
  31.     term.clearLine()
  32.     term.write("> ")
  33.     local x,_ = term.getCursorPos()
  34.     term.write(self.Input.Value)
  35.     if c then term.write(c) end
  36.     term.setCursorPos(x+self.Input.Index,self.Input.Line)
  37.   end
  38.  
  39.   function helper:handleInput(c)
  40.     if not c then return
  41.     elseif type(c) == "number" then
  42.       local p = self.Input.Index + c
  43.       self.Input.Value = string.sub(self.Input.Value,1,p-1)..string.sub(self.Input.Value,p+1,-1)
  44.       if c <= 0 then self:moveCursor(c-1) end
  45.     else
  46.       self.Input.Value = string.sub(self.Input.Value,1,self.Input.Index)..c..string.sub(self.Input.Value,self.Input.Index+1,-1)
  47.       self:moveCursor(string.len(c))
  48.     end
  49.     if settings.get("shell.autocomplete") then self:fetchShellCompletions()
  50.     else self.Completion = {List={},Index=1}
  51.     end
  52.   end
  53.  
  54.   function helper:getCompletion()
  55.     return self.Completion.List and self.Completion.List[self.Completion.Index] or nil
  56.   end
  57.  
  58.   function helper:changeCompletion(c)
  59.     if not self.Completion.List then return end
  60.     local max = #self.Completion.List
  61.     self.Completion.Index = self.Completion.Index + c
  62.     if self.Completion.Index <= 0 then self.Completion.Index = max
  63.     elseif self.Completion.Index > max then self.Completion.Index = 1
  64.     end
  65.   end
  66.  
  67.   function helper:scroll(c)
  68.     term.scroll(c)
  69.   end
  70.  
  71.   function helper:executeInput()
  72.     self.Completion.List = {}
  73.     self.Completion.Index = 1
  74.     self.Input.Index = 0
  75.     local w,h = term.getSize()
  76.     if self.Input.Index + 1 > h then
  77.       self:scroll(h - self.Input.Line)
  78.       self.Input.Line = self.Input.Line-1
  79.     end
  80.     self:draw()
  81.     term.setCursorBlink(false)
  82.     term.setCursorPos(1,self.Input.Line+1)
  83.     shell.run(self.Input.Value)
  84.     self.Input.Value = ""
  85.     _,self.Input.Line = term.getCursorPos()
  86.     term.setCursorBlink(true)
  87.   end
  88.  
  89.   function helper:moveCursor(c)
  90.     self.Input.Index = self.Input.Index + c
  91.     local l = string.len(self.Input.Value)
  92.     if self.Input.Index < 0 then self.Input.Index = 0
  93.     elseif self.Input.Index > l then self.Input.Index = l
  94.     end
  95.   end
  96.  
  97.   function helper:handleKeyPressed(key, held)
  98.     if key == keys.backspace then self:handleInput(0)
  99.     elseif key == keys.delete then self:handleInput(1)
  100.     elseif not held then
  101.       if key == keys.enter then self:executeInput()
  102.       elseif key == keys.up then self:changeCompletion(1)
  103.       elseif key == keys.down then self:changeCompletion(-1)
  104.       elseif key == keys.left then self:moveCursor(-1)
  105.       elseif key == keys.right then self:moveCursor(1)
  106.       elseif key == keys.tab then self:handleInput(self:getCompletion())
  107.       end
  108.     end
  109.   end
  110.  
  111.   function helper:fetchShellCompletions()
  112.     if not settings.get("shell.autocomplete") then return false, "Autocomplete disabled." end
  113.     self.Completion.List = shell.complete(self.Input.Value)
  114.     self.Completion.Index = 1
  115.     return true
  116.   end
  117.   return helper
  118. end
Add Comment
Please, Sign In to add comment