Advertisement
Guest User

fileapi

a guest
Jan 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.42 KB | None | 0 0
  1. -- This is the file API
  2. -- I made this to make writing files n' stuff
  3. -- easier
  4. -- Chock500 - 22|12|12,016HE
  5.  
  6. os.loadAPI("chockos/config")
  7.  
  8. function writeFile(directory,name,contents)
  9.   -- Simply input the directory of the new file,
  10.   -- then put in the name and then what you want
  11.   -- to put inside
  12.   local file = io.open(directory.."/"..name,"w")
  13.   file:write(contents)
  14.   file:close()
  15. end
  16.  
  17.  
  18. function readFile(destination)
  19.   -- This literally takes the destination of a file
  20.   -- and returns its contents, that's it.
  21.   local file = fs.open(destination,"r")
  22.   contents = file.readAll()
  23.   file.close()
  24.   return contents
  25. end
  26.  
  27. function listFiles(directory,x,y,pre)
  28.   -- this lists files
  29.   -- it prints them beginning at x,y
  30.   -- point it at a directory
  31.   -- pre can be any string or true
  32.   -- when true, it prints a different prefix,
  33.   -- depending on whether or not it is a directory
  34.   pref = pre
  35.   y = y - 1
  36.   files = fs.list(directory)
  37.   length = table.getn(files)
  38.   listLoop = 1
  39.   while listLoop <= length do
  40.     term.setCursorPos(x,y+listLoop)
  41.     if pre == true then
  42.       if fs.isDir(directory..files[listLoop]) then
  43.         pref = "[] "
  44.         term.setTextColour(colours.lightBlue)
  45.       else
  46.         pref = ">> "
  47.         term.setTextColor(colours.blue)
  48.       end
  49.     end
  50.     print(pref..files[listLoop])
  51.     listLoop = listLoop+1
  52.   end
  53.   term.setTextColour(config.defaultTextColour())
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement