-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.rb
executable file
·197 lines (176 loc) · 4.27 KB
/
todo.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env ruby
require 'yaml'
# Represents a single todo item. An Item contains just a text and
# a context.
#
class Item
attr_accessor :context, :text
# Creates a new Item instance in-memory.
#
# value - The text of the Todo. Context is extracted if exists.
#
# Returns the unpersisted Item instance.
def initialize(value)
@context = value.scan(/@[A-Z0-9.+-]+/i).last || '@next'
@text = value.gsub(context, '').strip
end
# Overide: Quick and simple way to print Items
#
# Returns String for this Item
def to_s
"#{@text}: #{@context}"
end
end
# The Todo contains many Items. They exist as buckets in which to categorize
# individual Items. The relationship is maintained in a simple array.
#
class Todo
# Creates a new Todo instance in-memory.
#
# Returns the persisted Todo instance.
def initialize(options = {})
@options, @items = options, []
bootstrap
load_items
end
# The main todos in the user's home directory
FILE = File.expand_path('.todos')
# Allow to items to be accessible from the outside
attr_accessor :items
# Creates a new todo
#
# Example:
# @todo.add('lorem epsim etc @work')
#
# Returns the add todo Item
def add(todo)
@items << Item.new(todo)
save
@items.last
end
# Removes the todo
#
# Example:
# @todo.delete(1)
#
# Returns the deleted todo Item
def delete(index)
todo = @items.delete_at(index.to_i-1)
save
todo
end
# Marks a todo as done
#
# Example:
# @todo.done(1)
#
# Returns the done todo Item
def done(index)
item = @items[index.to_i-1]
item.context = '@done'
save
item
end
# Prints all the active todos in a nice neat format
#
# Examples:
# @todo.list @work
#
# Returns nothing
def list
longest = @items.map(&:text).max_by(&:length) || 0
@items.each_with_index do |todo, index|
if @items.length > 9
printf "%2s: %-#{longest.size+5}s %s\n", index+1, todo.text, todo.context
else
printf "%s: %-#{longest.size+5}s %s\n", index+1, todo.text, todo.context
end
end
end
# Moves a todo up or down in priority
#
# Example:
# @todo.bump(2, +1)
#
def bump(index, position = 1)
@items.insert(position-1, @items.delete_at(index.to_i-1))
save
@items[position.to_i-1]
end
# Accessor for the todo list file
#
# Returns String file path
def file
@file ||= File.exist?(FILE) ? FILE : "#{ENV['HOME']}/.todos"
end
# Formats the current set of todos
#
# Returns a lovely hash
def to_hash
@items.group_by(&:context).inject({}) do |h,(k,v)|
h[k.to_sym] = v.map(&:text); h
end
end
# Loads the yaml todos file and creates a hash
#
# Returns the items loaded from the file
def load_items
YAML.load_file(file).each do |key, texts|
texts.each do |text|
if key.to_s == @options[:filter] || @options[:filter].nil?
@items << Item.new("#{text} #{key}") if key.to_s != '@done'
end
end
end
@items
end
# Implodes all the todo items save an empty file
#
# Returns nothing
def clear!
@items.clear
save
end
private
# Saves the current list of todos to disk
#
# Returns nothing
def save
File.open(file, "w") {|f| f.write(to_hash.to_yaml) }
end
# Creates a new todo file if none is present
#
# Returns nothing
def bootstrap
return if File.exist?(file)
save
end
end
if __FILE__ == $0
case ARGV[0]
when 'list','ls'
Todo.new(:filter => ARGV[1]).list
when 'add','a'
puts "Added: #{Todo.new.add(ARGV[1..-1].join(' '))}"
when 'delete', 'del', 'd'
puts "Deleted: #{Todo.new.delete(ARGV[1])}"
when 'done', 'do'
puts "Done: #{Todo.new.done(ARGV[1])}"
when 'edit'
system("`echo $EDITOR` #{Todo.new.file} &")
when 'clear'
puts "All #{Todo.new.clear!} todos cleared! #{Todo.new.clear!}"
when 'bump'
puts "Bump: #{Todo.new.bump(ARGV[1])}"
Todo.new.list
else
puts "\nUsage: todo [options] COMMAND\n\n"
puts "Commands:"
puts " add TODO Adds a todo"
puts " delete NUM Removes a todo"
puts " done NUM Completes a todo"
puts " list [CONTEXT] Lists all active todos"
puts " bump NUM Bumps priority of a todo"
puts " edit Opens todo file"
end
end