Advertisement
FFGFlash

.main

Sep 17th, 2021 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | None | 0 0
  1. local App = app.new(...)
  2.  
  3. App.Input = ""
  4. App.CursorPos = 0
  5. App.InputLine = 1
  6. App.CompletionIndex = 1
  7. App.Completions = {}
  8. App.Config,App.ConfigExists = App:load("options.cfg")
  9.  
  10. if App.Config.UseSystemColors == nil then App.Config.UseSystemColors = true end
  11. if App.Config.BackgroundColor == nil or not colors[App.Config.BackgroundColor] then App.Config.BackgroundColor = "black" end
  12. if App.Config.TextColor == nil or not colors[App.Config.TextColor] then App.Config.TextColor = "white" end
  13. if App.Config.AccentColor == nil or not colors[App.Config.AccentColor] then App.Config.AccentColor = "yellow" end
  14. if App.Config.HighlightBackgroundColor == nil or not colors[App.Config.HighlightBackgroundColor] then App.Config.HighlightBackgroundColor = "white" end
  15. if App.Config.HighlightTextColor == nil or not colors[App.Config.HighlightTextColor] then App.Config.HighlightTextColor = "black" end
  16.  
  17. if not App.ConfigExists then App:save("options.cfg", App.Config) end
  18.  
  19. App.EventHandler:connect("key", function(key, held)
  20.   if key == keys.backspace then App:updateInput(0)
  21.   elseif key == keys.delete then App:updateInput(1)
  22.   elseif not held then
  23.     if key == keys.enter then App:executeInput()
  24.     elseif key == keys.up then App:changeCompletion(1)
  25.     elseif key == keys.down then App:changeCompletion(-1)
  26.     elseif key == keys.left then App:moveCursor(-1)
  27.     elseif key == keys.right then App:moveCursor(1)
  28.     elseif key == keys.tab then App:updateInput(App.Completions[App.CompletionIndex])
  29.     end
  30.   end
  31. end)
  32.  
  33. -- App.EventHandler:connect("mouse_scroll", function(direction) App:scroll(-direction) end)
  34.  
  35. App.EventHandler:connect("paste", function(paste) App:updateInput(paste) end)
  36. App.EventHandler:connect("char", function(char) App:updateInput(char) end)
  37.  
  38. function App:scroll(change)
  39.   term.scroll(change)
  40. end
  41.  
  42. function App:changeCompletion(change)
  43.   if not self.Completions then return end
  44.   local max = #self.Completions
  45.   self.CompletionIndex = self.CompletionIndex + change
  46.   if self.CompletionIndex <= 0 then self.CompletionIndex = max
  47.   elseif self.CompletionIndex > max then self.CompletionIndex = 1
  48.   end
  49. end
  50.  
  51. function App:moveCursor(change)
  52.   self.CursorPos = self.CursorPos + change
  53.   local inLen = string.len(self.Input)
  54.   if self.CursorPos < 0 then self.CursorPos = 0
  55.   elseif self.CursorPos > inLen then self.CursorPos = inLen
  56.   end
  57. end
  58.  
  59. function App:updateInput(change)
  60.   if type(change) == "number" then
  61.     local charPos = self.CursorPos + change
  62.     self.Input = string.sub(self.Input,1,charPos-1)..string.sub(self.Input,charPos+1,-1)
  63.     if change <= 0 then self:moveCursor(change-1) end
  64.   else
  65.     self.Input = string.sub(self.Input,1,self.CursorPos)..change..string.sub(self.Input,self.CursorPos+1,-1)
  66.     self:moveCursor(string.len(change))
  67.   end
  68.   self.Completions = shell.complete(self.Input)
  69.   self.CompletionIndex = 1
  70. end
  71.  
  72. function App:executeInput()
  73.   self.Completions = {}
  74.   self.CompletionIndex = 1
  75.   self.CursorPos = 0
  76.   local w,h = term.getSize()
  77.   if self.InputLine + 1 > h then
  78.     self:scroll(h - self.InputLine)
  79.     self.InputLine = self.InputLine - 1
  80.   end
  81.   self:draw()
  82.   term.setCursorBlink(false)
  83.   term.setCursorPos(1,self.InputLine + 1)
  84.   shell.run(self.Input)
  85.   self.Input = ""
  86.   _,self.InputLine = term.getCursorPos()
  87.   term.setCursorBlink(true)
  88. end
  89.  
  90. function App:writeCompletion()
  91.   if not self.Completions or not self.Completions[self.CompletionIndex] then return end
  92.   local tc,tb = term.getTextColor(), term.getBackgroundColor()
  93.   term.setTextColor(colors[self.Config.HighlightTextColor])
  94.   term.setBackgroundColor(colors[self.Config.HighlightBackgroundColor])
  95.   term.write(self.Completions[self.CompletionIndex])
  96.   term.setTextColor(tc)
  97.   term.setBackgroundColor(tb)
  98. end
  99.  
  100. function App:draw()
  101.   term.setCursorPos(1,self.InputLine)
  102.   term.setBackgroundColor(self.Config.UseSystemColors and _G.BackgroundColor or colors[self.Config.BackgroundColor])
  103.   term.clearLine()
  104.   term.setTextColor(colors[self.Config.AccentColor])
  105.   term.write(shell.dir())
  106.   term.write("> ")
  107.   term.setTextColor(self.Config.UseSystemColors and _G.TextColor or colors[self.Config.TextColor])
  108.   local x,_ = term.getCursorPos()
  109.   term.write(self.Input)
  110.   self:writeCompletion()
  111.   term.setCursorPos(x + self.CursorPos,self.InputLine)
  112. end
  113.  
  114. function App:stopped()
  115.  
  116. end
  117.  
  118. term.setTextColor(App.Config.UseSystemColors and _G.TextColor or colors[App.Config.TextColor])
  119. term.setBackgroundColor(App.Config.UseSystemColors and _G.BackgroundColor or colors[App.Config.BackgroundColor])
  120. term.setCursorPos(1,1)
  121. term.clear()
  122. term.setCursorBlink(true)
  123.  
  124. App:start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement