ComputerMan123

Webby Console (ComputerCraft)

Jan 21st, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. -- Commands
  2. local command = {
  3.   --[[
  4.   Webby Command API
  5.   By: houseofkraft
  6.  
  7.   Usage:
  8.   cmdname = {
  9.     desc = "A short description of your command.", <required>
  10.     cmd = "any CraftOS shell command" (optional, set to false if you don't want one),
  11.     output = { (optional, set to false if you don't want an output)
  12.       "This is an output",
  13.       "that will be displayed in",
  14.       "this console log. This setting is",
  15.       "completely optional and you can set it to false if",
  16.       "you don't want one."
  17.     }
  18.   }
  19.   It's also recommended to add your command to the help and you MUST add it to the list table!
  20.   ]]--
  21.   list = {
  22.     -- Make sure to put your command in the list!
  23.     "help",
  24.     "reboot",
  25.     "shutdown",
  26.     "exit"
  27.   },
  28.   reboot = {
  29.     desc = "Reboot the computer.",
  30.     cmd = "reboot",
  31.     output = false
  32.   },
  33.   shutdown = {
  34.     desc = "Shutdown the computer.",
  35.     cmd = "shutdown",
  36.     output = false
  37.   },
  38.   exit = {
  39.     desc = "Exit the Webby Console and return to CraftOS."
  40.     cmd = "exit",
  41.     output = false
  42.   },
  43.   help = {
  44.     desc = "Display the list of commands.",
  45.     cmd = false,
  46.     output = {
  47.       -- It's also recommended to add your command to the help command!
  48.       "=== Webby Console Help ===",
  49.       "help - " .. command.help.desc,
  50.       "reboot - " .. command.reboot.desc,
  51.       "shutdown - " .. command.shutdown.desc,
  52.       "exit - " .. command.exit.desc
  53.     }
  54.   }
  55. }
  56.  
  57. function tableFind(arr, text)
  58.   local found = false
  59.   for k,v in pairs(arr) do
  60.     if v == text then
  61.       found = true
  62.     end
  63.   end
  64.   return found
  65. end
  66.  
  67. function gui()
  68.   local color = term.isColor() -- If the system supports color
  69.   term.clear()
  70.   term.setCursorPos(1,1)
  71.   if color then
  72.     term.setTextColor(colors.orange)
  73.   else
  74.     term.setTextColor(colors.gray)
  75.   end
  76.   print("Firmware: " .. os.version())
  77.   print("Running Webby Console")
  78.   print()
  79.   if color then
  80.     term.setTextColor(colors.green)
  81.   else
  82.     term.setTextColor(colors.lightGray)
  83.   end
  84.   write("> ")
  85.   cmd = read()
  86.   if tableFind(command.list, cmd) then
  87.     if command[cmd]["output"] then
  88.       if type(command[cmd]["output"]) == 'table' then
  89.         for k,v in pairs(command[cmd]["output"]) do
  90.           print(v)
  91.         end
  92.       end
  93.     end
  94.     if command[cmd]["cmd"] then
  95.       if type(command[cmd]["cmd"]) == 'string' then
  96.         -- Execute the command
  97.         shell.run(command[cmd]["cmd"])
  98.       end
  99.     end
  100.   end
  101. end
  102.  
  103. function log(text)
  104.   local log = textutils.formatTime(os.time()) .. " " .. log)
  105.   if color then
  106.     term.setTextColor(colors.green)
  107.   else
  108.     term.setTextColor(colors.lightGray)
  109.   end
  110.   write(text .. "\n")
  111. end
Add Comment
Please, Sign In to add comment