Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Get args
- -- local arg1, arg2, arg3 = ...
- -- local w, h = term.getSize()
- local todoList = {}
- -- monitorSideInput = arg1
- -- if monitor == nil then
- -- print("Monitor null")
- -- end
- -- Monitor Settings
- monitor = peripheral.wrap('right')
- local maxX,maxY = monitor.getSize()
- monitor.setTextScale(1.5)
- monitor.setBackgroundColor(colors.black)
- term.setCursorBlink(false)
- function GetUserInput()
- print('Todo: 1. Add, 2. Update, 3. Remove, 4. List, 5. Clear, 6. Exit')
- local choice = read()
- if choice == "1" then
- print('Todo: ')
- local newItem = read()
- AddTodoItem(newItem)
- elseif choice == "2" then
- print('To update: (index) ')
- local itemToUpdateKey = read()
- print('To update: (text) ')
- local itemToUpdateString = read()
- UpdateTodoItem(itemToUpdateKey, itemToUpdateString)
- elseif choice == "3" then
- print('To remove: (index) ')
- local itemToRemoveKey = read()
- RemoveTodoItem(itemToRemoveKey)
- elseif choice == "4" then
- print('List: ', dump(todoList))
- elseif choice == "5" then
- print('Clear list? (y/n) ')
- local clear = read()
- if clear == "y" then
- print('Clear db? (y/n) ')
- local cleardb = read()
- if cleardb == "y" then
- ClearDb()
- end
- todoList = {}
- end
- elseif choice == "6" then
- error("Quitting...")
- end
- end
- -- Main Function
- function main()
- while true do
- -- monitor.clear()
- -- monitor.setCursorPos(1,1)
- -- monitor.setTextColor(colors.white)
- GetUserInput()
- ShowOnMonitor(todoList)
- sleep(1)
- end
- end
- -- Table
- function AddTodoItem(string)
- table.insert(todoList, string)
- UpdateDb()
- end
- function UpdateTodoItem(key, string)
- todoList[tonumber(key)] = string
- UpdateDb()
- end
- function RemoveTodoItem(key)
- table.remove(todoList, tonumber(key))
- UpdateDb()
- end
- -- Helpers
- function dump(o)
- if type(o) == 'table' then
- local s = '{ '
- for k,v in pairs(o) do
- if type(k) ~= 'number' then k = '"'..k..'"' end
- s = s .. '['..k..'] = ' .. dump(v) .. ','
- end
- return s .. '} '
- else
- return tostring(o)
- end
- end
- -- Monitor
- function ShowOnMonitor(o)
- monitor.clear()
- monitor.setCursorPos(maxX / 2 - 3, 1)
- monitor.setTextColor(colors.yellow)
- monitor.write('Todo List')
- if type(o) == 'table' then
- monitor.setTextColor(colors.blue)
- local s = '{ '
- for k,v in pairs(o) do
- monitor.setCursorPos(1, k + 1)
- monitor.write(k..') '..dump(v))
- end
- end
- end
- -- Local Db
- function UpdateDb()
- local file = fs.open('.todolist','w') -- the period in front of the name to make it hidden, just a habit i guess
- file.write(textutils.serialize(todoList))
- file.close()
- end
- function ReadDb()
- local file = fs.open('.todolist','r')
- local database = textutils.unserialize(file.readAll())
- file.close()
- end
- function InitDb()
- local file = fs.open('.todolist','r')
- if file ~= nil then
- local readData = textutils.unserialize(file.readAll())
- if readData ~= nil then
- todoList = readData
- else
- todoList = {}
- end
- file.close()
- else
- todoList = {}
- local file = fs.open('.todolist','w')
- file.write(textutils.serialize(todoList))
- file.close()
- end
- end
- function ClearDb()
- local file = fs.open('.todolist','w')
- local clear = {}
- file.write(clear)
- file.close()
- end
- -- Init
- InitDb()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement