Advertisement
Redxone

RedCmd - OS

Jul 22nd, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. --]] CC-DOS Interface By: Redxone [[--
  2. -- IGN: trainerred2000            
  3. -- BIOS: MultMine
  4. -- Project Leader: Gonow32 :)
  5.  
  6. local _osver = "CS-DOS v3.5"
  7.  
  8. local _DOS = {
  9.     setpos = term.setCursorPos,
  10.     setback = term.setBackgroundColor,
  11.     settext = term.setTextColor,
  12.     clear = function() term.clear() term.setCursorPos(1,1) end;
  13.     inDir = "",
  14.     input_history = {};
  15.     isCmd = false,
  16.     run = true,
  17.     getScreenSize = term.getSize,
  18.     runProg = shell.run,
  19.  
  20.     breakpath = function(inputstr, sep)
  21.             i=1
  22.             t={}
  23.             s=""
  24.             ki=0
  25.         for str in string.gmatch(inputstr, "([^/]+)") do
  26.                 t[i] = str  
  27.                 i = i + 1
  28.         end
  29.         for k, v in pairs(t) do
  30.                ki=k
  31.         end
  32.  
  33.             for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  34.                     t[i] = str  
  35.                     i = i + 1
  36.             end
  37.             for k, v in pairs(t) do
  38.                if(k <= ki-1)then s = s..t[k].."/" end
  39.             end
  40.             return s
  41.     end,
  42.  
  43.    
  44.     getfiles = function(self,dir)
  45.         self.settext(colors.white)
  46.         dir=dir.."/"
  47.         local dirs = {}
  48.         local files = {}
  49.         print("Directory of :"..dir..".")
  50.         for k, v in pairs(fs.list(dir)) do
  51.             if(fs.isDir(dir..v))then table.insert(dirs,v)
  52.             else table.insert(files,v) end
  53.         end
  54.         textutils.tabulate(colors.orange, dirs, colors.white, files)
  55.  
  56.     end,   
  57.  
  58.  
  59.     cmds = {
  60.  
  61.         {name="cd",desc="Change directory",run=function(self,v)
  62.             if(v=="" or v==" ")then print("The syntax of the command is incorrect. \n") return end
  63.  
  64.                     if(fs.exists(shell.resolve(v)))then
  65.                         shell.setDir(shell.resolve(v))
  66.                     else
  67.                         self.settext(colors.red)
  68.                         print("Unknown directory: "..shell.resolve(v).."\n")
  69.                         self.settext(colors.white)
  70.                     end
  71.         end};
  72.  
  73.         {name="reload",desc="Reload CS-DOS",run=function(self,v) self.run=false self.runProg(shell.getRunningProgram()) return end};
  74.         {name="exit",desc="Exits shell",run = function(self, v) self.run = false return end};
  75.         {name="error",desc="Test error message",run = function(self, v) error("Exception Caught!") end};
  76.         {name="cls",desc="Clear screen",run = function(self, v) self.clear() end};
  77.         {name="edit",desc="Edit a file",run = function(self, v) if(v=="" or v==" ")then print("The syntax of the command is incorrect. \n") end self.runProg("edit",v) end};
  78.         {name="dir",desc="Lists files and directorys",run = function(self, v) self.getfiles(self,shell.resolve(v)) end};
  79.         {name="help",desc="Lists all commands",run = function(self, v)
  80.             write("CS-DOS Commands: \n")
  81.             for i = 1, #self.cmds do
  82.                 write("<")
  83.                 self.settext(colors.blue)
  84.                 write(string.upper(self.cmds[i].name)..string.rep(" ",8 - #self.cmds[i].name))
  85.                 self.settext(colors.white)
  86.                 local w, h = self.getScreenSize()
  87.                 write(">")
  88.                 self.settext(colors.lightGray)
  89.                 write(self.cmds[i].desc.."\n")
  90.                 self.settext(colors.white)
  91.             end
  92.          end};
  93.     };
  94.    
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. local _logo = {
  102.    
  103.     " /---|  /--|    /--\    /-\  /--|  ",
  104.     " |      |       |   |  |   | |     ",
  105.     " |      |       |   |  |   | |     ",
  106.     " |      \--\    |   |  |   | |--\  ",
  107.     " |          |   |   |  |   |     | ",
  108.     " |          |   |   |  |   |     | ",
  109.     " \--|   |--/    \--/    \-/  |--/  ",
  110. }
  111.  
  112. _DOS.clear()
  113. -- functions
  114.  
  115. local _printlogo = function()
  116.     _DOS.setback(colors.blue)
  117.     for i = 1, #_logo do
  118.         _DOS.settext(colors.green)
  119.         print(_logo[i])
  120.         _DOS.settext(colors.white)
  121.     end
  122.  
  123.     print(string.rep(" ",_logo[1]:len()))
  124.     print(_osver,string.rep(" ", (_logo[1]:len() - #_osver )))
  125.    
  126.     _DOS.setback(colors.black)
  127. end
  128.  
  129. -- Manage
  130. _system = function()
  131.  
  132.     write(shell.dir().."> ")
  133.  
  134.     _inp = read(nil,_DOS.input_history)
  135.     table.insert(_DOS.input_history,_inp)
  136.    
  137.     _DOS.isCmd = false
  138.  
  139.     -- Catch Potential Error --
  140.     _proInp = _inp
  141.     -- check command
  142.  
  143.     for i = 1, #_DOS.cmds do
  144.         _proInp = _inp:sub(1,#_DOS.cmds[i].name)
  145.         if(_proInp == _DOS.cmds[i].name)then
  146.             _DOS.cmds[i].run(_DOS,_inp:sub(#_DOS.cmds[i].name+2,-1))
  147.             _DOS.isCmd = true
  148.         end
  149.     end
  150.  
  151.     if(not _DOS.isCmd)then
  152.         if(fs.exists(shell.resolve(_inp)))then
  153.             shell.run(shell.resolve(_inp))
  154.         else
  155.             _DOS.settext(colors.gray)
  156.             print("Bad command or file name: ".._inp..". \n")
  157.             _DOS.settext(colors.white)
  158.         end
  159.     end
  160. end
  161.  
  162.  
  163. -- Commands
  164.  
  165.  
  166. -- Loop
  167. _OS = function()
  168.     _printlogo()
  169.     while _DOS.run do
  170.         local _gud, _value = pcall(_system)
  171.  
  172.         if(not _gud)then
  173.             _DOS.settext(colors.red)
  174.             print("ERROR SEVERE: ".._value..". \n")
  175.             _DOS.settext(colors.white)
  176.         end
  177.  
  178.     end
  179. end
  180.  
  181. _OS()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement