Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --shopping list created
- -- by gunnar jessee/gnoor1s
- --[[
- Create a list of tasks
- Being able to add tasks to a list
- Being able to remove a task from the list
- Needs a logging api
- tasks will be stored in an array. each task will be able to
- have a remove/pop task from the list
- tasks will have an idea to them
- ]]
- tasks = {}
- appRunning = true
- function split(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- function save_data( arr )
- local dataFile = fs.open('tasks', 'w')
- for i, k in pairs(arr) do
- dataFile.write( arr[i]['task'] .. ',' )
- end
- dataFile.flush()
- dataFile.close()
- end
- function load_data()
- if fs.exists('tasks') then
- local dataFile = fs.open('tasks', 'r')
- local dataString = dataFile.readAll()
- print(split(dataString, ','))
- local splitted = split(dataString, ',')
- for i, k in pairs(splitted) do
- print(splitted[i])
- sleep(.1)
- add_task(task_info(splitted[i]), tasks)
- end
- end
- end
- -- UTILITIES
- function clear_screen(col)
- term.setBackgroundColor(col)
- term.clear()
- term.setCursorPos(1, 1)
- end -- inperfect
- -- set y position and startX position based on character count of text
- function center_text(text, y_pos)
- char_count = string.len( text )
- screen_width = term.getSize()
- term.setCursorPos((screen_width/2) - (char_count/2), y_pos)
- term.write( text .. '\n' )
- end
- function set_y( y_pos )
- term.setCursorPos(1, y_pos)
- end
- -- Returns info about task create
- -- when referencing table, do as (table[i][N])
- -- task, id
- function task_info( info_text )
- local info = {
- ['task'] = info_text
- }
- return info
- end
- function print_at_line( text, y_pos )
- set_y( y_pos )
- term.clearLine()
- set_y( y_pos )
- term.write( text )
- end
- --task data is referred here
- --has to references a array called
- function add_task( task_data, arr )
- table.insert( arr, task_data )
- save_data( arr )
- end
- function draw_list()
- screen_width, screen_height = term.getSize()
- clear_screen(colors.blue)
- term.setBackgroundColor(colors.blue)
- center_text("Task List", 2)
- for i, v in pairs(tasks) do
- set_y(4 + i)
- term.setBackgroundColor(colors.lightBlue)
- term.clearLine()
- set_y(4 + i)
- tasks[i]['line'] = i + 4
- print("["..i.. "]"..tasks[i]['task'])
- end
- term.setBackgroundColor(colors.green)
- term.setCursorPos(1, screen_height)
- term.write("[+]")
- term.setCursorPos(screen_width-2, screen_height)
- term.setBackgroundColor(colors.red)
- term.write("[X]")
- end
- function check_remove_at(y_pos)
- for i, k in pairs(tasks) do
- if tasks[i]['line'] == y_pos then
- table.remove(tasks, i)
- end
- end
- end
- -- PAGES
- function render_add_task()
- local page_running = true
- clear_screen(colors.blue)
- center_text('Add todo', 2)
- set_y(5)
- print('Enter Task Name')
- input = read()
- add_task(task_info(input), tasks)
- draw_list() -- goes to page
- return
- end
- function render_list()
- screen_width, screen_height = term.getSize()
- draw_list()
- while appRunning do
- local event, button, x, y = os.pullEvent( "mouse_click" )
- if y >= 4 and y <= screen_height - 1 then
- check_remove_at(y)
- save_data( tasks )
- draw_list()
- end
- if y == screen_height and x < 4 then
- render_add_task()
- end
- if y == screen_height and x > screen_width - 4 then
- clear_screen(colors.black)
- set_y(1)
- print("Programming has exited safely")
- return
- end
- end
- end
- -- All running code & Variables
- function run()
- load_data()
- render_list()
- end
- run()
Add Comment
Please, Sign In to add comment