cat, a basic terminal program
#1
Posted 25 May 2013 - 11:24 AM
this simple program prints the contents of a file to the screen
i wrote this because its a fun command to play with(on linux) and its not default for comutercraft like alot of other commands
#2
Posted 25 May 2013 - 02:30 PM
local screen = {} local scroll = 1 function clear() term.clear() for scroll, #screen do print(screen[i]) end end args = {...} file = fs.open(args[1], "r") local lines = file.readLine() for lines in file.readLine do table.insert(screen,lines) end print("Press a key to show") os.pullEvent("key") while true do clear() evt, s = os.pullEventRaw() if evt=="char" and s=="w" then scroll = scroll-1 end if evt=="char" and s=="s" then scroll = scroll+1 end if evt=="mouse_scroll" and s > 0 then scroll = scroll+math.abs(s) end if evt=="mouse_scroll" and s < 0 then scroll = scroll-math.abs(s) endNot tested but should work for advanced computers and normal ones(with w and s instead of scrolling)
#3
Posted 28 May 2013 - 01:49 PM
local args = { ... } local file = nil if #args > 1 then print("Usage: cat [file]") return end if #args == 1 then local f = args[1] if not fs.exists(f) then print("cat: " .. f .. ": No such file") return end if fs.isDir(f) then print("cat: " .. f .. ": Is a directory") end file = fs.open(f, 'r') end local line = nil while true do if file ~= nil then line = file.readLine() else line = read() end if line ~= nil then print(line) else return end end
#4
Posted 28 May 2013 - 08:08 PM
#5
Posted 30 May 2013 - 10:28 AM
Nina, on 28 May 2013 - 01:49 PM, said:
local args = { ... } local file = nil if #args > 1 then print("Usage: cat [file]") return end if #args == 1 then local f = args[1] if not fs.exists(f) then print("cat: " .. f .. ": No such file") return end if fs.isDir(f) then print("cat: " .. f .. ": Is a directory") end file = fs.open(f, 'r') end local line = nil while true do if file ~= nil then line = file.readLine() else line = read() end if line ~= nil then print(line) else return end end
#6
Posted 01 June 2013 - 04:47 PM
args = {...} if not args[1] then print("Usage: cat [file]") return end local f = fs.open(args[1], "r") or error("Error while opening file!") print(f.readAll()) f.close()
#7
Posted 15 June 2013 - 06:38 AM
if evt=="mouse_scroll" and s > 0 then scroll = scroll+math.abs(s) end if evt=="mouse_scroll" and s < 0 then scroll = scroll-math.abs(s) endcan be simplified to this:
if evt == "mouse_scroll" then scroll = scroll + s end
#8
Posted 15 June 2013 - 11:34 AM
#9
Posted 16 June 2013 - 04:50 PM
jesusthekiller, on 01 June 2013 - 04:47 PM, said:
args = {...} if not args[1] then print("Usage: cat [file]") return end local f = fs.open(args[1], "r") or error("Error while opening file!") print(f.readAll()) f.close()
local f = assert( fs.open( args[1], 'r' ), 'error while etc.')
#10
Posted 17 June 2013 - 04:12 AM
#12
Posted 17 June 2013 - 04:37 AM
#13
Posted 17 June 2013 - 11:11 AM
#14
Posted 17 June 2013 - 11:15 AM
#15
Posted 17 June 2013 - 06:56 PM
Engineer, on 16 June 2013 - 04:50 PM, said:
jesusthekiller, on 01 June 2013 - 04:47 PM, said:
args = {...} if not args[1] then print("Usage: cat [file]") return end local f = fs.open(args[1], "r") or error("Error while opening file!") print(f.readAll()) f.close()
local f = assert( fs.open( args[1], 'r' ), 'error while etc.')
I wish fs.open returned [handle], [reason for error] so that you could simply do assert(fs.open(path, mode)), and it would automatically error with the proper error message that gives complete detail of the problem instead of some generic error.
#16
Posted 18 June 2013 - 02:02 AM
#17
Posted 18 June 2013 - 02:05 AM
ElvishJerricco, on 17 June 2013 - 06:56 PM, said:
Engineer, on 16 June 2013 - 04:50 PM, said:
jesusthekiller, on 01 June 2013 - 04:47 PM, said:
args = {...} if not args[1] then print("Usage: cat [file]") return end local f = fs.open(args[1], "r") or error("Error while opening file!") print(f.readAll()) f.close()
local f = assert( fs.open( args[1], 'r' ), 'error while etc.')
I wish fs.open returned [handle], [reason for error] so that you could simply do assert(fs.open(path, mode)), and it would automatically error with the proper error message that gives complete detail of the problem instead of some generic error.
local ok, err = pcall( function() local file = fs.open("hi", "r") file.close() end) if not ok then error(err, 2 ) endHowever, this is kind of pointless. If you want a real error, you just do:
local file = fs.open("hi","r")
That would error on itself already..
#18
Posted 18 June 2013 - 06:16 AM
#19
Posted 18 June 2013 - 07:20 AM
#20
Posted 26 July 2013 - 04:20 PM
Does anyone know that cat is designed to concatenate multiple files, like this?
args = {...} str = "" for i = 1, #args do h = fs.open(args[i], "r") or error("Failed to open file", 0) str = str .. h.readAll() h.close() end print(str)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users