-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtodo_list.rb
112 lines (100 loc) · 2.45 KB
/
todo_list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require './storage'
require './exporter'
class TodoList
FILENAME = 'todo_list.csv'
def initialize
@storage = Storage.new(FILENAME)
@todos = @storage.load
end
def print_todos
@todos.each do |todo|
if todo.completed?
puts "[X] #{todo.title}"
else
puts "[ ] #{todo.title}"
end
end
end
def add(title)
todo = Todo.new(title, false)
@todos.push(todo)
puts "'#{todo.title}' was added."
end
def remove(title)
todo = @todos.find { |todo| todo.title == title }
@todos.delete(todo)
puts "'#{todo.title}' was removed."
end
def mark(title)
todo = @todos.find { |todo| todo.title == title }
if todo
todo.completed = true
puts "'#{todo.title}' marked as completed."
else
puts "Todo with title '#{title}' could not be found."
end
end
def unmark(title)
todo = @todos.find { |todo| todo.title == title }
if todo
todo.completed = false
puts "'#{todo.title}' marked as uncompleted."
else
puts "Todo with title '#{title}' could not be found."
end
end
def save
@storage.save(@todos)
end
def export
exporter = Exporter.new(@todos)
supported_formats = exporter.supported_formats.join(", ")
format = readline("Please enter an export format (#{supported_formats}):")
exporter.export(format)
end
end
todo_list = TodoList.new
def readline(prompt = ">")
print "#{prompt} "
gets.chomp
end
puts 'Welcome to the todo_list app!'
puts 'Type help for listing all commands.'
loop do
command = readline
case command
when 'help'
puts 'help: Print this help'
puts 'list: List all todos'
puts 'add: Add a new todo'
puts 'remove: Remove an existing todo'
puts 'mark: Mark a todo as completed'
puts 'unmark: Unmark a completed todo'
puts 'export: Export the current todos'
puts 'exit: Exit the application and saves all todos'
when 'list'
todo_list.print_todos
when 'add'
puts 'Enter the title of the todo:'
title = readline
todo_list.add(title)
when 'remove'
puts 'Enter the title of the todo:'
title = readline
todo_list.remove(title)
when 'mark'
puts 'Enter the title of the todo:'
title = readline
todo_list.mark(title)
when 'unmark'
puts 'Enter the title of the todo:'
title = readline
todo_list.unmark(title)
when 'export'
puts '[TODOLIST] export...'
todo_list.export
when 'exit'
todo_list.save
exit
end
end