-
Notifications
You must be signed in to change notification settings - Fork 4
/
it.rb
271 lines (226 loc) · 5.74 KB
/
it.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
require 'yaml'
require 'mutter'
require 'fileutils'
module It
Path = {root: ".it", db: ".it/database.yml"}
def self.init dir = Dir.pwd
unless init?
FileUtils.mkdir File.join(dir, Path[:root])
FileUtils.touch File.join(dir, Path[:db])
end
end
def self.init! dir = Dir.pwd
FileUtils.rm_rf File.join(dir, Path[:root])
init dir
end
def self.init? dir = root
File.exist? File.join(dir, Path[:root]) if dir
end
def self.run *args
cmd = Command.new(*args)
cmd[:silent] = true
cmd.run
end
def self.version
File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).strip
end
def self.root
path = Dir.pwd.split('/').reject {|d| d.empty?}
(path.size + 1).times do
if It.init? (sub = File.join('/', *path))
return sub
end
path.pop
end
return nil
end
class Command
Commands = [
:init, :add, :list, :tag,
:done, :did, :log, :status,
:remove, :float, :sink,
:ls, :rm
]
Initializers = [:init, :add]
Special = {"!" => :done, "?" => :status}
def initialize *all
cmd, param, args, options = all
@command = Special[cmd] || cmd.to_sym
@args = [args || []].flatten
@param = param =~ /^\d+$/ ? param.to_i : param
@options = options || {}
@db = It.init?? Database.new(File.join(It.root, Path[:db])) : Database.new
@mut = Mutter.new(blue: '#', underline: "''", cyan: '@@', green: '!!', yellow: '^').clear(:default)
end
def run
if Commands.include? @command
if It.init? or Initializers.include? @command
if !@param or @command == :add or @param = @db.find(@param)
if send(@command, *[@param, *@args].compact.flatten)
save
else
err "error running #@command"
end
else
err "task wasn't found"
end
else
err "'it' is not initialized here, please run `it init`"
end
else
err "#{@command} is not a valid command."
end
end
def []= key, val
@options[key] = val
end
def [] key
@options[key]
end
def init
unless It.init
err "'it' has already been initialized here"
else
true
end
end
def status
out "in the water (#{@db.select {|e| e.new? }.size})"
self.list 5
out "recently fished (#{@db.select {|e| e.completed? }.size})"
@db.select {|e| e[:status] == :completed}.
sort_by {|e| e[:completed_at]}[0..3].reverse.each do |e|
out "- !!#{e[:title]}!!"
end
out
true
end
#
# List current tasks
#
def list count = 10, index = -1
out
@db.select {|e| e.new? }[0..count].each do |e|
out "#[#{index += 1}]# ''#{e[:title]}'' @@#{e[:tags].join(' ')}@@" unless e[:status] == :removed
end.tap do |list|
out " !!nothing left to do!!" if list.size.zero?
end
out
end
alias :ls list
#
# Show task history
#
def log
@db.map do |entity|
Entity::Status.map do |status|
{ title: entity[:title],
action: status,
time: entity[:"#{status}_at"]
} if entity[:"#{status}_at"]
end.compact
end.flatten.sort_by {|e| e[:time]}.reverse.each do |entry|
out "##{entry[:time]}# ^#{entry[:action]}^ ''#{entry[:title]}''"
end
end
def out obj = ""
if obj.is_a? Hash
puts "#{obj[:title]} : #{obj[:status]}"
else
@mut.say obj.to_s
end unless @options[:silent]
end
def err str
@options[:silent] ? abort : abort(str)
end
def add entry, *tags
It.init
e = Entity.new(title: entry, tags: tags)
@db << e
end
def tag entry, tags
entry[:tags] << tags
end
def did entry = 0
entry.status = :completed
entry[:completed_by] = ENV['USER']
end
alias :done did
alias :fish did
def save
File.open(File.join(It.root, Path[:db]), 'w') {|f| f.write @db.to_yaml}
end
#
# Mark task as :removed (doesn't show up anywhere)
#
def remove entry
entry.status = :removed
end
alias :rm remove
alias :kill remove
end
class Database
include Enumerable
def initialize path = nil
if @path = path
self.load path
else
@data = []
end
end
def find key
if key.is_a? String
@data.find {|e| e[:title].include? key}
elsif key.is_a? Fixnum
@data.select {|e| e[:status] == :created}[key]
else
raise ArgumentError, "key must be a String or Fixnum, but is #{key.class}"
end
end
alias :[] find
def load path = @path || Path[:db]
@data = if db = YAML.load_file(path)
db.map {|e| Entity.new(e)}
else
[]
end
self
end
def each &blk
@data.each &blk
end
def << entry
@data << (entry.is_a?(Entity) ? entry : Entity.new(entry))
end
def to_yaml *args, &blk
@data.to_yaml *args, &blk
end
end
class Entity < Hash
Status = [:created, :completed, :removed]
def initialize data = {}
self.replace status: :created,
created_at: Time.now,
owner: ENV['USER'],
tags: []
merge!(data)
end
def new?
self[:status] == :created
end
def status= st
self[:status] = st
self[:"#{st}_at"] = Time.now
end
#
# Handle things like `self.removed?`
#
def method_missing meth, *args, &blk
if meth.to_s.end_with?('?') && Status.include?(s = meth.to_s.chop.to_sym)
self[:status] == s
else
super
end
end
end
end