Module: HCl::Commands
- Included in:
- App
- Defined in:
- lib/hcl/commands.rb
Defined Under Namespace
Classes: Error
Instance Method Summary collapse
- #alias(task_name, *value) ⇒ Object
- #aliases ⇒ Object
- #cancel ⇒ Object (also: #oops, #nvm)
- #completion(command = nil) ⇒ Object
-
#config ⇒ Object
Display a sanitized view of your auth credentials.
- #console ⇒ Object
- #log(*args) ⇒ Object
- #note(*args) ⇒ Object
- #resume(*args) ⇒ Object
- #set(key = nil, *args) ⇒ Object
- #show(*args) ⇒ Object
- #start(*args) ⇒ Object
-
#status ⇒ Object
Show the network status of the Harvest service.
- #stop(*args) ⇒ Object
- #tasks(project_code = nil) ⇒ Object
- #unalias(task) ⇒ Object
- #unset(key) ⇒ Object
Instance Method Details
#alias(task_name, *value) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/hcl/commands.rb', line 86 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 |
#aliases ⇒ Object
104 105 106 |
# File 'lib/hcl/commands.rb', line 104 def aliases @settings.keys.select { |s| s =~ /^task\./ }.map { |s| "@"+s.slice(5..-1) } end |
#cancel ⇒ Object Also known as: oops, nvm
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hcl/commands.rb', line 58 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 |
#completion(command = nil) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/hcl/commands.rb', line 96 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 |
#config ⇒ Object
Display a sanitized view of your auth credentials.
9 10 11 |
# File 'lib/hcl/commands.rb', line 9 def config http.config_hash.merge(password:'***').map {|k,v| "#{k}: #{v}" }.join("\n") end |
#console ⇒ Object
26 27 28 29 |
# File 'lib/hcl/commands.rb', line 26 def console Console.new(self) nil end |
#log(*args) ⇒ Object
120 121 122 123 124 |
# File 'lib/hcl/commands.rb', line 120 def log *args fail "There is already a timer running." if DayEntry.with_timer(http) start(*args) stop end |
#note(*args) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/hcl/commands.rb', line 137 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 |
#resume(*args) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/hcl/commands.rb', line 165 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 |
#set(key = nil, *args) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/hcl/commands.rb', line 44 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 |
#show(*args) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/hcl/commands.rb', line 151 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 |
#start(*args) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/hcl/commands.rb', line 108 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 |
#status ⇒ Object
Show the network status of the Harvest service.
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/hcl/commands.rb', line 14 def status result = Faraday.new("http://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 |
#stop(*args) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/hcl/commands.rb', line 126 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 |
#tasks(project_code = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hcl/commands.rb', line 31 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 |
#unalias(task) ⇒ Object
81 82 83 84 |
# File 'lib/hcl/commands.rb', line 81 def unalias task unset "task.#{task}" "Removed task alias @#{task}." end |
#unset(key) ⇒ Object
76 77 78 79 |
# File 'lib/hcl/commands.rb', line 76 def unset key @settings.delete key write_settings end |