This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ETHANATOR360's profile picture

cat, a basic terminal program

Started by ETHANATOR360, 25 May 2013 - 10:24 AM
ETHANATOR360 #1
Posted 25 May 2013 - 12:24 PM
http://pastebin.com/RN3STjsz
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
FuuuAInfiniteLoop(F.A.I.L) #2
Posted 25 May 2013 - 03:30 PM
One with scrolling:


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)
Nina #3
Posted 28 May 2013 - 02:49 PM
Now 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
ElvishJerricco #4
Posted 28 May 2013 - 09:08 PM
EDIT: Nevermind. But this might be useful.
ETHANATOR360 #5
Posted 30 May 2013 - 11:28 AM
Now 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
ah i forgot my basic file errors
jesusthekiller #6
Posted 01 June 2013 - 05:47 PM
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()
Tazkazz #7
Posted 15 June 2013 - 07: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)
end
can be simplified to this:

if evt == "mouse_scroll" then scroll = scroll + s end
:)/>
jesusthekiller #8
Posted 15 June 2013 - 12:34 PM
You shall do no necromancy…
Engineer #9
Posted 16 June 2013 - 05:50 PM
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()
However it does work, I'd rather use this:

local f = assert( fs.open( args[1], 'r' ), 'error while etc.')
jesusthekiller #10
Posted 17 June 2013 - 05:12 AM
Same thing
theoriginalbit #11
Posted 17 June 2013 - 05:14 AM
Same thing
but Engineer's method is a little easier to read.
jesusthekiller #12
Posted 17 June 2013 - 05:37 AM
I wouldn't say so :P/>
Geforce Fan #13
Posted 17 June 2013 - 12:11 PM
Yeah, I can actually read killer's better.
jesusthekiller #14
Posted 17 June 2013 - 12:15 PM
jesus's not killer's :P/>
ElvishJerricco #15
Posted 17 June 2013 - 07:56 PM
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()
However it does work, I'd rather use this:

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.
jesusthekiller #16
Posted 18 June 2013 - 03:02 AM
Write your own fs.open then
Engineer #17
Posted 18 June 2013 - 03:05 AM
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()
However it does work, I'd rather use this:

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.
With a little bit of creativity, this should give you an 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")

That would error on itself already..
joostlek #18
Posted 18 June 2013 - 07:16 AM
Does it work in an OS, and do u allow it into my OS?
jesusthekiller #19
Posted 18 June 2013 - 08:20 AM
If you can't make so simple program, don't make OS…
Zudo #20
Posted 26 July 2013 - 05:20 PM
Sorry admins about this necro.

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)
NeptunasLT #21
Posted 28 July 2013 - 03:56 AM
You just can make this:
- Put cat to etc: /.cat
- then write to next file:

shell.clearAlias("cat")
shell.setAlias("cat", "cat file")

What do this done?
- Maked cat command for shell you will can run it anywhere!

Can i have +R3P, bro