MKlegoman357

Computer manager

Jan 4th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. local running = true
  2.  
  3. local function exit ()
  4.   running = false
  5. end
  6.  
  7. local function clear ()
  8.   term.clear()
  9.   term.setCursorPos(1, 1)
  10. end
  11.  
  12. local func = {
  13.   [1] = {"Wipe computer",
  14.   function ()
  15.     print("\nAre you sure? There is no way back! [y/N]\n")
  16.    
  17.     if ({os.pullEvent("key")})[2] == keys.y then
  18.       clear()
  19.       print("Deleting files:\n")
  20.      
  21.       for i, file in ipairs(fs.list("/")) do
  22.         if not fs.isReadOnly(file) then
  23.           print("Deleting '" .. file .. "'")
  24.           pcall(function () fs.delete(file) end)
  25.          
  26.           if fs.exists(file) then
  27.             print("Couldn't delete '" .. file .. "'")
  28.           end
  29.         end
  30.       end
  31.      
  32.       print("\nDone!\nEject disk and shutdown? [y/N]\n")
  33.      
  34.       if ({os.pullEvent("key")})[2] == keys.y then
  35.         for i, name in ipairs(peripheral.getNames()) do
  36.           if disk.isPresent(name) then
  37.             disk.eject(name)
  38.           end
  39.         end
  40.        
  41.         os.shutdown()
  42.       end
  43.      
  44.       clear()
  45.     end
  46.   end},
  47.  
  48.   [2] = {"File list",
  49.   function ()
  50.     clear()
  51.     shell.run("list")
  52.     print("\nPress any key to go back")
  53.     os.pullEvent("key")
  54.   end},
  55.  
  56.   [3] = {"Shell",
  57.   function ()
  58.     exit()
  59.     clear()
  60.   end},
  61.  
  62.   [4] = {"Lua prompt",
  63.   function ()
  64.     clear()
  65.     shell.run("rom/programs/lua")
  66.   end},
  67.  
  68.   [5] = {"Reboot",
  69.   os.reboot},
  70.  
  71.   [6] = {"Shutdown",
  72.   os.shutdown},
  73.  
  74.   [7] = {"Eject disk and shutdown",
  75.   function ()
  76.     for i, name in ipairs(peripheral.getNames()) do
  77.       if disk.isPresent(name) then
  78.         disk.eject(name)
  79.       end
  80.     end
  81.    
  82.     os.shutdown()
  83.   end},
  84.  
  85.   [keys.getName(keys.s)] = os.shutdown,
  86.  
  87.   [keys.getName(keys.r)] = os.reboot,
  88. }
  89.  
  90. while running do
  91.   clear()
  92.   print("Computer manager by MKlegoman357\n")
  93.  
  94.   for i, t in ipairs(func) do
  95.     print(i .. ". " .. t[1])
  96.   end
  97.  
  98.   local e, b = os.pullEvent()
  99.  
  100.   if e == "char" then
  101.     if func[tonumber(b)] then
  102.       func[tonumber(b)][2]()
  103.     end
  104.   elseif e == "key" then
  105.     b = keys.getName(b)
  106.    
  107.     if func[b] and type(func[b]) == "function" then
  108.       func[b]()
  109.     end
  110.   end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment