-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.rb
executable file
·259 lines (216 loc) · 5.15 KB
/
stack.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
#!/usr/bin/env ruby
require 'rubygems'
require 'pp'
require 'json'
require 'uuid'
def help
puts <<HELP
Usage: stack command [arguments]
Commands:
help Show this help message.
list List items in the stack.
push <description> Add a new item to the stack.
pop Remove the most-recent item from the stack.
drop <index> Remove the specified item from the stack.
touch <index> Move the specified item to the top of the stack.
tag <index> <tag> Add the specified tag to the specified item.
tag <index> -<tag> Remove the specified tag from the specified item.
archive <index> Move the item to the archive. See 'list' behavior below.
archive list List the archived items, ordered by date last touched.
archive clear Clear the archive."
HELP
end
def datadir
dropbox = "#{ENV['HOME']}/Dropbox"
winDropbox = "#{ENV['HOME']}/My Dropbox"
if File.directory?(dropbox)
data = "#{dropbox}/.stack-data"
elsif File.directory?(winDropbox)
data = "#{winDropbox}/.stack-data"
end
if !File.directory?(data)
puts "Creating stack data dir at #{data}"
Dir.mkdir(data)
end
return data
end
class Record
attr_accessor :id
attr_accessor :description
attr_accessor :last_activity
attr_accessor :tags
attr_accessor :archived
def initialize(description)
@id = UUID.new.generate
@description = description
@last_activity = now
@tags = []
@archived = false
end
def self.load(file)
text = File.read(file)
json = JSON.parse(text)
record = Record.new(json['description'])
record.id = json['id']
record.last_activity = json['last_activity']
record.tags = json['tags']
record.archived = json['archived'] || false
return record
end
def to_json
json = {}
self.instance_variables.each do |var|
key = var[1..-1]
json[key] = self.instance_variable_get(var.to_sym)
end
JSON.unparse(json)
end
def store
File.open(path, "w") do |file|
file.write(to_json)
end
end
def delete
File.delete(path)
end
def path
File.join(DATADIR, @id)
end
def touch
@last_activity = now
end
def add_tag(tag)
if [email protected]?(tag)
@tags.push(tag)
end
end
def remove_tag(tag)
@tags.delete(tag)
end
def has_tag?(tag)
@tags != nil && @tags.include?(tag)
end
def self.load_all_records(dir)
records = []
Dir.foreach(dir) do |file|
# Bafflingly, on Windows, char literal comparison fails.
if file[0] != '.' && file[0] != 46 # 46: decimal value of '.'
records.push(Record.load(File.join(dir, file)))
end
end
records.sort { |a, b| a.last_activity <=> b.last_activity }
end
def self.load_records(dir, archived)
records = load_all_records(dir)
return records.select { |r| r.archived == archived }
end
end
def list(archived)
records = Record.load_records(DATADIR, archived)
reversed = records.reverse
reversed.each do |r|
if !archived
str = "#{records.index(r) + 1}. "
else
str = ""
end
str += r.description
if r.tags != nil && r.tags.length > 0
str += " [#{r.tags.join(',')}]"
end
puts str
end
end
def push(description)
record = Record.new(description)
record.store
end
def pop
records = Record.load_records(DATADIR, false)
record = records.last
record.delete
end
# drops the 1-based index from the list
def drop(index)
record = item(index)
record.delete
end
# moves the 1-based index to the top of the list
def touch(index)
record = item(index)
record.touch
record.store
end
def item(index)
records = Record.load_records(DATADIR, false)
records[index - 1]
end
def now()
Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
end
def assert_arg_count(args, count)
if args.count != count
throw "expected exactly #{count} argument(s); got #{args.count}"
end
end
def run(args)
if args.count == 0
help
return -1
end
command = args.shift.to_sym
case command
when :help
help
return 0
when :list
assert_arg_count(args, 0)
list(false)
return 0
when :push
if args.count == 0
throw "expected an item description"
end
push(args.join(' '))
return 0
when :pop
assert_arg_count(args, 0)
pop
return 0
when :drop
assert_arg_count(args, 1)
drop(args[0].to_i)
return 0
when :touch
assert_arg_count(args, 1)
touch(args[0].to_i)
return 0
when :tag
assert_arg_count(args, 2)
record = item(args[0].to_i)
if args[1].start_with?("-")
record.remove_tag(args[1][1..-1])
else
record.add_tag(args[1])
end
record.store
return 0
when :archive
assert_arg_count(args, 1)
if args[0].to_sym == :list
list(true)
elsif args[0].to_sym == :clear
Record.load_records(DATADIR, true).each { |r| r.delete }
else
record = item(args[0].to_i)
record.archived = true
record.store
end
return 0
end
help
return -1
end
DATADIR = datadir()
statusCode = run(ARGV.clone)
exit(statusCode)