Flaghacker

program menu

May 15th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.92 KB | None | 0 0
  1. --[[Settings]]--
  2. --true for blacklist mode, programs table is the blacklist
  3. --false for whitelist mode, programs table is whitelist
  4. local blacklistMode = true
  5. local programs = {}
  6. local selector = {"[", "]"}
  7.  
  8. --[[Variables & peripherals]]--
  9. local width, height = term.getSize()
  10. local list = {}
  11. if blacklistMode then
  12.     for i, name in pairs(fs.list("/")) do
  13.         if not fs.isDir(name) then
  14.             allowed = true
  15.             for j, black in pairs(programs) do
  16.                 if black == name then
  17.                     allowed = false
  18.                 end
  19.             end
  20.             if allowed then
  21.                 table.insert(list, name)
  22.             end
  23.         end
  24.     end
  25. else
  26.     list = programs
  27. end
  28.  
  29. --[[Functions]]--
  30. local function round(number)
  31.     return math.floor(number + .5)
  32. end
  33.  
  34. local function spaces(number)
  35.         for i = 1, number do
  36.                 write(" ")
  37.         end
  38. end
  39.  
  40. local function menu(list, selector)
  41.         local selector = selector or {"[", "]"}
  42.         local longestItem = 0
  43.         local width, height = term.getSize()
  44.         for i, item in pairs(list) do
  45.                 if #item > longestItem then
  46.                         longestItem = #item
  47.                 end
  48.         end
  49.         local selected = 1
  50.         local startX = round((width / 2) - (longestItem / 2)) - 2
  51.         local startY = round((height / 2) - (#list / 2))
  52.         for i, j in pairs(list) do
  53.                 if #j > longestItem then
  54.                         longestItem = #j
  55.                 end
  56.         end
  57.         local running = true
  58.         while running do
  59.                 curX = startX
  60.                 curY = startY
  61.                 for i, item in pairs(list) do
  62.                         term.setCursorPos(curX, curY)
  63.                         if i == selected then
  64.                                 write(selector[1].." "..item)
  65.                                 spaces(longestItem - #item + 1)
  66.                                 write(selector[2])
  67.                         else
  68.                                 spaces(2)
  69.                                 write(item)
  70.                                 spaces(longestItem - #item + 2)
  71.                         end
  72.                         curY = curY + 1
  73.                 end
  74.                 event, key = os.pullEvent("key")
  75.                 if key == keys.up and selected > 1 then
  76.                         selected =  selected - 1
  77.                 elseif key == keys.down and selected < #list then
  78.                         selected =  selected + 1
  79.                 elseif key == keys.enter then
  80.                         return list[selected]
  81.                 elseif key == keys.q then
  82.                         return "quit"
  83.                 end
  84.         end
  85. end
  86.  
  87. local function centerPrint(text, y)
  88.     local width, height = term.getSize()
  89.     term.setCursorPos(round(( width / 2 ) - ( #text / 2 )), y)
  90.     print(text)
  91. end
  92.  
  93.  
  94. --[[Main program]]--
  95. shell.run("clear")
  96. centerPrint("Which program do you want to run?", 2)
  97. local option = menu(list, selector)
  98. shell.run("clear")
  99. shell.run(option)
Advertisement
Add Comment
Please, Sign In to add comment