Skip to content

Commit

Permalink
fix: cat doesn't print newline on last file
Browse files Browse the repository at this point in the history
  • Loading branch information
jdugan6240 committed Apr 20, 2024
1 parent 229fe4c commit 96ac3b9
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions nature/commands/cat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ commander.register('cat', function(args, sinks)
usage: cat [file]...]]
end

local chunk_size = 2^13 -- 8K buffer size
local chunkSize = 2^13 -- 8K buffer size

for _, fName in ipairs(args) do
for i, fName in ipairs(args) do
local f = io.open(fName)
if f == nil then
exit = 1
Expand All @@ -20,11 +20,19 @@ usage: cat [file]...]]
end

while true do
local block = f:read(chunk_size)
local block = f:read(chunkSize)
if not block then break end
sinks.out:write(block)
end
sinks.out:writeln("")
-- Unless this is the last file, output a newline.
-- This helps differentiate one file from the next,
-- and prevent problems with one file starting on the same
-- line the previous file ended.
if i ~= #args then
sinks.out:writeln("")
else
sinks.out:writeln("***END***")
end
::continue::
end
io.flush()
Expand Down

0 comments on commit 96ac3b9

Please sign in to comment.