Module: HCl::Commands

Included in:
App
Defined in:
lib/hcl/commands.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Instance Method Details

#alias(task_name, *value) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/hcl/commands.rb', line 70

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

#aliasesObject



85
86
87
# File 'lib/hcl/commands.rb', line 85

def aliases
  @settings.keys.select { |s| s =~ /^task\./ }.map { |s| "@"+s.slice(5..-1) }
end

#cancelObject Also known as: oops, nvm



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hcl/commands.rb', line 45

def cancel
  entry = DayEntry.with_timer(http) || DayEntry.last(http)
  if entry
    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



80
81
82
83
# File 'lib/hcl/commands.rb', line 80

def completion command=nil
  command ||= $PROGRAM_NAME.split('/').last
  %[complete -W "#{aliases.join ' '}" #{command}]
end

#configObject

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

#consoleObject



13
14
15
16
# File 'lib/hcl/commands.rb', line 13

def console
  Console.new(self)
  nil
end

#log(*args) ⇒ Object



101
102
103
104
105
# File 'lib/hcl/commands.rb', line 101

def log *args
  fail "There is already a timer running." if DayEntry.with_timer(http)
  start *args
  stop
end

#note(*args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/hcl/commands.rb', line 118

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hcl/commands.rb', line 146

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



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hcl/commands.rb', line 31

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



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/hcl/commands.rb', line 132

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



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/hcl/commands.rb', line 89

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

#stop(*args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/hcl/commands.rb', line 107

def stop *args
  entry = DayEntry.with_timer(http) || DayEntry.with_timer(http, DateTime.yesterday)
  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



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hcl/commands.rb', line 18

def tasks project_code=nil
  tasks = Task.all
  if tasks.empty? # cache tasks
    DayEntry.today
    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



65
66
67
68
# File 'lib/hcl/commands.rb', line 65

def unalias task
  unset "task.#{task}"
  "Removed task alias @#{task}."
end

#unset(key) ⇒ Object



60
61
62
63
# File 'lib/hcl/commands.rb', line 60

def unset key
  @settings.delete key
  write_settings
end