Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- 'less' Unix command
- by EldidiStroyrr
- Prints the contents of a file, either on the hard drive, or from URL.
- Can also dump contents to another file, which makes it kinda like wget.
- pastebin get en8GA73P less
- std pb en8GA73P less
- std ld less less
- --]]
- local tArg = {...}
- local programName = fs.getName(shell.getRunningProgram())
- local oldbgcolor, oldtxtcolor = term.getBackgroundColor(), term.getTextColor()
- local displayHelp = function(verbose)
- print(programName.." [-a] [-u <url>] [-d/-D <dumped file>] <file>")
- if verbose then
- print([[
- Use UP/DOWN arrow keys or mousewheel to scroll, and press X to exit.
- Hold LCTRL to alternate scrolling up/down and left/right with mouse wheel.
- -h, -v, -help
- -a (Uses absolute path)
- -u (Downloads a URL and uses that)
- -d (Dumps to file. Overwrites)
- -D (Dumps to file. Appends)
- ]])
- else
- print("Do -v for verbose help.")
- end
- end
- if not tArg[1] then
- displayHelp(false)
- return false
- elseif tArg[1] == "-help" or tArg[1] == "-h" or tArg[1] == "-v" then
- displayHelp(true)
- return true
- end
- local renderTable = function(tab, start, ending, rightscroll, txtcolor, bgcolor)
- term.setCursorPos(1,1)
- local buffer = ""
- local scr_x, scr_y = term.getSize()
- term.setTextColor(txtcolor)
- term.setBackgroundColor(bgcolor)
- term.clear()
- local bezelSize = #tostring(#tab)
- for a = start,ending do
- if tab[a] then
- term.setTextColor(colors.black)
- term.setBackgroundColor(colors.lightGray)
- local bezel = tostring(a)
- local bezel = bezel..string.rep(" ",bezelSize-#bezel)
- write(bezel)
- term.setTextColor(txtcolor)
- term.setBackgroundColor(bgcolor)
- write( string.sub(buffer..tab[a],rightscroll+1,rightscroll+scr_x-bezelSize) )
- if a ~= ending then
- write("\n")
- end
- else
- term.setBackgroundColor(colors.lightGray)
- term.clearLine()
- end
- end
- end
- local tableFind = function(tab, str)
- for a = 1, #tab do
- if tab[a] == str then
- return true, a
- end
- end
- return false
- end
- local tableStringFind = function(tab, str)
- local output = {}
- for a = 1, #tab do
- local finder = {string.find(tab[a],str)}
- if finder ~= {} then
- table.insert(finder,a)
- table.insert(output,finder)
- end
- end
- return output
- end
- local function displayCredits()
- local credits = {
- "LESS unix command",
- "programmed by",
- "EldidiStroyrr",
- "",
- "Coding started at",
- "January 1st, 2016",
- "",
- "For normal CC1.7x computers, you're",
- "good to go!",
- }
- term.setBackgroundColor(colors.gray)
- if term.isColor() then term.setTextColor(colors.yellow) else term.setTextColor(colors.white) end
- term.clear()
- local scr_x, scr_y = term.getSize()
- for a = 1, #credits+12 do
- local msg = credits[a/2]
- if a % 2 == 0 and msg then
- term.setCursorPos((scr_x/2)-(#msg/2),scr_y)
- write(string.upper(msg))
- end
- sleep(0.4)
- term.scroll(1)
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- return "butts"
- end
- local getTableFromFile = function(filename)
- if not filename then
- return false
- end
- if not fs.exists(filename) then
- return false
- end
- local file = fs.open(filename,"r")
- local line = ""
- local output = {}
- while line do
- line = file.readLine()
- table.insert(output,line)
- end
- file.close()
- return output
- end
- local switches = {}
- for a = 1, #tArg do
- if string.sub(tArg[a],1,1) == "-" then
- table.insert(switches,string.sub(tArg[a],2,2))
- end
- end
- local lessfile
- local tArgFileNo = 1
- local dumpFile = nil
- if tableFind(tArg,"Eldeedoo") and fs.exists("Eldeedoo") then
- displayCredits()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.red)
- term.clear()
- term.setCursorPos(1,1)
- for a = 1, 4096 do
- write("H")
- write("A")
- if a % 32 == 0 then sleep(0) end
- end
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- print("What a bad dream!")
- if term.isColor() then term.setTextColor(colors.yellow) end
- write("> ")
- term.setTextColor(colors.white)
- sleep(0.1)
- term.setCursorBlink(true)
- os.pullEventRaw("key")
- if term.isColor() then term.setTextColor(colors.red) end
- term.setCursorPos(1,2)
- print("AND YOU'RE NEVER WAKING UP!!")
- term.setTextColor(colors.white)
- sleep(0)
- return "I like Undertale too much..."
- end
- if tableFind(tArg,"-d") or tableFind(tArg,"-D") then
- local _, dumpPos
- if tableFind(tArg,"-d") then
- _, dumpPos = tableFind(tArg,"-d")
- dumpPos = dumpPos + 1
- else
- _, dumpPos = tableFind(tArg,"-D")
- dumpPos = dumpPos + 1
- end
- dumpFile = tArg[dumpPos]
- if fs.isReadOnly(dumpFile) then
- error("That path is read only.")
- end
- tArgFileNo = tArgFileNo + 2
- if tableFind(tArg,"-a") then
- lessfile = tArg[tArgFileNo]
- else
- lessfile = fs.combine(shell.dir(),tArg[tArgFileNo])
- end
- end
- if tableFind(tArg,"-u") then
- local _, urlPos = tableFind(tArg,"-u")
- urlPos = urlPos + 1
- local url = tArg[urlPos]
- tArgFileNo = tArgFileNo + 2
- write("Connecting...")
- local internetFile = http.get(url)
- if not internetFile then
- print("FAILED!")
- end
- internetFile = internetFile.readAll()
- if #internetFile > fs.getFreeSpace("/") then --1 byte for every character, plus one
- error("You don't have enough space!")
- end
- local file = fs.open(".temp","w")
- file.write(internetFile)
- file.close()
- lessfile = ".temp"
- else
- lessfile = tArg[tArgFileNo]
- end
- if not fs.exists(lessfile) then
- error("'"..lessfile.."' does not exist!")
- elseif fs.isDir(lessfile) then
- printError("That.")
- sleep(0.3)
- printError("Is a directory.")
- return false
- end
- if dumpFile then
- local file
- if tableFind(tArg,"-d") then
- file = fs.open(dumpFile,"w")
- print("Dumped to '"..dumpFile.."'")
- else
- file = fs.open(dumpFile,"a")
- print("Appended to '"..dumpFile.."'")
- end
- local file2 = fs.open(lessfile,"r")
- file.write(file2.readAll())
- file.close()
- file2.close()
- return true
- end
- local lessfilecontents = getTableFromFile(lessfile)
- local longestStringLen = 0
- for a = 1, #lessfilecontents do
- if #lessfilecontents[a] > longestStringLen then
- longestStringLen = #lessfilecontents[a]
- end
- end
- local scr_x, scr_y = term.getSize()
- local scrollX, scrollY = 0, 0
- local event, var1, var2, var3
- local function draw()
- renderTable(lessfilecontents,scrollY+1,scrollY+scr_y,scrollX,colors.white,colors.gray)
- end
- draw()
- local allInput = function()
- local alternateScroll = false
- local scr_x, scr_y = term.getSize()
- local lineHistory = {}
- while true do
- repeat
- event, var1, var2, var3 = os.pullEvent()
- until event == "key" or event == "char" or event == "mouse_scroll" or event == "key_up"
- oldScrollX, oldScrollY = scrollX, scrollY
- if event == "key" or event == "char" then
- if var1 == 38 then --'l' to go to line
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clearLine()
- write("Goto line: ")
- sleep(0)
- local lineno = tonumber(read(nil,lineHistory))
- if lineno then
- term.setCursorPos(1,1)
- if lineno < 1 then
- write("Huh? Line "..tostring(lineno).."??")
- sleep(1.2)
- elseif lineno > #lessfilecontents then
- write("Too high a number! (lines="..#lessfilecontents..")")
- sleep(1.2)
- else
- table.insert(lineHistory)
- scrollY = lineno-1 or 1
- end
- end
- draw()
- end
- if var1 == 200 or var1 == 21 then --up arrow or 'y'
- if scrollY > 0 then
- scrollY = scrollY - 1
- end
- elseif var1 == 208 or var1 == 18 then --down arrow or 'e'
- if scrollY < #lessfilecontents-1 then
- scrollY = scrollY + 1
- end
- elseif var1 == 21 then --'u'
- if scrollY-math.floor(scr_y/2) > 0 then
- scrollY = scrollY - math.floor(scr_y/2)
- end
- elseif var1 == 18 then --'d'
- if scrollY+math.floor(scr_y/2) < #lessfilecontents-1 then
- scrollY = scrollY + math.floor(scr_y/2)
- end
- elseif var1 == 203 then --left arrow
- if scrollX > 0 then
- scrollX = scrollX - 1
- end
- elseif var1 == 205 then --right arrow
- if scrollX < longestStringLen then
- scrollX = scrollX + 1
- end
- end
- if var1 == 45 or var1 == 16 then --'X' or 'Q' to quit
- sleep(0)
- break
- end
- if var1 == 29 then --left ctrl
- alternateScroll = true
- end
- if var1 == 199 or var1 == "g" then --home
- scrollY = 0
- end
- if var1 == 207 or var1 == "G" then --end
- scrollY = #lessfilecontents-scr_y
- end
- if var1 == 201 or var1 == "b" then --page up
- scrollY = scrollY - scr_y
- if scrollY < 0 then scrollY = 0 end
- end
- if var1 == 209 or var1 == "f" then --page down
- scrollY = scrollY + scr_y
- if scrollY > #lessfilecontents-1 then scrollY = #lessfilecontents-1 end
- end
- elseif event == "mouse_scroll" then
- if not alternateScroll then
- if var1 == -1 then
- if scrollY > 0 then
- scrollY = scrollY - 1
- end
- elseif var1 == 1 then
- if scrollY < #lessfilecontents-1 then
- scrollY = scrollY + 1
- end
- end
- else
- if var1 == -1 then
- if scrollX > 0 then
- scrollX = scrollX - 1
- end
- elseif var1 == 1 then
- if scrollX < longestStringLen then
- scrollX = scrollX + 1
- end
- end
- end
- elseif event == "key_up" then
- if var1 == 29 then --left ctrl
- alternateScroll = false
- end
- end
- if scrollX ~= oldScrollX or scrollY ~= oldScrollY then --Yay flicker reduction
- draw()
- end
- end
- end
- allInput()
- term.setBackgroundColor(oldbgcolor)
- term.setTextColor(oldtxtcolor)
- term.clear()
- term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement