-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathcommands.rb
211 lines (187 loc) · 5.55 KB
/
commands.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
require 'chronic'
require 'highline'
module HCl
module Commands
class Error < StandardError; end
def self.deprecate command, explanation
private_command = :"_deprecated_#{command}"
alias_method private_command, command
define_method command do |*args|
$stderr.puts "warning: #{command} command is deprecated, #{explanation}"
send(private_command, *args)
end
end
# Display a sanitized view of your auth credentials.
def config
http.config_hash.merge(password:'***').map {|k,v| "#{k}: #{v}" }.join("\n")
end
# Show the network status of the Harvest service.
def status
result = Faraday.new("https://kccljmymlslr.statuspage.io/api/v2") do |f|
f.adapter Faraday.default_adapter
end.get('status.json').body
json = Yajl::Parser.parse result, symbolize_keys: true
status = json[:status][:description]
updated_at = DateTime.parse(json[:page][:updated_at]).strftime "%F %T %:z"
"#{status} [#{updated_at}]"
end
def console
Console.new(self)
nil
end
def tasks project_code=nil
tasks = Task.all
if tasks.empty? # cache tasks
DayEntry.today(http)
tasks = Task.all
end
tasks.select! {|t| t.project.code == project_code } if project_code
if tasks.empty?
fail "No matching tasks."
end
tasks.map { |task| "#{task.project.id} #{task.id}\t#{task}" }.join("\n")
end
def set key = nil, *args
if key.nil?
@settings.each do |k, v|
puts "#{k}: #{v}"
end
else
value = args.join(' ')
@settings ||= {}
@settings[key] = value
write_settings
end
nil
end
def cancel
entry = DayEntry.with_timer(http) || DayEntry.last(http)
if entry
confirmed = /^y/.match(ask("#{entry}\nDelete this entry? (y/n): ").downcase)
return unless confirmed
if entry.cancel http
"Deleted entry #{entry}."
else
fail "Failed to delete #{entry}!"
end
else
fail 'Nothing to cancel.'
end
end
alias_method :oops, :cancel
alias_method :nvm, :cancel
def unset key
@settings.delete key
write_settings
end
def unalias task
unset "task.#{task}"
"Removed task alias @#{task}."
end
def alias task_name, *value
task = Task.find(*value)
if task
set "task.#{task_name}", *value
"Added alias @#{task_name} for #{task}."
else
fail "Unrecognized project and task ID: #{value.inspect}"
end
end
def completion command=nil
command ||= $PROGRAM_NAME.split('/').last
$stderr.puts \
"The hcl completion command is deprecated (and slow!), instead use something like:",
"> complete -W \"`cat #{HCl::App::ALIAS_LIST}`\" #{command}"
%[complete -W "#{aliases.join ' '}" #{command}]
end
def aliases
@settings.keys.select { |s| s =~ /^task\./ }.map { |s| "@"+s.slice(5..-1) }
end
def start *args
starting_time = get_starting_time args
task = get_task args
if task.nil?
fail "Unknown task alias, try one of the following: ", aliases.join(', ')
end
timer = task.start http,
:starting_time => starting_time,
:note => args.join(' ')
"Started timer for #{timer} (at #{current_time})"
end
def log *args
fail "There is already a timer running." if DayEntry.with_timer(http)
start(*args)
stop
end
def toggle *args
ident = get_ident args
entry = if ident
task_ids = get_task_ids ident, args
DayEntry.last_by_task http, *task_ids
else
DayEntry.last(http)
end
if entry
if entry.running?
"Stopping #{entry} (at #{current_time})"
else
"Starting #{entry} (at #{current_time})"
end
entry.toggle http
else
fail "No timer found."
end
end
def stop *args
entry = DayEntry.with_timer(http) || DayEntry.with_timer(http, Date.today - 1)
if entry
entry.append_note(http, args.join(' ')) if args.any?
entry.toggle http
"Stopped #{entry} (at #{current_time})"
else
fail "No running timers found."
end
end
def note *args
entry = DayEntry.with_timer http
if entry
if args.empty?
return entry.notes
else
entry.append_note http, args.join(' ')
"Added note to #{entry}."
end
else
fail "No running timers found."
end
end
def show *args
date = args.empty? ? nil : Chronic.parse(args.join(' '))
total_hours = 0.0
result = ''
DayEntry.daily(http, date).each do |day|
running = day.running? ? '(running) ' : ''
columns = HighLine::SystemExtensions.terminal_size[0] rescue 80
result << "\t#{day.formatted_hours}\t#{running}#{day.project}: #{day.notes.lines.to_a.last}\n"[0..columns-1]
total_hours = total_hours + day.hours.to_f
end
result << ("\t" + '-' * 13) << "\n"
result << "\t#{as_hours total_hours}\ttotal (as of #{current_time})\n"
end
def resume *args
ident = get_ident args
entry = if ident
task_ids = get_task_ids ident, args
DayEntry.last_by_task http, *task_ids
else
DayEntry.last(http)
end
if entry
entry.toggle http
else
fail "No matching timer found."
end
end
deprecate :resume, 'use toggle instead'
end
end