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