GNOOR1S

Todo List For Computer Craft Advance Computers

Mar 23rd, 2022 (edited)
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.35 KB | None | 0 0
  1. --shopping list created
  2. -- by gunnar jessee/gnoor1s
  3.  
  4. --[[
  5.     Create a list of tasks
  6.     Being able to add tasks to a list
  7.     Being able to remove a task from the list
  8.     Needs a logging api
  9.  
  10.     tasks will be stored in an array. each task will be able to
  11.         have a remove/pop task from the list
  12.     tasks will have an idea to them
  13.  
  14. ]]
  15.  
  16. tasks = {}
  17. appRunning = true
  18.  
  19. function split(pString, pPattern)
  20.     local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  21.     local fpat = "(.-)" .. pPattern
  22.     local last_end = 1
  23.     local s, e, cap = pString:find(fpat, 1)
  24.     while s do
  25.            if s ~= 1 or cap ~= "" then
  26.           table.insert(Table,cap)
  27.            end
  28.            last_end = e+1
  29.            s, e, cap = pString:find(fpat, last_end)
  30.     end
  31.     if last_end <= #pString then
  32.            cap = pString:sub(last_end)
  33.            table.insert(Table, cap)
  34.     end
  35.     return Table
  36.  end
  37.  
  38. function save_data( arr )
  39.     local dataFile = fs.open('tasks', 'w')
  40.  
  41.     for i, k in pairs(arr) do
  42.         dataFile.write( arr[i]['task'] .. ',' )
  43.     end
  44.  
  45.     dataFile.flush()
  46.     dataFile.close()
  47. end
  48.  
  49. function load_data()
  50.     if fs.exists('tasks') then
  51.         local dataFile = fs.open('tasks', 'r')
  52.         local dataString = dataFile.readAll()
  53.         print(split(dataString, ','))
  54.         local splitted = split(dataString, ',')
  55.         for i, k in pairs(splitted) do
  56.             print(splitted[i])
  57.             sleep(.1)
  58.             add_task(task_info(splitted[i]), tasks)
  59.         end
  60.     end
  61. end
  62.  
  63. -- UTILITIES
  64. function clear_screen(col)
  65.     term.setBackgroundColor(col)
  66.     term.clear()
  67.     term.setCursorPos(1, 1)
  68. end -- inperfect
  69.  
  70. -- set y position and startX position based on character count of text
  71. function center_text(text, y_pos)
  72.     char_count = string.len( text )
  73.     screen_width = term.getSize()
  74.     term.setCursorPos((screen_width/2) - (char_count/2), y_pos)
  75.     term.write( text .. '\n' )
  76. end
  77.  
  78. function set_y( y_pos )
  79.     term.setCursorPos(1, y_pos)
  80. end
  81.  
  82. -- Returns info about task create
  83. -- when referencing table, do as (table[i][N])
  84. -- task, id
  85. function task_info( info_text )
  86.     local info = {
  87.         ['task'] = info_text
  88.     }
  89.     return info
  90. end
  91.  
  92. function print_at_line( text, y_pos )
  93.     set_y( y_pos )
  94.     term.clearLine()
  95.     set_y( y_pos )
  96.     term.write( text )
  97. end
  98.  
  99. --task data is referred here
  100. --has to references a array called
  101. function add_task( task_data, arr )
  102.     table.insert( arr, task_data )
  103.     save_data( arr )
  104. end
  105.  
  106. function draw_list()
  107.    
  108.     screen_width, screen_height = term.getSize()
  109.     clear_screen(colors.blue)
  110.     term.setBackgroundColor(colors.blue)
  111.     center_text("Task List", 2)
  112.     for i, v in pairs(tasks) do
  113.         set_y(4 + i)
  114.         term.setBackgroundColor(colors.lightBlue)
  115.         term.clearLine()
  116.         set_y(4 + i)
  117.         tasks[i]['line'] = i + 4
  118.         print("["..i.. "]"..tasks[i]['task'])
  119.     end
  120.     term.setBackgroundColor(colors.green)
  121.     term.setCursorPos(1, screen_height)
  122.     term.write("[+]")
  123.     term.setCursorPos(screen_width-2, screen_height)
  124.     term.setBackgroundColor(colors.red)
  125.     term.write("[X]")
  126. end
  127.  
  128. function check_remove_at(y_pos)
  129.     for i, k in pairs(tasks) do
  130.         if tasks[i]['line'] == y_pos then
  131.             table.remove(tasks, i)
  132.         end
  133.     end
  134. end
  135.  
  136. -- PAGES
  137.  
  138. function render_add_task()
  139.     local page_running = true
  140.    
  141.     clear_screen(colors.blue)
  142.     center_text('Add todo', 2)
  143.     set_y(5)
  144.     print('Enter Task Name')
  145.     input = read()
  146.     add_task(task_info(input), tasks)
  147.     draw_list() -- goes to page
  148.     return
  149. end
  150.  
  151. function render_list()
  152.  
  153.     screen_width, screen_height = term.getSize()
  154.     draw_list()
  155.     while appRunning do
  156.         local event, button, x, y = os.pullEvent( "mouse_click" )
  157.  
  158.         if y >= 4 and y <= screen_height - 1 then
  159.             check_remove_at(y)
  160.             save_data( tasks )
  161.             draw_list()
  162.         end
  163.  
  164.         if y == screen_height and x < 4 then
  165.             render_add_task()
  166.         end
  167.  
  168.         if y == screen_height and x > screen_width - 4 then
  169.             clear_screen(colors.black)
  170.             set_y(1)
  171.             print("Programming has exited safely")
  172.             return
  173.         end
  174.  
  175.     end
  176.  
  177. end
  178.  
  179. -- All running code & Variables
  180. function run()
  181.     load_data()
  182.     render_list()
  183. end
  184.  
  185.  
  186. run()
Add Comment
Please, Sign In to add comment