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