Advertisement
mrkarp

ToDo

Feb 11th, 2021
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.57 KB | None | 0 0
  1. -- Get args
  2. -- local arg1, arg2, arg3 = ...
  3.  
  4. -- local w, h = term.getSize()
  5. local todoList = {}
  6.  
  7. -- monitorSideInput = arg1
  8. -- if monitor == nil then
  9. --    print("Monitor null")
  10. -- end
  11.  
  12. -- Monitor Settings
  13. monitor = peripheral.wrap('right')
  14. local maxX,maxY = monitor.getSize()
  15. monitor.setTextScale(1.5)
  16. monitor.setBackgroundColor(colors.black)
  17. term.setCursorBlink(false)
  18.  
  19. function GetUserInput()
  20.     print('Todo: 1. Add, 2. Update, 3. Remove, 4. List, 5. Clear, 6. Exit')
  21.     local choice = read()
  22.     if choice == "1" then
  23.         print('Todo: ')
  24.         local newItem = read()
  25.         AddTodoItem(newItem)
  26.     elseif choice == "2" then
  27.         print('To update: (index) ')
  28.         local itemToUpdateKey = read()
  29.         print('To update: (text) ')
  30.         local itemToUpdateString = read()
  31.         UpdateTodoItem(itemToUpdateKey, itemToUpdateString)
  32.     elseif choice == "3" then
  33.         print('To remove: (index) ')
  34.         local itemToRemoveKey = read()
  35.         RemoveTodoItem(itemToRemoveKey)
  36.     elseif choice == "4" then
  37.         print('List: ', dump(todoList))
  38.     elseif choice == "5" then
  39.         print('Clear list? (y/n) ')
  40.         local clear = read()
  41.         if clear == "y" then
  42.             print('Clear db? (y/n) ')
  43.             local cleardb = read()
  44.             if cleardb == "y" then
  45.                 ClearDb()
  46.             end
  47.             todoList = {}
  48.         end
  49.     elseif choice == "6" then
  50.         error("Quitting...")
  51.     end
  52. end
  53.  
  54. -- Main Function
  55. function main()
  56.     while true do
  57.         -- monitor.clear()
  58.         -- monitor.setCursorPos(1,1)
  59.         -- monitor.setTextColor(colors.white)
  60.         GetUserInput()
  61.         ShowOnMonitor(todoList)
  62.         sleep(1)
  63.     end
  64. end
  65.  
  66. -- Table
  67. function AddTodoItem(string)
  68.     table.insert(todoList, string)
  69.     UpdateDb()
  70. end
  71.  
  72. function UpdateTodoItem(key, string)
  73.     todoList[tonumber(key)] = string
  74.     UpdateDb()
  75. end
  76.  
  77. function RemoveTodoItem(key)
  78.     table.remove(todoList, tonumber(key))
  79.     UpdateDb()
  80. end
  81.  
  82. -- Helpers
  83. function dump(o)
  84.    if type(o) == 'table' then
  85.       local s = '{ '
  86.       for k,v in pairs(o) do
  87.          if type(k) ~= 'number' then k = '"'..k..'"' end
  88.          s = s .. '['..k..'] = ' .. dump(v) .. ','
  89.       end
  90.       return s .. '} '
  91.    else
  92.       return tostring(o)
  93.    end
  94. end
  95.  
  96. -- Monitor
  97.  
  98. function ShowOnMonitor(o)
  99.     monitor.clear()
  100.     monitor.setCursorPos(maxX / 2 - 3, 1)
  101.     monitor.setTextColor(colors.yellow)
  102.    
  103.     monitor.write('Todo List')
  104.    if type(o) == 'table' then
  105.     monitor.setTextColor(colors.blue)
  106.       local s = '{ '
  107.       for k,v in pairs(o) do
  108.           monitor.setCursorPos(1, k + 1)
  109.           monitor.write(k..') '..dump(v))
  110.       end
  111.    end
  112. end
  113.  
  114. -- Local Db
  115. function UpdateDb()
  116.     local file = fs.open('.todolist','w') -- the period in front of the name to make it hidden, just a habit i guess
  117.     file.write(textutils.serialize(todoList))
  118.     file.close()
  119. end
  120.  
  121. function ReadDb()
  122.     local file = fs.open('.todolist','r')
  123.     local database = textutils.unserialize(file.readAll())
  124.     file.close()
  125. end
  126.  
  127. function InitDb()
  128.     local file = fs.open('.todolist','r')
  129.     if file ~= nil then
  130.         local readData = textutils.unserialize(file.readAll())
  131.         if readData ~= nil then
  132.             todoList = readData
  133.         else
  134.             todoList = {}
  135.         end
  136.         file.close()
  137.     else
  138.         todoList = {}
  139.         local file = fs.open('.todolist','w')
  140.         file.write(textutils.serialize(todoList))
  141.         file.close()
  142.     end
  143. end
  144.  
  145. function ClearDb()
  146.     local file = fs.open('.todolist','w')
  147.     local clear = {}
  148.     file.write(clear)
  149.     file.close()
  150. end
  151.  
  152. -- Init
  153. InitDb()
  154. main()
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement