- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.rb
More file actions
Latest commit
executable file
·259 lines (216 loc) · 5.15 KB
/
Copy pathstack.rb
File metadata and controls
executable file
·259 lines (216 loc) · 5.15 KB
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'
defhelp
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
defdatadir
dropbox="#{ENV['HOME']}/Dropbox"
winDropbox="#{ENV['HOME']}/My Dropbox"
ifFile.directory?(dropbox)
data="#{dropbox}/.stack-data"
elsifFile.directory?(winDropbox)
data="#{winDropbox}/.stack-data"
end
if !File.directory?(data)
puts"Creating stack data dir at #{data}"
Dir.mkdir(data)
end
returndata
end
classRecord
attr_accessor:id
attr_accessor:description
attr_accessor:last_activity
attr_accessor:tags
attr_accessor:archived
definitialize(description)
@id=UUID.new.generate
@description=description
@last_activity=now
@tags=[]
@archived=false
end
defself.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
returnrecord
end
defto_json
json={}
self.instance_variables.eachdo |var|
key=var[1..-1]
json[key]=self.instance_variable_get(var.to_sym)
end
JSON.unparse(json)
end
defstore
File.open(path,"w")do |file|
file.write(to_json)
end
end
defdelete
File.delete(path)
end
defpath
File.join(DATADIR,@id)
end
deftouch
@last_activity=now
end
defadd_tag(tag)
if !@tags.include?(tag)
@tags.push(tag)
end
end
defremove_tag(tag)
@tags.delete(tag)
end
defhas_tag?(tag)
@tags != nil && @tags.include?(tag)
end
defself.load_all_records(dir)
records=[]
Dir.foreach(dir)do |file|
# Bafflingly, on Windows, char literal comparison fails.
iffile[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
defself.load_records(dir,archived)
records=load_all_records(dir)
returnrecords.select{ |r| r.archived == archived}
end
end
deflist(archived)
records=Record.load_records(DATADIR,archived)
reversed=records.reverse
reversed.eachdo |r|
if !archived
str="#{records.index(r) + 1}. "
else
str=""
end
str += r.description
ifr.tags != nil && r.tags.length > 0
str += " [#{r.tags.join(',')}]"
end
putsstr
end
end
defpush(description)
record=Record.new(description)
record.store
end
defpop
records=Record.load_records(DATADIR,false)
record=records.last
record.delete
end
# drops the 1-based index from the list
defdrop(index)
record=item(index)
record.delete
end
# moves the 1-based index to the top of the list
deftouch(index)
record=item(index)
record.touch
record.store
end
defitem(index)
records=Record.load_records(DATADIR,false)
records[index - 1]
end
defnow()
Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
end
defassert_arg_count(args,count)
ifargs.count != count
throw"expected exactly #{count} argument(s); got #{args.count}"
end
end
defrun(args)
ifargs.count == 0
help
return -1
end
command=args.shift.to_sym
casecommand
when:help
help
return0
when:list
assert_arg_count(args,0)
list(false)
return0
when:push
ifargs.count == 0
throw"expected an item description"
end
push(args.join(' '))
return0
when:pop
assert_arg_count(args,0)
pop
return0
when:drop
assert_arg_count(args,1)
drop(args[0].to_i)
return0
when:touch
assert_arg_count(args,1)
touch(args[0].to_i)
return0
when:tag
assert_arg_count(args,2)
record=item(args[0].to_i)
ifargs[1].start_with?("-")
record.remove_tag(args[1][1..-1])
else
record.add_tag(args[1])
end
record.store
return0
when:archive
assert_arg_count(args,1)
ifargs[0].to_sym == :list
list(true)
elsifargs[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
return0
end
help
return -1
end
DATADIR=datadir()
statusCode=run(ARGV.clone)
exit(statusCode)