Kingdaro

notes

Apr 2nd, 2013
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.17 KB | None | 0 0
  1. local args = {...}
  2. local opt = table.remove(args, 1)
  3. local notes = {}
  4. local path = shell.resolve '.notes-data'
  5.  
  6. local function loadNotes()
  7.   local file = fs.open(path, 'r')
  8.   if file then
  9.     for line in file.readLine do
  10.       table.insert(notes, line)
  11.     end  
  12.     file.close()
  13.   end
  14. end
  15.  
  16. local function saveNotes()
  17.   local file = fs.open(path, 'w')  
  18.   assert(file, 'Notes path is read-only')
  19.   for i=1, #notes do
  20.     file.writeLine(notes[i])
  21.   end
  22.   file.close()
  23. end
  24.  
  25. if opt == 'add' then
  26.   loadNotes()
  27.   local newNote = table.concat(args, ' ')
  28.   table.insert(notes, newNote)
  29.   saveNotes()
  30.   print('Added "'..newNote..'"')
  31. elseif opt == 'remove'
  32. or opt == 'delete' then
  33.   loadNotes()
  34.   local noteID = tonumber(args[1])
  35.   assert(noteID, 'invalid note ID')
  36.   local removed = table.remove(notes, noteID)
  37.   saveNotes()
  38.   print('Removed note '..noteID..': "'..removed..'"')
  39. elseif opt == 'list' then
  40.   loadNotes()
  41.   for i=1, #notes do
  42.     local space = string.rep(' ', 4 - #tostring(i))
  43.     print(i..space..notes[i])
  44.   end
  45. else
  46.   print [[
  47.   Usage:
  48.     notes add <note>
  49.     notes remove <note #>
  50.     notes delete <note #>
  51.     notes list
  52.   ]]
  53. end
Advertisement
Add Comment
Please, Sign In to add comment