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