Jump to content




cat, a basic terminal program


20 replies to this topic

#1 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 25 May 2013 - 11:24 AM

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

#2 FuuuAInfiniteLoop(F.A.I.L)

  • Banned
  • 435 posts
  • LocationThe left part of this post

Posted 25 May 2013 - 02: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)

#3 Nina

  • Members
  • 19 posts

Posted 28 May 2013 - 01: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


#4 ElvishJerricco

  • Members
  • 803 posts

Posted 28 May 2013 - 08:08 PM

EDIT: Nevermind. But this might be useful.

#5 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 30 May 2013 - 10:28 AM

View PostNina, on 28 May 2013 - 01:49 PM, said:

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

#6 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 01 June 2013 - 04: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()


#7 Tazkazz

  • Members
  • 4 posts

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)
end
can be simplified to this:
if evt == "mouse_scroll" then scroll = scroll + s end
:)

#8 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 15 June 2013 - 11:34 AM

You shall do no necromancy...

#9 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 16 June 2013 - 04:50 PM

View Postjesusthekiller, on 01 June 2013 - 04:47 PM, said:

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.')


#10 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 17 June 2013 - 04:12 AM

Same thing

#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 June 2013 - 04:14 AM

View Postjesusthekiller, on 17 June 2013 - 04:12 AM, said:

Same thing
but Engineer's method is a little easier to read.

#12 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 17 June 2013 - 04:37 AM

I wouldn't say so :P

#13 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 17 June 2013 - 11:11 AM

Yeah, I can actually read killer's better.

#14 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 17 June 2013 - 11:15 AM

jesus's not killer's :P

#15 ElvishJerricco

  • Members
  • 803 posts

Posted 17 June 2013 - 06:56 PM

View PostEngineer, on 16 June 2013 - 04:50 PM, said:

View Postjesusthekiller, on 01 June 2013 - 04:47 PM, said:

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.

#16 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 18 June 2013 - 02:02 AM

Write your own fs.open then

#17 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 18 June 2013 - 02:05 AM

View PostElvishJerricco, on 17 June 2013 - 06:56 PM, said:

View PostEngineer, on 16 June 2013 - 04:50 PM, said:

View Postjesusthekiller, on 01 June 2013 - 04:47 PM, said:

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..

#18 joostlek

  • Members
  • 14 posts

Posted 18 June 2013 - 06:16 AM

Does it work in an OS, and do u allow it into my OS?

#19 jesusthekiller

  • Banned
  • 562 posts
  • LocationWrocław, Poland

Posted 18 June 2013 - 07:20 AM

If you can't make so simple program, don't make OS...

#20 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 26 July 2013 - 04: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)






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users