Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment