· 2 years ago · Jun 20, 2023, 09:40 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--speaker
68local function speaker()
69 local spk = peripheral.find("speaker")
70
71 if spk and playNote then
72 spk.playNote(instrument,1,10)
73 spk.playNote(instrument,1,12)
74 playNote = false
75 end
76end
77
78--functions that will need to run in parallel
79 -- Calls the termQuestion function and inserts its returned value into the itmes table, updateScreen gets set to true
80 local function listInput()
81 while true do
82 local itemToAdd = termQuestion()
83 table.insert(items,itemToAdd)
84 updateScreen = true
85 sleep(1)
86 end
87 end
88
89 local function updateMonitor() --this function updates the monitor when there are any changes made
90 while true do
91 if clearScreen then
92
93 clearScreen = false
94 updateScreen = true
95
96 winclear()
97 printListHeader()
98
99 end
100
101 if updateScreen then
102
103 updateScreen = false
104
105 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.
106 centerWrite(tdlWindow,k ..". " ..v, 2+k) -- writes to the monitor
107 end
108 end
109
110 local listFile = fs.open(listDirectory,"w") -- opens the list file in write mode
111 listFile.write(textutils.serialise(items)) --
112 listFile.close()
113
114 sleep(0.5)
115 end
116 end
117
118 local function removeListItemWithTouch()
119 local timer_id
120 while true do
121
122 local event = {os.pullEvent()} -- creates a table of the event that just occured. when pull event is called, it will wait for an event.
123
124 if event[1] == "monitor_touch" and toDelete > 0 and event[4] == toDelete + 2 then
125
126 toDelete = 0 --sets the item to delete to 0
127 clearScreen = true
128
129 playNote = true
130
131 table.remove(items,toDelete) --removes the item selected
132 os.cancelTimer(timer_id)
133
134 elseif event[1] == "monitor_touch" and toDelete <= 0 and event[4] > 2 then
135
136 local touchYPos = event[4]
137 local tblIndex = touchYPos - 2 --this componsates for the header
138
139 if items[tblIndex] == nil then break end
140
141 local txt,foregroundColour,backgroundColour = tdlWindow.getLine(touchYPos)
142
143 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”.
144
145 tdlWindow.setCursorPos(1,touchYPos) --sets the curosr to the y level touched
146 tdlWindow.blit(txt,redFG,backgroundColour) --writes to the screen using custom text and background colours
147
148 toDelete = tblIndex
149 timer_id = os.startTimer(3)
150
151 elseif event[1] == "timer" and event[2] == timer_id then
152
153 local touchYPos = toDelete + 2
154 local txt,foregroundColour,backgroundColour = tdlWindow.getLine(touchYPos)
155 local whiteFG = string.rep("0",#txt)
156
157 tdlWindow.setCursorPos(1,touchYPos)
158 tdlWindow.blit(txt,whiteFG,backgroundColour)
159 toDelete = 0
160 end
161
162 end
163 end
164
165
166parallel.waitForAll(listInput,updateMonitor,removeListItemWithTouch,speaker)