View difference between Paste ID: AJkQGB3B and rpL5EmBv
SHOW: | | - or go back to the newest paste.
1
function load(name)
2
	local file = fs.open(name, "r");
3-
	if file ~= nil then return {} end
3+
	if file ~= nil then
4
		local data = file.readAll();
5
		file.close();
6
		return textutils.unserialize(data);
7
	end
8
end
9
10
hasMon = false;
11
list = load("todoList");
12
i = 1;
13
len = 0;
14
if peripheral.isPresent("top") then
15
	hasMon = true;
16
	mon = peripheral.wrap("top");
17
	print("Monitor was found!");
18
	mon.clear();
19
	mon.setCursorPos(1,1);
20
end
21
22
function save(table, name)
23
	local file = fs.open(name, "w");
24
	file.write(textutils.serialize(table));
25
	file.close();
26
end
27
28
29
30
function nl()
31
	if hasMon == true then
32
	local x,y = mon.getCursorPos();
33
	mon.setCursorPos(1, y+1);
34
	end
35
end
36
37
function printm(str)
38
	if type(str) ~= "string" then return end
39
	mon.write(str);
40
	nl();
41
end
42
43
function drawScreen()
44
	if hasMon == true then
45
		mon.setCursorPos(1,1);
46
		mon.clear();
47
		for j = 1,len do
48
			printm(list[j]);
49
		end
50
	end
51
end
52
53
function add()
54
	print("Enter your task: ");
55
	listAns = read();
56
	print("\n");
57
	if type(listAns) == "string" then
58
		list[i] = " - " .. listAns;
59
		printm(list[i]);
60
		i = i +1;
61
		len = len +1;
62
	end
63
	save(list, "todoList");
64
end
65
66
function removeFromList(n)
67
	table.remove(list, n);
68
end
69
70
function remove()
71
	print("Which task would you like to remove?");
72
	removeAns = read();
73
	print("\n");
74
	if type(removeAns) == "string" then
75
		removeAns = tonumber(removeAns);
76
		if removeAns <= len then
77
			removeFromList(removeAns);
78
		end 
79
	end
80
	drawScreen();
81
	save(list, "todoList");
82
end
83
84
function start()
85
	while true do
86
		print("What would you like to do?");
87
		print(" - Type \"add\" to add an item");
88
		print(" - Type \"remove\" to add an item");
89
		print(" - Type \"quit\" to save and quit");
90
		ans = read();
91
		if type(ans) == "string" then
92
			if ans == "add" then add() end
93
			if ans == "remove" then remove() end
94
			if ans == "quit" then 
95
				printm("Goodbye!");
96
				os.shutdown();
97
			end
98
		end
99
	end
100
end
101
102
start();