-
Notifications
You must be signed in to change notification settings - Fork 1
/
log
executable file
·56 lines (52 loc) · 1.94 KB
/
log
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
#!/usr/bin/env ruby
require 'sqlite3'
DB = SQLite3::Database.new 'eo.db'
cmds = %w(start finish show sum)
firstword = ARGV[0]
raise "needs first word: #{cmds.join(',')}" unless cmds.include? firstword
def show(row)
puts "%s\t%d\t%s\t%s" % [
row[0],
row[3],
row[1].gsub("\n", ' '),
String(row[2]).gsub("\n", ' ')]
end
case firstword
when 'start'
rows = DB.execute("select start, actions from log where finish is null")
if rows.size > 0
puts "finish this one first: %s\t%s" % [rows[0][0], rows[0][1]]
print "how many minutes spent on it? "
minutes = STDIN.gets.strip.to_i
DB.execute("update log set finish = datetime(start, '+#{minutes} minutes') where start = ?", rows[0][0])
else
print "actions? "
actions = STDIN.gets.strip
DB.execute("insert into log(start, actions) values (datetime('now', 'localtime'), ?)", actions)
puts "OK GO"
end
when 'finish'
rows = DB.execute("select start from log where finish is null")
unless rows.size == 1
puts 'not one open log entry'
else
start = rows[0][0]
print "thoughts? "
thoughts = STDIN.gets.strip
if thoughts.size > 0
DB.execute("update log set finish=datetime('now', 'localtime'), thoughts=? where start=?", thoughts, start)
else
DB.execute("update log set finish=datetime('now', 'localtime') where start=?", start)
end
rows = DB.execute("select start, actions, thoughts, (strftime('%s', finish) - strftime('%s', start)) / 60 from log where start = ?", start)
show rows[0]
end
when 'show'
DB.execute("select start, actions, thoughts, (strftime('%s', finish) - strftime('%s', start)) / 60 from log where finish is not null order by start").each do |row|
show row
end
when 'sum'
rows = DB.execute("select (strftime('%s', finish) - strftime('%s', start)) / 60 from log where finish is not null")
minutes = rows.inject(0) {|s,e| s += e[0].to_i}
puts '%d hours and %d minutes' % [minutes / 60, minutes % 60]
end