jille_Jr

CC: cat.lua

Nov 15th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.97 KB | None | 0 0
  1. local args = {...}
  2.  
  3. if #args == 0 then
  4.     error("Usage: cat <file> [file2] [file3...]")
  5. end
  6.  
  7. local function ends_with(str, ending)
  8.     return ending == "" or str:sub(-#ending) == ending
  9. end
  10.  
  11. local w,h = term.getSize()
  12.  
  13. local function nextLine()
  14.     local x,y = term.getCursorPos()
  15.     if y == h then
  16.         term.scroll(1)
  17.     else
  18.         y = y + 1
  19.     end
  20.     term.setCursorPos(1, y)
  21. end
  22.  
  23. for _,path in ipairs(args) do
  24.     if not fs.exists(path) then
  25.         error("cat: file not found: " .. path, 0)
  26.     end
  27.     local file = fs.open(path, "r")
  28.     local content = file.readAll()
  29.     write(content)
  30.    
  31.     if not ends_with(content, "\n") then
  32.         local bg = term.getBackgroundColor()
  33.         local fg = term.getTextColor()
  34.         term.setBackgroundColor(colors.white)
  35.         term.setTextColor(colors.black)
  36.         term.write("%")
  37.         term.setBackgroundColor(bg)
  38.         term.setTextColor(fg)
  39.     end
  40.     nextLine()
  41.     file.close()
  42. end
Add Comment
Please, Sign In to add comment