-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminitask.rb
More file actions
executable file
·227 lines (176 loc) · 4.25 KB
/
Copy pathminitask.rb
File metadata and controls
executable file
·227 lines (176 loc) · 4.25 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
#! /usr/bin/env ruby
require 'optparse'
require 'json'
require 'open-uri'
class MiniTaskOptions
def self.parse(args)
options = Hash.new
options[:output] = :text
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: minitask [options]"
opts.separator ""
opts.separator "Commands:"
opts.on("-l", "--list", "List all tasks") do
options[:command] = :list
end
opts.on("-a", "--add TASK", "Adds a new task") do |task|
options[:command] = :add
options[:task] = task
end
opts.on("-t", "--take", "Takes the next task") do
options[:command] = :take
end
opts.on("-r", "--random", "Takes a random task") do
options[:command] = :random
end
opts.on("-d", "--defer [TASK]", "Defers the task for 1 month") do |task|
options[:command] = :defer
options[:task] = task
end
opts.on("-c", "--complete [TASK]", "Completes the task") do |task|
options[:command] = :complete
options[:task] = task
end
opts.on("--update", "Updates this app from master") do
opts[:command] = :update
end
opts.separator ""
opts.separator "Modiers:"
opts.on("-e", "--effort AMOUNT", [:small, :medium, :large], "Scope command to amount of effort") do |effort|
options[:effort] = :"#{effort}"
end
opts.on("--json", "JSON output") do
options[:output] = :json
end
opts.separator ""
opts.separator "Common options:"
opts.on_tail("--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts "MiniTask version #{MiniTask::VERSION} #{File.absolute_path(__FILE__)}"
exit
end
end
opt_parser.parse!(args)
options
end
end
class MiniTask
VERSION = "1.0"
attr_reader :options
def initialize options
@options = options
end
def self.run(options)
mini_task = new(options)
mini_task.send(options[:command])
end
def list
with_data do
print_tasks tasks
end
end
def add
with_data do
tasks << {'title' => options[:task]}
print_success "Task '#{options[:task]}' added"
end
end
def take
with_data do
task = tasks.first
print_tasks task
end
end
def random
with_data do
task = tasks.sample
print_tasks task
end
end
def complete task = nil
with_data do
end
end
def update
# curl code from github
# do opposite of data saving
update_code = ""
update_download = open("")
while (line = update_download.gets)
break if line.chomp == "__END__"
update_code << line
end
read_file = File.new(absolute_path, "r")
file_contents = ""
while (line = read_file.gets) do
file_contents << line
break if line.chomp == "__END__"
end
read_file.close
file_contents << @data.to_json
write_file = File.open(absolute_path, "w")
write_file.puts file_contents
write_file.close
end
private
def config
@data['config']
end
def tasks
@data['tasks']
end
def with_data &block
load_data
block.call
save_data
end
def load_data
@data = JSON.parse(DATA.read)
rescue
@data = {
'config' => {},
'tasks' => []
}
end
def save_data
read_file = File.new(absolute_path, "r")
file_contents = ""
while (line = read_file.gets) do
file_contents << line
break if line.chomp == "__END__"
end
read_file.close
file_contents << @data.to_json
write_file = File.open(absolute_path, "w")
write_file.puts file_contents
write_file.close
end
def absolute_path
File.absolute_path(__FILE__)
end
def print_text text
@output ||= Array.new
end
def print_success text
case options[:output]
when :text
puts "✓ #{text}"
when :json
puts JSON.dump({success: text})
end
end
def print_tasks tasks
case options[:output]
when :text
puts tasks.map{|t| t.title}.join("\r\n")
when :json
puts JSON.dump(tasks)
end
end
end
MiniTask.run(MiniTaskOptions.parse(ARGV))
__END__
{"config":{},"tasks":[{"title":"Do the dishes"},{"title":"record the TV program"}]}