Advertisement
pfgpastebin

CLI os

Feb 4th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.25 KB | None | 0 0
  1. VERSION = 7
  2. os.pullEvent = os.pullEventRaw
  3.  
  4. --Install stuff if missing
  5. if(fs.exists("/CLI/API/cprint"))then
  6.     os.loadAPI("/CLI/API/cprint")
  7. else
  8.     shell.run("pastebin get QR1qTMtm CLI/API/cprint")
  9.     os.reboot()
  10. end
  11. if(fs.exists("/CLI/Skins"))then
  12. else
  13.     fs.makeDir("/CLI/Skins")
  14. end
  15. if(fs.exists("/CLI/Settings"))then
  16. else
  17.     fs.makeDir("/CLI/Settings")
  18. end
  19. if(fs.exists("/CLI/Settings/skin"))then
  20. else
  21.     defaultskin=fs.open("/CLI/Settings/skin","w")
  22.     defaultskin.write("/CLI/Skins/default")
  23.     defaultskin.close()
  24. end
  25. if(fs.exists("/CLI/Skins/default"))then
  26. else
  27.     shell.run("pastebin get gVatgky8 /CLI/Skins/default")
  28.     os.reboot()
  29. end
  30. print("Exiting CraftOS")
  31. sleep(1)
  32. term.clear()
  33. term.setCursorPos(1,1)
  34. print("Starting CLI")
  35. for i = 1, 100, 1 do
  36.     term.setCursorPos(1,1)
  37.     print("Starting CLI "..i.."%         ")
  38.     term.setCursorPos(1,2)
  39.     if(i == 1)then
  40.         print("Loading kernel         ")
  41.     elseif(i == 13)then
  42.         print("Loading file system         ")
  43.     elseif(i == 26)then
  44.         print("Loading APIs         ")
  45.     elseif(i == 43)then
  46.         print("Locking you in         ")
  47.     elseif(i == 87)then
  48.         print("Applying your settings         ")
  49.     end
  50.     sleep(math.random()/8)
  51. end
  52.  
  53. term.clear()
  54. term.setCursorPos(1,1)
  55. print("It works! This is the default web page for this server.")
  56. sleep(1)
  57. function helpprint(cmd, desc)
  58.     term.setTextColor(s.textColor)
  59.     write(cmd)
  60.     term.setTextColor(s.textColorLight)
  61.     print(" - "..desc)
  62. end
  63. function errorprint(desc)
  64.     --term.setTextColor(s.errorColor)
  65.     --write(s.errorText)
  66.     --term.setTextColor(s.errorColorLight)
  67.     --print(desc)
  68.     cprint.cprint(string.gsub(s.text.errorFormat,"*s",desc))
  69. end
  70. function string.starts(String,Start)
  71.    return string.sub(String,1,string.len(Start))==Start
  72. end
  73.  
  74. function string.ends(String,End)
  75.    return End=='' or string.sub(String,-string.len(End))==End
  76. end
  77.  
  78. function loadSave()
  79.     defaultskin=fs.open("/CLI/Settings/skin","r")
  80.     dir = defaultskin.readAll()
  81.     defaultskin.close()
  82.    
  83.     s={text={errorFormat = "&1Error: *s"}}
  84.     dir = formatPath(dir,"","",true)
  85.    
  86.    
  87.     if(dir == false)then
  88.         loadSkin("/CLI/Skins/default")
  89.         errorprint("Error loading your skin. Make sure it still exists. Type 'skin resetdefault' to reset the default skin")
  90.         sleep(10)
  91.     else
  92.         loadSkin(dir)
  93.     end
  94.     s2 = {
  95.         textColor = colors.white,
  96.         textColorLight = colors.lightGray,
  97.        
  98.         bgColor = colors.black,
  99.        
  100.         lsFolder = colors.white,
  101.         lsFile = colors.lightGray,
  102.        
  103.         text = {
  104.             cmdFormat = "&0*p> ",
  105.             errorFormat = "Error: *s"}}
  106. end
  107. function loadSkin(file)
  108.     pt = formatPath(file,path,"",true)
  109.     if(pt == false)then
  110.         return(false)
  111.     else
  112.         skin = fs.open(formatPath(file,path,"",true),"r")
  113.     end
  114.     skn = skin.readAll()
  115.     skin.close()
  116.     s = textutils.unserialize(skn)
  117. end
  118. function saveSkin(file)
  119.     skin=fs.open(formatPath(file,path,"",false),"w")
  120.     skin.write(textutils.serialize(s))
  121.     skin.close()
  122. end
  123. function formatPath(file,current,enchar,reqExist)
  124.     local pathb = current
  125.     local cpath = current
  126.     local echar = enchar
  127.     local allowerror = true
  128.     if(enchar == "//")then
  129.         allowerror = false
  130.         echar = "/"
  131.     end
  132.     if(string.ends(file,enchar))then echar = "" end
  133.     if(string.starts(file,"/"))then
  134.         cpath = file..echar
  135.     elseif(string.starts(file,"~"))then
  136.         cpath = string.gsub (file, "~", home)..echar
  137.     else
  138.         cpath = cpath..file..echar
  139.     end
  140.     if(reqExist)then
  141.         if(fs.exists(cpath))then
  142.             if(fs.isDir(cpath))then
  143.                 if(enchar == "")then
  144.                     if allowerror then errorprint("File not found") end
  145.                     return(false)
  146.                 end
  147.             else
  148.                 if(enchar == "/")then
  149.                     if allowerror then errorprint("Directory not found") end
  150.                     return(pathb)
  151.                 end
  152.             end
  153.         else
  154.             if(enchar == "/")then
  155.                 if allowerror then errorprint("Directory not found") end
  156.                 return(pathb)
  157.             else
  158.                 if allowerror then errorprint("File not found") end
  159.                 return(false)
  160.             end
  161.             return(pathb)
  162.         end
  163.     end
  164.     return(cpath)
  165. end
  166. loadSave()
  167. if(tonumber((http.get("http://pastebin.com/raw.php?i=XE9Hbau4")).readAll()) > VERSION)then
  168.     fs.delete("install")
  169.     startup=fs.open("startup","w")
  170.     startup.write('shell.run("update")')
  171.     startup.close()
  172.     os.reboot()
  173. else
  174.     term.setBackgroundColor(s.bgColor)
  175.     term.clear()
  176.     term.setCursorPos(1,1)
  177.     home = "/CLI/Usr"
  178.     path = "/CLI/Usr/"
  179.     user = "User"
  180.     computerName = "CLI"
  181.     while true do
  182.         term.setBackgroundColor(s.bgColor)
  183.         term.setTextColor(s.textColor)
  184.         pathv = path
  185.         pathv = string.gsub (pathv, home, "~")
  186.         gsubbed = string.gsub (string.gsub (string.gsub (s.text.cmdFormat, "*u", user),"*c",computerName),"*p",pathv)
  187.         cprint.cwrite(gsubbed)
  188.         command = read()
  189.         if(command == "help")then
  190.             print("Help:")
  191.             helpprint("help", "List commands")
  192.             helpprint("shutdown", "Shut down the OS")
  193.             helpprint("reboot", "Reboot the OS with no warning")
  194.             helpprint("cd <directory>", "Switch directories")
  195.             helpprint("ls", "List content of current directory")
  196.             helpprint("pwd", "Display current directory")
  197.             helpprint("skin", "Skin your CLI")
  198.             helpprint("clear", "Clear the screen")
  199.             helpprint("mkdir <path>","Creates a directory")
  200.             helpprint("open <file>","Opens <file>")
  201.             helpprint("edit <file>","Edits <file>")
  202.             helpprint("rm <file>","Deletes <file>")
  203.         elseif (command == "exit")then
  204.             print("Do you really think I'd let you out that easily?")
  205.             sleep(1)
  206.             textutils.slowPrint("Don't exit don't exit don't exit don't exit")
  207.             os.reboot()
  208.         elseif (command == "shutdown")then
  209.             os.shutdown()
  210.         elseif (command == "reboot")then
  211.             os.reboot()
  212.         elseif (command=="")then
  213.            
  214.         elseif (command=="clear")then
  215.             term.clear()
  216.             term.setCursorPos(1,1)
  217.         elseif (command=="ls")then
  218.             local tArgs = { ... }
  219.             local sDir = path
  220.             local tAll = fs.list( sDir )
  221.             local tFiles = {}
  222.             local tDirs = {}
  223.  
  224.             for n, sItem in pairs( tAll ) do
  225.                 if string.sub( sItem, 1, 1 ) ~= "." then
  226.                     local sPath = fs.combine( sDir, sItem )
  227.                     if fs.isDir( sPath ) then
  228.                         table.insert( tDirs, sItem )
  229.                     else
  230.                         table.insert( tFiles, sItem )
  231.                     end
  232.                 end
  233.             end
  234.             table.sort( tDirs )
  235.             table.sort( tFiles )
  236.            
  237.             textutils.pagedTabulate( s.lsFolder, tDirs, s.lsFile, tFiles )
  238.         elseif (command=="pwd")then
  239.             print(path)
  240.         else
  241.             words = {}
  242.             for word in command:gmatch("%S+") do table.insert(words, word) end
  243.             --print(table.concat(words,","))
  244.             length = table.maxn(words)
  245.             if(words[1] == "cd")then
  246.                 if(length == 2)then
  247.                 --  local pathb = path
  248.                 --  local echar = "/"
  249.                 --  if(string.ends(words[2],"/"))then echar = "" end
  250.                 --  if(string.starts(words[2],"/"))then
  251.                 --      path = words[2]..echar
  252.                 --  elseif(string.starts(words[2],"~"))then
  253.                 --      path = string.gsub (words[2], "~", home)..echar
  254.                 --  else
  255.                 --      path = path..words[2]..echar
  256.                 --  end
  257.                 --  if(fs.exists(path))then
  258.                 --      if(fs.isDir(path))then
  259.                 --      else
  260.                 --          errorprint("Directory not found")
  261.                 --          path = pathb
  262.                 --      end
  263.                 --  else
  264.                 --      errorprint("Directory not found")
  265.                 --      path = pathb
  266.                 --  end
  267.                     path = formatPath(words[2],path,"/",true)
  268.                 else
  269.                     helpprint("Usage", "cd <directory>")
  270.                 end
  271.             elseif(words[1] == "yes")then
  272.                 print("You don't want to do that...")
  273.             elseif(words[1] == "open")then
  274.                 if(length > 1)then
  275.                     local files = formatPath(words[2],path,"",true)
  276.                     local args = ""
  277.                     if(length > 2)then
  278.                         args = words
  279.                         table.remove(args, 1)
  280.                         table.remove(args, 1)
  281.                         args = table.concat(args," ")
  282.                         args = " "..args
  283.                     end
  284.                     if(not files == false)then
  285.                         shell.run("cd "..path)
  286.                         shell.run(files..args)
  287.                     end
  288.                 else
  289.                     helpprint("open <file>","Opens <file>")
  290.                 end
  291.             elseif(words[1] == "edit")then
  292.                 if(length == 2)then
  293.                     shell.run('edit '..formatPath(words[2],path,"",false))
  294.                 else
  295.                     helpprint("edit <file>","Edits <file>")
  296.                 end
  297.             elseif(words[1] == "mv" or words[1] == "cp")then
  298.                 if(length == 3)then
  299.                     olddir = formatPath(words[2],path,"//",true)
  300.                     if(olddir == false)then
  301.                         olddir = formatPath(words[2],path,"",true)
  302.                     end
  303.                     if(olddir == false)then
  304.                     else
  305.                         newdir = formatPath(words[3],path,"//",false)
  306.                         if(newdir == false)then
  307.                             newdir = formatPath(words[3],path,"",false)
  308.                         end
  309.                         if(newdir == false)then
  310.                         else
  311.                             if(fs.exists(newdir))then
  312.                                 errorprint(words[3].." already exists")
  313.                             else
  314.                                 if(words[1] == "mv")then
  315.                                     fs.move(olddir,newdir)
  316.                                 elseif(words[1] == "cp")then
  317.                                     fs.copy(olddir,newdir)
  318.                                 end
  319.                             end
  320.                         end
  321.                     end
  322.                 else
  323.                     helpprint("mv <oldlocation> <newlocation>","Moves oldlocation to newlocation")
  324.                     helpprint("cp <file> <newlocation>","Copies file and puts it at newlocation")
  325.                 end
  326.             elseif(words[1] == "rm")then
  327.                 if(length == 2)then
  328.                     files = formatPath(words[2],path,"//",true)
  329.                     if(files == path)then
  330.                         files = formatPath(words[2],path,"",true)
  331.                         write("Are you sure y/n? ")
  332.                         if(read()=="y")then
  333.                             fs.delete(files)
  334.                         end
  335.                     else
  336.                         write("Are you sure y/n? ")
  337.                         if(read()=="y")then
  338.                             fs.delete(files)
  339.                         end
  340.                     end
  341.                 else
  342.                     helpprint("rm <file>","Deletes <file>")
  343.                 end
  344.             elseif(words[1] == "mkdir")then
  345.                 if(length == 2)then
  346.                     fs.makeDir(formatPath(words[2],path,"/",false))
  347.                 else
  348.                     helpprint("mkdir <path>","Creates a directory")
  349.                 end
  350.             elseif(words[1] == "skin")then
  351.                 if(length == 4)then
  352.                     if(words[2] == "download")then
  353.                         shell.run("pastebin get "..words[3].." "..formatPath(words[4],path,"",false))
  354.                     end
  355.                 elseif(length == 3)then
  356.                     if(words[2] == "save")then
  357.                         saveSkin(words[3])
  358.                     elseif(words[2] == "load")then
  359.                         loadSkin(words[3])
  360.                     elseif(words[2] == "setdefault")then
  361.                         defaultskin=fs.open("/CLI/Settings/skin","w")
  362.                         defaultskin.write(formatPath(words[3],path,"",true))
  363.                         defaultskin.close()
  364.                     elseif(words[2] == "resetdefault")then
  365.                         defaultskin=fs.open("/CLI/Settings/skin","w")
  366.                         defaultskin.write("/CLI/Skins/default")
  367.                         defaultskin.close()
  368.                     end
  369.                 elseif(length == 2)then
  370.                     if(words[2] == "edit")then
  371.                         term.clear()
  372.                         term.setCursorPos(1,1)
  373.                         local editing = true
  374.                         while editing do
  375.                             term.setBackgroundColor(s.bgColor)
  376.                             term.clear()
  377.                             term.setCursorPos(1,1)
  378.                             term.setTextColor(s.textColor)
  379.                             print("Skin preview:")
  380.                             local gsubbed = string.gsub (string.gsub (string.gsub (s.text.cmdFormat, "*u", user),"*c",computerName),"*p",pathv)
  381.                             cprint.cprint(gsubbed.."help")
  382.                             term.setTextColor(s.textColor)
  383.                             helpprint("Light text", "Dark text")
  384.                             term.setTextColor(s.textColor)
  385.                             errorprint("File not found")
  386.                             term.setTextColor(s.textColor)
  387.                             term.setTextColor(s.lsFolder)
  388.                             write("Folder ")
  389.                             term.setTextColor(s.lsFile)
  390.                             print("File")
  391.                             term.setTextColor(s.textColor)
  392.                             local w, h = term.getSize()
  393.                             print("Commands:")
  394.                             helpprint("edit text","Edit the main text colors for CLI")
  395.                             helpprint("edit ls","Edit the colors for the ls command")
  396.                             helpprint("edit error","Complex: Change how error messages display")
  397.                             helpprint("edit shell","Complex: Edit your command prompt (~/> command)")
  398.                             helpprint("exit","Get me out of here")
  399.                             term.setCursorPos(1,h-2)
  400.                             term.clearLine()
  401.                             term.setCursorPos(1,h-1)
  402.                             term.clearLine()
  403.                             term.setCursorPos(1,h)
  404.                             write("> ")
  405.                             local cmd = read()
  406.                             if(cmd == "exit")then
  407.                                 editing = false
  408.                             elseif(cmd == "edit text")then
  409.                                 if(true)then -- Edit text
  410.                                 term.setCursorPos(1,h-2)
  411.                                 cprint.cwrite("&0Colors: &11&22&33&44&55&66&77&88&99&00&aa&bb&cc&dd&ee&ff")
  412.                                 term.setCursorPos(1,h)
  413.                                 term.setTextColor(s.textColor)
  414.                                 write("Normal text color > ")
  415.                                 local subcmd = read()
  416.                                 if(subcmd == "")then
  417.                                 else
  418.                                     s.textColor = cprint.cCode(subcmd)
  419.                                 end
  420.                                 term.setCursorPos(1,h-2)
  421.                                 cprint.cwrite("&0Colors: &11&22&33&44&55&66&77&88&99&00&aa&bb&cc&dd&ee&ff")
  422.                                 term.setCursorPos(1,h)
  423.                                 term.setTextColor(s.textColorLight)
  424.                                 write("Light text color > ")
  425.                                 local subcmd = read()
  426.                                 if(subcmd == "")then
  427.                                 else
  428.                                     s.textColorLight = cprint.cCode(subcmd)
  429.                                 end
  430.                                 term.setCursorPos(1,h-2)
  431.                                 cprint.cwrite("&0Colors: +11+22+33+44+55+66+77+88+99+00+aa+bb+cc+dd+ee+ff")
  432.                                 term.setCursorPos(1,h)
  433.                                 term.setTextColor(s.textColorLight)
  434.                                 write("Background color > ")
  435.                                 local subcmd = read()
  436.                                 if(subcmd == "")then
  437.                                 else
  438.                                     s.bgColor = cprint.cCode(subcmd)
  439.                                 end
  440.                                 end
  441.                             elseif(cmd == "edit error")then
  442.                                 term.setCursorPos(1,h-4)
  443.                                 print("Use & then a color code for a color (EX: &2)")
  444.                                 term.setCursorPos(1,h-3)
  445.                                 cprint.cwrite("*s will be replaced with the error message")
  446.                                 term.setCursorPos(1,h-2)
  447.                                 cprint.cwrite("&0Colors: &11&22&33&44&55&66&77&88&99&00&aa&bb&cc&dd&ee&ff")
  448.                                 term.setCursorPos(1,h-1)
  449.                                 term.setTextColor(s.textColor)
  450.                                 write("Example: &bError! *s")
  451.                                 term.setCursorPos(1,h)
  452.                                 term.setTextColor(s.textColor)
  453.                                 write("Error text > ")
  454.                                 local subcmd = read()
  455.                                 if(subcmd == "")then
  456.                                 else
  457.                                     s.text.errorFormat = subcmd
  458.                                 end
  459.                             elseif(cmd == "edit shell")then
  460.                                 term.setCursorPos(1,h-4)
  461.                                 print("Use & then a color code for a color (EX: &2)")
  462.                                 term.setCursorPos(1,h-3)
  463.                                 cprint.cwrite("*u for your user *c for your computername *p for your current directory ")
  464.                                 term.setCursorPos(1,h-2)
  465.                                 cprint.cwrite("&0Colors: &11&22&33&44&55&66&77&88&99&00&aa&bb&cc&dd&ee&ff")
  466.                                 term.setCursorPos(1,h-1)
  467.                                 term.setTextColor(s.textColor)
  468.                                 write("Example: &1*p &3>")
  469.                                 term.setCursorPos(1,h)
  470.                                 term.setTextColor(s.textColor)
  471.                                 write("Prompt text > ")
  472.                                 local subcmd = read()
  473.                                 if(subcmd == "")then
  474.                                 else
  475.                                     s.text.cmdFormat = subcmd
  476.                                 end
  477.                             elseif(cmd == "edit ls")then
  478.                                 term.setCursorPos(1,h-2)
  479.                                 cprint.cwrite("&0Colors: &11&22&33&44&55&66&77&88&99&00&aa&bb&cc&dd&ee&ff")
  480.                                 term.setCursorPos(1,h)
  481.                                 term.setTextColor(s.lsFolder)
  482.                                 write("Folder text color > ")
  483.                                 local subcmd = read()
  484.                                 if(subcmd == "")then
  485.                                 else
  486.                                     s.lsFolder = cprint.cCode(subcmd)
  487.                                 end
  488.                                 term.setCursorPos(1,h-2)
  489.                                 cprint.cwrite("&0Colors: &11&22&33&44&55&66&77&88&99&00&aa&bb&cc&dd&ee&ff")
  490.                                 term.setCursorPos(1,h)
  491.                                 term.setTextColor(s.lsFile)
  492.                                 write("File text color > ")
  493.                                 local subcmd = read()
  494.                                 if(subcmd == "")then
  495.                                 else
  496.                                     s.lsFile = cprint.cCode(subcmd)
  497.                                 end
  498.                             end
  499.                         end
  500.                     end
  501.                 else
  502.                     helpprint("skin save <filename>", "Saves the current skin to <filename>")
  503.                     helpprint("skin load <filename>", "Loads the skin at <filename>")
  504.                     helpprint("skin setdefault <filename>", "Automaticly loads the skin at <filename> on startup")
  505.                     helpprint("skin edit", "Edit your current skin")
  506.                     helpprint("skin download <code> <name>", "Download a skin off pastebin")
  507.                 end
  508.             else
  509.                 helpprint("help", "List commands")
  510.             end
  511.         end
  512.     end
  513. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement