Advertisement
Guest User

menu.lua

a guest
Jun 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.78 KB | None | 0 0
  1. local term = require("term")
  2. local fs = require("filesystem")
  3. local comp = require("component")
  4. local computer = require("computer")
  5. local key = require("keyboard")
  6. local event = require("event")
  7. local thread = require("thread")
  8. local gpu = comp.gpu
  9.  
  10. local resx, resy = gpu.getResolution()
  11. local halfresx = math.floor(resx/2)
  12. local halfresy = math.floor(resy/2)
  13. local quarterresx = math.floor(halfresx/2)
  14. local quarterresy = math.floor(halfresy/2)
  15.  
  16. local src = 0
  17.  
  18. function printMenu()
  19.  
  20.   term.clear()
  21.   if src == 0 then
  22.     term.setCursor(quarterresx+2,halfresy-2)
  23.     print("[Continue Normally]")
  24.   else
  25.     term.setCursor(quarterresx+3,halfresy-2)
  26.     print("Continue Normally")
  27.   end
  28.   if src == 1 then
  29.     term.setCursor(quarterresx+2,halfresy) -- Will need to change this
  30.     print("[Display Information]")
  31.   else
  32.     term.setCursor(quarterresx+3,halfresy)
  33.     print("Display Information")
  34.   end
  35.   if src == 2 then
  36.     term.setCursor(quarterresx+2,halfresy+2)
  37.     print("[Shutdown Computer]")
  38.   else
  39.     term.setCursor(quarterresx+3,halfresy+2)
  40.     print("Shutdown Computer")
  41.   end
  42.  
  43.   local _,_,_,kn = event.pull("key_down")
  44.  
  45.   if kn == 208 then
  46.     if src == 0 then
  47.       src = src +1
  48.     elseif src == 1 then
  49.       src = src +1
  50.     end
  51.   elseif kn == 200 then
  52.     if src == 1 then
  53.       src = src -1
  54.     elseif src == 2 then
  55.       src = src -1
  56.     end
  57.   end
  58.   if kn == 28 then
  59.     if src == 0 then
  60.       term.clear()
  61.       os.execute("/etc/motd")
  62.       os.exit()
  63.     elseif src == 1 then
  64.       printInfo()
  65.     elseif src == 2 then
  66.       computer.shutdown()
  67.     end
  68.   end
  69.   printMenu()
  70. end
  71.  
  72. function printBarGraph()
  73.   local barMaxCharacters = 9
  74.   local barValue = computer.freeMemory()
  75.   local barMaxValue = computer.totalMemory()
  76.   local barString = "["
  77.   local barValueCharacterCount = math.floor((barValue/barMaxValue)*barMaxCharacters)
  78.   for i=1, barMaxCharacters, 1 do
  79.     if i <= barValueCharacterCount then
  80.       barString = barString .. "#"
  81.     else
  82.       barString = barString .. " "
  83.     end
  84.   end
  85.   barString = barString .. "]"
  86.   term.setCursor(quarterresx+2, halfresy+2)
  87.   print("Free: " .. barString)
  88. end
  89.  
  90. function printInfo()
  91.   id = event.timer(0.5,printMem,math.huge)
  92.   term.clear()
  93.   term.setCursor(quarterresx+2,halfresy-2)
  94.   print("Free Memory: " ..computer.freeMemory()/1024 .. " KB")
  95.   term.setCursor(quarterresx+2,halfresy)
  96.   print("Installed memory: " .. computer.totalMemory()/1024 .. " KB")
  97.   local bar = thread.create(function()
  98.     while true do
  99.       os.sleep(0.5)
  100.       printBarGraph()
  101.     end
  102.   end)
  103.   term.setCursor(quarterresx+5,halfresy+4)
  104.   event.pull("key_down")
  105.   event.cancel(id)
  106.   bar:kill()
  107. end
  108.  
  109. function printMem()
  110.   term.setCursor(quarterresx+2,halfresy-2)
  111.   term.clearLine()
  112.   term.setCursor(quarterresx+2,halfresy-2)
  113.   print("Free Memory: " .. computer.freeMemory()/1024 .. " KB")
  114. end
  115.  
  116. printMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement