-
Notifications
You must be signed in to change notification settings - Fork 6
/
d
executable file
·111 lines (85 loc) · 2.26 KB
/
d
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
#!/usr/bin/env ruby
# Daniel Von Fange
# Matthias Derer
# Configuration
$data_dir = File.expand_path('~/.dtimetracker')
# Program starts
require 'time'
data_dir = $data_dir
input = $*.join(' ').strip
`/usr/bin/env mkdir -p #{data_dir}`
def current_task
return task("current")
end
def last_task
return task("last")
end
def task(whichtask)
return nil if ! File.exists?("#{$data_dir}/#{whichtask}")
File.open("#{$data_dir}/#{whichtask}",'r') do |f|
line = f.gets
return if line.nil?
start, task = line.strip.split("\t")
start = Time.parse(start)
end_time = Time.new
minutes = ((end_time - start).to_f / 60).ceil
return start, task, end_time, minutes
end
end
def set_current_task(task)
`/usr/bin/env mkdir -p #{$data_dir}`
if File.exists?("#{$data_dir}/last")
File.unlink("#{$data_dir}/last")
end
File.open("#{$data_dir}/current",'w') do |f|
f.puts "#{Time.now}\t#{task.gsub(/\n/,' ')}"
end
end
def h_m(minutes)
hours = (minutes.to_i / 60).floor
hours_minutes = "#{hours}:#{'%02d' % (minutes % 60)}"
end
# And now for the real work...
# Show current task
if input.empty?
if ! current_task
puts "You're not working on anything"
exit
end
start, task, end_time, minutes = current_task
puts "In progress\t#{h_m(minutes)}\t#{task}"
exit
end
if input.match(/^(e|edit)$/)
if ! ENV['EDITOR']
puts "No EDITOR environment varible defined"
exit
end
`#{ENV['EDITOR']} #{data_dir}/current`
exit
end
if input.match(/^(r|resume)$/)
if ! last_task
puts "No task to resume"
exit
end
start, task, end_time, minutes = last_task
set_current_task(task)
puts "Resuming #{task}"
exit
end
# If there's a current task, record the time spent on it.
if current_task
start, task, end_time, minutes = current_task
`/usr/bin/env mkdir -p #{$data_dir}/#{Time.now.year}`
File.open("#{data_dir}/#{Time.now.year}/#{start.strftime('%Y-%m-%d')}.txt",'a') do |f|
f.puts "#{start}\t#{end_time}\t#{task}\t#{minutes}"
end
puts("Finished\t#{h_m(minutes)}\t#{task}")
File.rename("#{data_dir}/current", "#{data_dir}/last")
end
# Unless we are only marking a task done, start a new task
if ! input.match(/^(d|done|stop|)$/)
set_current_task(input)
puts "Started \tnow\t#{input}"
end