Advertisement
Piorjade

cLinux GRUB menu

Sep 26th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None | 0 0
  1. --[[
  2.   cLinux001: cLinux 0.0.1 (complete rewrite)
  3.   NAME:        /startup
  4.   CATEGORY:    bootloader
  5.   VERSION:     0.9 (didn't decide a version-system yet.)
  6.   DESCRIPTION: Bootloader, may be called a 'GRUB' clone.
  7.  
  8.   Made by Piorjade & thecrimulo
  9. ]]--
  10.  
  11. --variables
  12. local bootList = {} --The list with bootable images (.i) is going to be stored here
  13. local selected = 1 --The pre-selected image
  14. local defaultcmd = {} --The default command, which the command is booted with
  15.  
  16. --functions
  17.  
  18. local function getList()
  19.     local file = fs.open("/grubcfg", "r")
  20.     local inhalt = file.readAll()
  21.     file.close()
  22.     inhalt = textutils.unserialize(inhalt)
  23.     bootList = inhalt.list
  24.     selected = inhalt.default
  25.     defaultcmd = inhalt.command
  26.  
  27.     for _, a in ipairs(bootList) do
  28.         local i,j = string.find(a, "../")
  29.         if i then
  30.             table.remove(bootList, _)
  31.         end
  32.     end
  33. end
  34.  
  35. local function readNoJump() --Reads the user input, but doesn't jump to the next line when finishing
  36.     local str = ""
  37.     local reading = true
  38.     term.setCursorBlink(true)
  39.     sleep(0.2)
  40.     while reading do
  41.         local _, k = os.pullEventRaw()
  42.         local x, y = term.getCursorPos()
  43.         if _ == "key" and k == keys.enter then
  44.             term.setCursorBlink(false)
  45.             reading = false
  46.             return str
  47.         elseif _ == "key" and k == keys.backspace and x > 1 then
  48.             str = string.reverse(str)
  49.             str = string.sub(str, 2)
  50.             str = string.reverse(str)
  51.             local x, y = term.getCursorPos()
  52.             term.setCursorPos(x-1, y)
  53.             term.write(" ")
  54.             term.setCursorPos(x-1, y)
  55.         elseif _ == "char" then
  56.             str = str..tostring(k)
  57.             term.write(tostring(k))
  58.         end
  59.     end
  60. end
  61.  
  62. local function drawMenu()
  63.     local function clear()
  64.         term.setCursorPos(1,1)
  65.         term.setBackgroundColor(colors.black)
  66.         term.setTextColor(colors.white)
  67.         term.clear()
  68.     end
  69.     clear()
  70.  
  71.    
  72.     oldTerm = term.current()
  73.     local w, h = 15, 11
  74.     local wlist = window.create(term.current(), 26-w/2, 10-h/2, w, h)
  75.     local str = "-GRUB-"
  76.     local str2 = "Please select an image."
  77.     local str3 = "Press E to write additional commands."
  78.     local cmd = window.create(term.current(), 26-#str3/2, 19, #str3, 1)
  79.     cmd.setBackgroundColor(colors.gray)
  80.     cmd.clear()
  81.     term.setCursorPos(26-#str/2, 1)
  82.     term.write(str)
  83.     term.setCursorPos(26-#str2/2, 2)
  84.     term.write(str2)
  85.     term.setCursorPos(26-#str3/2, 18)
  86.     term.write(str3)
  87.     local function redrawList()
  88.         wlist.setBackgroundColor(colors.gray)
  89.         wlist.clear()
  90.         wlist.setCursorPos(1,1)
  91.         wlist.setTextColor(colors.white)
  92.         term.redirect(wlist)
  93.         for _, o in ipairs(bootList) do
  94.             local x, y = term.getCursorPos()
  95.             if _ == selected then
  96.                 term.setBackgroundColor(colors.lightBlue)
  97.                 term.clearLine()
  98.                 term.write(o)
  99.                 if _ < #bootList then
  100.                     term.setCursorPos(1, y+1)
  101.                 end
  102.                 term.setBackgroundColor(colors.gray)
  103.             else
  104.                 term.write(o)
  105.                 if _ < #bootList then
  106.                     term.setCursorPos(1, y+1)
  107.                 end
  108.             end
  109.         end
  110.         term.redirect(oldTerm)
  111.     end
  112.  
  113.     local function redrawCommand()
  114.         term.redirect(cmd)
  115.         term.setCursorPos(1,1)
  116.         term.setBackgroundColor(colors.gray)
  117.         term.setTextColor(colors.lime)
  118.         term.clear()
  119.         if defaultcmd[selected] ~= nil then
  120.             term.write(defaultcmd[selected])
  121.         else
  122.             term.write("NONE")
  123.         end
  124.         term.redirect(oldTerm)
  125.     end
  126.  
  127.     redrawList()
  128.     redrawCommand()
  129.     local running = true
  130.     term.redirect(wlist)
  131.     while running do
  132.         local _, k = os.pullEventRaw("key")
  133.         if k == keys.up and selected > 1 then
  134.             selected = selected-1
  135.             redrawList()
  136.             redrawCommand()
  137.         elseif k == keys.down and selected < #bootList then
  138.             selected = selected+1
  139.             redrawList()
  140.             redrawCommand()
  141.         elseif k == keys.e then
  142.             term.redirect(cmd)
  143.             term.setCursorPos(1,1)
  144.             term.setBackgroundColor(colors.gray)
  145.             term.setTextColor(colors.lime)
  146.             term.clear()
  147.             local e = readNoJump()
  148.             term.redirect(oldTerm)
  149.             defaultcmd[selected] = e
  150.         elseif k == keys.enter then
  151.             if fs.exists("/boot/"..bootList[selected]) == false then
  152.                 clear()
  153.                 printError("/boot/"..bootList[selected].." does not exist.")
  154.                 running = false
  155.                 break
  156.             else
  157.                 running = false
  158.                 clear()
  159.                 local tArgs = {}
  160.                 if defaultcmd[selected] ~= nil then
  161.                     repeat
  162.                         local i, j = string.find(defaultcmd[selected], " ")
  163.                         if i then
  164.                             local a = string.sub(defaultcmd[selected], 1, i-1)
  165.                             table.insert(tArgs, a)
  166.                             defaultcmd[selected] = string.sub(defaultcmd[selected], j+1, #defaultcmd[selected])
  167.                         end
  168.                     until i == nil
  169.                     table.insert(tArgs, defaultcmd[selected])
  170.                 end
  171.                
  172.                 term.redirect(oldTerm)
  173.                 term.setCursorPos(1,1)
  174.                 term.clear()
  175.                 os.run({}, "/boot/"..bootList[selected], unpack(tArgs))
  176.             end
  177.         end
  178.     end
  179.     term.redirect(oldTerm)
  180. end
  181.  
  182.  
  183. if fs.exists("/grubcfg") then
  184.     getList()
  185.    
  186.     if #bootList <= 11 then
  187.         drawMenu()
  188.     elseif #bootList > 11 then
  189.         printError("/grubcfg: Too many entries.")
  190.     elseif #bootList < 1 then
  191.         printError("/grubcfg: No entries.")
  192.     end
  193. else
  194.     printError("/grubcfg: Not found.")
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement