jille_Jr icon

CC: cat.lua

jille_Jr | PRO | 11/15/20 03:54:31 PM UTC | 0 ⭐ | 323 👁️ | Never ⏰ | []
Lua |

970 B

|

None

|

0 👍

/

0 👎

local args = {...}
 
if #args == 0 then
    error("Usage: cat <file> [file2] [file3...]")
end
 
local function ends_with(str, ending)
    return ending == "" or str:sub(-#ending) == ending
end
 
local w,h = term.getSize()
 
local function nextLine()
    local x,y = term.getCursorPos()
    if y == h then
        term.scroll(1)
    else
        y = y + 1
    end
    term.setCursorPos(1, y)
end
 
for _,path in ipairs(args) do
    if not fs.exists(path) then
        error("cat: file not found: " .. path, 0)
    end
    local file = fs.open(path, "r")
    local content = file.readAll()
    write(content)
    
    if not ends_with(content, "\n") then
        local bg = term.getBackgroundColor()
        local fg = term.getTextColor()
        term.setBackgroundColor(colors.white)
        term.setTextColor(colors.black)
        term.write("%")
        term.setBackgroundColor(bg)
        term.setTextColor(fg)
    end
    nextLine()
    file.close()
end

Comments