· 2 years ago · Jun 20, 2023, 08:10 PM
1--settings variables
2local listDirectory = ""
3local userName = ""
4
5--making sure the file exists if not, create items
6if not fs.exists(listDirectory) then
7 local fR = fs.open(listDirectory,"w")
8 fR.close()
9end
10
11--program variables
12 --monitor variables
13 local m = peripheral.find("monitor")
14 local mSizeX,mSizeY = m.getSize()
15 local tdlWindow = window.create(m,1,1,mSizeX,mSizeY)
16
17 --Screen state variables
18 local clearScreen = false
19 local updateScreen = false
20 local toDelete = 0
21
22 --Gather the to do list
23 local fR = fs.open(listDirectory,"r")
24 local items = textutils.unserialise(fR.readAll()) or {}
25 fR.close()
26
27
28--functions
29local function centerWrite(term,text,customY)
30 local width, height = term.getSize()
31 local x, y = term.getCursorPos()
32 term.setCursorPos(math.ceil((width / 2) - (text:len() / 2)), customY)
33 term.write(text)
34end
35
36-- clears the terminal and sets the position to 1,1 (top left)
37local function termClear()
38 term.clear()
39 term.setCursorPos(1,1)
40end
41
42-- clears the monitor and sets the position to 1,1 (top left)
43local function winclear()
44 tdlWindow.clear()
45 tdlWindow.setCursorPos(1,1)
46end
47
48-- This function will put the header at the top of the list
49local function printListHeader()
50 centerWrite(tdlWindow,userName.."'s to do list",1)
51 centerWrite(tdlWindow,"-------------------",2)
52end
53
54-- This function clears the terminal, writes a prompt to add an item to the list, sets the cursor position to the second line, reads a line of input from the user, and returns the input as a string.
55local function termQuestion()
56 termClear()
57 term.write("Add to the list:")
58 term.setCursorPos(1,2)
59 local listItemToAdd = io.read()
60 return listItemToAdd
61end
62
63--functions that will need to run in parallel
64 -- Calls the termQuestion function and inserts its returned value into the itmes table, updateScreen gets set to true
65 local function listInput()
66 while true do
67 local itemToAdd = termQuestion()
68 table.insert(items,itemToAdd)
69 updateScreen = true
70 sleep(1)
71 end
72 end
73
74 local function updateMonitor() --this function updates the monitor when there are any changes made
75 while true do
76 if clearScreen then
77
78 clearScreen = false
79 updateScreen = true
80
81 winclear()
82 printListHeader()
83
84 end
85
86 if updateScreen then
87
88 updateScreen = false
89
90 for k,v in pairs(items) do -- This loop iterates over all key-value pairs in the table items using the pairs function. It prints the key and value in each iteration. The order of the iterations is not specified by the pairs function.
91 centerWrite(tdlWindow,k ..". " ..v, 2+k) -- writes to the monitor
92 end
93 end
94
95 local listFile = fs.open(listDirectory,"w") -- opens the list file in write mode
96 listFile.write(textutils.serialise(items)) --
97 listFile.close()
98
99 sleep(0.5)
100 end
101 end
102
103 local function removeListItemWithTouch()
104 local timer_id
105 while true do
106
107 local event = {os.pullEvent()} -- creates a table of the event that just occured. when pull event is called, it will wait for an event.
108
109 if event[1] == "monitor_touch" and toDelete > 0 and event[4] == toDelete + 2 then
110
111 toDelete = 0 --sets the item to delete to 0
112 clearScreen = true
113
114 table.remove(items,toDelete) --removes the item selected
115 os.cancelTimer(timer_id)
116
117 elseif event[1] == "monitor_touch" and toDelete <= 0 and (event[4] ~= 1 or 2) then
118
119 local touchYPos = event[4]
120 local tblIndex = touchYPos - 2 --this componsates for the header
121 local txt,foregroundColour,backgroundColour = tdlWindow.getLine(touchYPos)
122
123 local redFG = string.rep("e",#txt) -- This line creates a string that is the repetition of the letter “e” as many times as the length of the string txt. For example, if txt is “hello”, then redFG is “eeeee”.
124
125 tdlWindow.setCursorPos(1,touchYPos) --sets the curosr to the y level touched
126 tdlWindow.blit(txt,redFG,backgroundColour) --writes to the screen using custom text and background colours
127
128 toDelete = tblIndex
129 timer_id = os.startTimer(3)
130
131 elseif event[1] == "timer" and event[2] == timer_id then
132
133 local touchYPos = toDelete + 2
134 local txt,foregroundColour,backgroundColour = tdlWindow.getLine(touchYPos)
135 local whiteFG = string.rep("0",#txt)
136
137 tdlWindow.setCursorPos(1,y)
138 tdlWindow.blit(txt,whiteFG,backgroundColour)
139 end
140
141 end
142 end
143
144parallel.waitForAll(listInput,updateMonitor,removeListItemWithTouch)