Advertisement
LDDestroier

less Unix Command (ComputerCraft)

Jan 7th, 2016
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.47 KB | None | 0 0
  1. --[[
  2.     'less' Unix command
  3.     by EldidiStroyrr
  4.  
  5.     Prints the contents of a file, either on the hard drive, or from URL.
  6.     Can also dump contents to another file, which makes it kinda like wget.
  7.  
  8.     pastebin get en8GA73P less
  9.     std pb en8GA73P less
  10.     std ld less less
  11. --]]
  12.  
  13. local tArg = {...}
  14. local programName = fs.getName(shell.getRunningProgram())
  15.  
  16. local oldbgcolor, oldtxtcolor = term.getBackgroundColor(), term.getTextColor()
  17.  
  18. local displayHelp = function(verbose)
  19.     print(programName.." [-a] [-u <url>] [-d/-D <dumped file>] <file>")
  20.     if verbose then
  21.         print([[
  22. Use UP/DOWN arrow keys or mousewheel to scroll, and press X to exit.
  23. Hold LCTRL to alternate scrolling up/down and left/right with mouse wheel.
  24. -h, -v, -help
  25. -a (Uses absolute path)
  26. -u (Downloads a URL and uses that)
  27. -d (Dumps to file. Overwrites)
  28. -D (Dumps to file. Appends)
  29.         ]])
  30.     else
  31.         print("Do -v for verbose help.")
  32.     end
  33. end
  34.  
  35. if not tArg[1] then
  36.     displayHelp(false)
  37.     return false
  38. elseif tArg[1] == "-help" or tArg[1] == "-h" or tArg[1] == "-v" then
  39.     displayHelp(true)
  40.     return true
  41. end
  42.  
  43. local renderTable = function(tab, start, ending, rightscroll, txtcolor, bgcolor)
  44.     term.setCursorPos(1,1)
  45.     local buffer = ""
  46.     local scr_x, scr_y = term.getSize()
  47.     term.setTextColor(txtcolor)
  48.     term.setBackgroundColor(bgcolor)
  49.     term.clear()
  50.     local bezelSize = #tostring(#tab)
  51.     for a = start,ending do
  52.         if tab[a] then
  53.             term.setTextColor(colors.black)
  54.             term.setBackgroundColor(colors.lightGray)
  55.             local bezel = tostring(a)
  56.             local bezel = bezel..string.rep(" ",bezelSize-#bezel)
  57.             write(bezel)
  58.             term.setTextColor(txtcolor)
  59.             term.setBackgroundColor(bgcolor)
  60.             write( string.sub(buffer..tab[a],rightscroll+1,rightscroll+scr_x-bezelSize) )
  61.             if a ~= ending then
  62.                 write("\n")
  63.             end
  64.         else
  65.             term.setBackgroundColor(colors.lightGray)
  66.             term.clearLine()
  67.         end
  68.     end
  69. end
  70.  
  71. local tableFind = function(tab, str)
  72.     for a = 1, #tab do
  73.         if tab[a] == str then
  74.             return true, a
  75.         end
  76.     end
  77.     return false
  78. end
  79.  
  80. local tableStringFind = function(tab, str)
  81.     local output = {}
  82.     for a = 1, #tab do
  83.         local finder = {string.find(tab[a],str)}
  84.         if finder ~= {} then
  85.             table.insert(finder,a)
  86.             table.insert(output,finder)
  87.         end
  88.     end
  89.     return output
  90. end
  91.  
  92. local function displayCredits()
  93.     local credits = {
  94.         "LESS unix command",
  95.         "programmed by",
  96.         "EldidiStroyrr",
  97.         "",
  98.         "Coding started at",
  99.         "January 1st, 2016",
  100.         "",
  101.         "For normal CC1.7x computers, you're",
  102.         "good to go!",
  103.     }
  104.     term.setBackgroundColor(colors.gray)
  105.     if term.isColor() then term.setTextColor(colors.yellow) else term.setTextColor(colors.white) end
  106.     term.clear()
  107.     local scr_x, scr_y = term.getSize()
  108.     for a = 1, #credits+12 do
  109.         local msg = credits[a/2]
  110.         if a % 2 == 0 and msg then
  111.             term.setCursorPos((scr_x/2)-(#msg/2),scr_y)
  112.             write(string.upper(msg))
  113.         end
  114.         sleep(0.4)
  115.         term.scroll(1)
  116.     end
  117.     term.setBackgroundColor(colors.black)
  118.     term.clear()
  119.     term.setCursorPos(1,1)
  120.     return "butts"
  121. end
  122.  
  123. local getTableFromFile = function(filename)
  124.     if not filename then
  125.         return false
  126.     end
  127.     if not fs.exists(filename) then
  128.         return false
  129.     end
  130.     local file = fs.open(filename,"r")
  131.     local line = ""
  132.     local output = {}
  133.     while line do
  134.         line = file.readLine()
  135.         table.insert(output,line)
  136.     end
  137.     file.close()
  138.     return output
  139. end
  140.  
  141. local switches = {}
  142. for a = 1, #tArg do
  143.     if string.sub(tArg[a],1,1) == "-" then
  144.         table.insert(switches,string.sub(tArg[a],2,2))
  145.     end
  146. end
  147.  
  148. local lessfile
  149. local tArgFileNo = 1
  150. local dumpFile = nil
  151.  
  152. if tableFind(tArg,"Eldeedoo") and fs.exists("Eldeedoo") then
  153.     displayCredits()
  154.     term.setBackgroundColor(colors.black)
  155.     term.setTextColor(colors.red)
  156.     term.clear()
  157.     term.setCursorPos(1,1)
  158.     for a = 1, 4096 do
  159.         write("H")
  160.         write("A")
  161.         if a % 32 == 0 then sleep(0) end
  162.     end
  163.     term.setTextColor(colors.white)
  164.     term.clear()
  165.     term.setCursorPos(1,1)
  166.     print("What a bad dream!")
  167.     if term.isColor() then term.setTextColor(colors.yellow) end
  168.     write("> ")
  169.     term.setTextColor(colors.white)
  170.     sleep(0.1)
  171.     term.setCursorBlink(true)
  172.     os.pullEventRaw("key")
  173.     if term.isColor() then term.setTextColor(colors.red) end
  174.     term.setCursorPos(1,2)
  175.     print("AND YOU'RE NEVER WAKING UP!!")
  176.     term.setTextColor(colors.white)
  177.     sleep(0)
  178.     return "I like Undertale too much..."
  179. end
  180.  
  181. if tableFind(tArg,"-d") or tableFind(tArg,"-D") then
  182.     local _, dumpPos
  183.     if tableFind(tArg,"-d") then
  184.         _, dumpPos = tableFind(tArg,"-d")
  185.         dumpPos = dumpPos + 1
  186.     else
  187.         _, dumpPos = tableFind(tArg,"-D")
  188.         dumpPos = dumpPos + 1
  189.     end
  190.     dumpFile = tArg[dumpPos]
  191.     if fs.isReadOnly(dumpFile) then
  192.         error("That path is read only.")
  193.     end
  194.     tArgFileNo = tArgFileNo + 2
  195.     if tableFind(tArg,"-a") then
  196.         lessfile = tArg[tArgFileNo]
  197.     else
  198.         lessfile = fs.combine(shell.dir(),tArg[tArgFileNo])
  199.     end
  200. end
  201. if tableFind(tArg,"-u") then
  202.     local _, urlPos = tableFind(tArg,"-u")
  203.     urlPos = urlPos + 1
  204.     local url = tArg[urlPos]
  205.     tArgFileNo = tArgFileNo + 2
  206.     write("Connecting...")
  207.     local internetFile = http.get(url)
  208.     if not internetFile then
  209.         print("FAILED!")
  210.     end
  211.     internetFile = internetFile.readAll()
  212.     if #internetFile > fs.getFreeSpace("/") then --1 byte for every character, plus one
  213.         error("You don't have enough space!")
  214.     end
  215.     local file = fs.open(".temp","w")
  216.     file.write(internetFile)
  217.     file.close()
  218.     lessfile = ".temp"
  219. else
  220.     lessfile = tArg[tArgFileNo]
  221. end
  222.  
  223. if not fs.exists(lessfile) then
  224.     error("'"..lessfile.."' does not exist!")
  225. elseif fs.isDir(lessfile) then
  226.     printError("That.")
  227.     sleep(0.3)
  228.     printError("Is a directory.")
  229.     return false
  230. end
  231.  
  232. if dumpFile then
  233.     local file
  234.     if tableFind(tArg,"-d") then
  235.         file = fs.open(dumpFile,"w")
  236.         print("Dumped to '"..dumpFile.."'")
  237.     else
  238.         file = fs.open(dumpFile,"a")
  239.         print("Appended to '"..dumpFile.."'")
  240.     end
  241.     local file2 = fs.open(lessfile,"r")
  242.     file.write(file2.readAll())
  243.     file.close()
  244.     file2.close()
  245.     return true
  246. end
  247.  
  248. local lessfilecontents = getTableFromFile(lessfile)
  249. local longestStringLen = 0
  250. for a = 1, #lessfilecontents do
  251.     if #lessfilecontents[a] > longestStringLen then
  252.         longestStringLen = #lessfilecontents[a]
  253.     end
  254. end
  255.  
  256. local scr_x, scr_y = term.getSize()
  257. local scrollX, scrollY = 0, 0
  258. local event, var1, var2, var3
  259.  
  260. local function draw()
  261.     renderTable(lessfilecontents,scrollY+1,scrollY+scr_y,scrollX,colors.white,colors.gray)
  262. end
  263.  
  264. draw()
  265.  
  266. local allInput = function()
  267.     local alternateScroll = false
  268.     local scr_x, scr_y = term.getSize()
  269.     local lineHistory = {}
  270.     while true do
  271.         repeat
  272.             event, var1, var2, var3 = os.pullEvent()
  273.         until event == "key" or event == "char" or event == "mouse_scroll" or event == "key_up"
  274.         oldScrollX, oldScrollY = scrollX, scrollY
  275.         if event == "key" or event == "char" then
  276.             if var1 == 38 then --'l' to go to line
  277.                 term.setCursorPos(1,1)
  278.                 term.setBackgroundColor(colors.black)
  279.                 term.setTextColor(colors.white)
  280.                 term.clearLine()
  281.                 write("Goto line: ")
  282.                 sleep(0)
  283.                 local lineno = tonumber(read(nil,lineHistory))
  284.                 if lineno then
  285.                     term.setCursorPos(1,1)
  286.                     if lineno < 1 then
  287.                         write("Huh? Line "..tostring(lineno).."??")
  288.                         sleep(1.2)
  289.                     elseif lineno > #lessfilecontents then
  290.                         write("Too high a number! (lines="..#lessfilecontents..")")
  291.                         sleep(1.2)
  292.                     else
  293.                         table.insert(lineHistory)
  294.                         scrollY = lineno-1 or 1
  295.                     end
  296.                 end
  297.                 draw()
  298.             end
  299.             if var1 == 200 or var1 == 21 then --up arrow or 'y'
  300.                 if scrollY > 0 then
  301.                     scrollY = scrollY - 1
  302.                 end
  303.             elseif var1 == 208 or var1 == 18 then --down arrow or 'e'
  304.                 if scrollY < #lessfilecontents-1 then
  305.                     scrollY = scrollY + 1
  306.                 end
  307.             elseif var1 == 21 then --'u'
  308.                 if scrollY-math.floor(scr_y/2) > 0 then
  309.                     scrollY = scrollY - math.floor(scr_y/2)
  310.                 end
  311.             elseif var1 == 18 then --'d'
  312.                 if scrollY+math.floor(scr_y/2) < #lessfilecontents-1 then
  313.                     scrollY = scrollY + math.floor(scr_y/2)
  314.                 end
  315.             elseif var1 == 203 then --left arrow
  316.                 if scrollX > 0 then
  317.                     scrollX = scrollX - 1
  318.                 end
  319.             elseif var1 == 205 then --right arrow
  320.                 if scrollX < longestStringLen then
  321.                     scrollX = scrollX + 1
  322.                 end
  323.             end
  324.             if var1 == 45 or var1 == 16 then --'X' or 'Q' to quit
  325.                 sleep(0)
  326.                 break
  327.             end
  328.             if var1 == 29 then --left ctrl
  329.                 alternateScroll = true
  330.             end
  331.             if var1 == 199 or var1 == "g" then --home
  332.                 scrollY = 0
  333.             end
  334.             if var1 == 207 or var1 == "G" then --end
  335.                 scrollY = #lessfilecontents-scr_y
  336.             end
  337.             if var1 == 201 or var1 == "b" then --page up
  338.                 scrollY = scrollY - scr_y
  339.                 if scrollY < 0 then scrollY = 0 end
  340.             end
  341.             if var1 == 209 or var1 == "f" then --page down
  342.                 scrollY = scrollY + scr_y
  343.                 if scrollY > #lessfilecontents-1 then scrollY = #lessfilecontents-1 end
  344.             end
  345.         elseif event == "mouse_scroll" then
  346.             if not alternateScroll then
  347.                 if var1 == -1 then
  348.                     if scrollY > 0 then
  349.                         scrollY = scrollY - 1
  350.                     end
  351.                 elseif var1 == 1 then
  352.                     if scrollY < #lessfilecontents-1 then
  353.                         scrollY = scrollY + 1
  354.                     end
  355.                 end
  356.             else
  357.                 if var1 == -1 then
  358.                     if scrollX > 0 then
  359.                         scrollX = scrollX - 1
  360.                     end
  361.                 elseif var1 == 1 then
  362.                     if scrollX < longestStringLen then
  363.                         scrollX = scrollX + 1
  364.                     end
  365.                 end
  366.             end
  367.         elseif event == "key_up" then
  368.             if var1 == 29 then --left ctrl
  369.                 alternateScroll = false
  370.             end
  371.         end
  372.         if scrollX ~= oldScrollX or scrollY ~= oldScrollY then --Yay flicker reduction
  373.             draw()
  374.         end
  375.     end
  376. end
  377.  
  378. allInput()
  379.  
  380. term.setBackgroundColor(oldbgcolor)
  381. term.setTextColor(oldtxtcolor)
  382. term.clear()
  383. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement