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
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)
end
Not tested but should work for advanced computers and normal ones(with w and s instead of scrolling)
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
ah i forgot my basic file errorsNow that we're at posting alternatives, here's one that behaves more like cat in UNIX/Linux: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
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()
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)
end
can be simplified to this:
if evt == "mouse_scroll" then scroll = scroll + s end
:)/>However it does work, I'd rather use this:Shrank your code (with file error handling):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.')
but Engineer's method is a little easier to read.Same thing
However it does work, I'd rather use this:Shrank your code (with file error handling):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.')
With a little bit of creativity, this should give you an error:However it does work, I'd rather use this:Shrank your code (with file error handling):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 ) end
However, this is kind of pointless. If you want a real error, you just do:
local file = fs.open("hi","r")
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)