Created
May 6, 2011 12:58
-
-
Save EinLama/958906 to your computer and use it in GitHub Desktop.
Timetracker for projects. Only a quick hack. I intend to build it from scratch - clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require 'yaml' | |
class Timestamp | |
attr_accessor :start, :end | |
def initialize(start_time) | |
@start = start_time | |
end | |
def complete? | |
@start && @end | |
end | |
def time_taken | |
if complete? | |
@end - @start | |
else | |
0 | |
end | |
end | |
end | |
class Project | |
attr_accessor :name, :timestamps, :total_minutes | |
def initialize(name, start) | |
@name = name | |
@timestamps = Array.new | |
@timestamps.push(Timestamp.new(start)) | |
end | |
def refresh | |
time_m | |
end | |
def time_h | |
return time_m / 60 # in hours | |
end | |
def time_m | |
total = 0 | |
@timestamps.each do |stamp| | |
total += stamp.time_taken | |
end | |
@total_minutes = total / 60 | |
return @total_minutes | |
end | |
def ==(other) | |
if other.respond_to?(:name) | |
@name == other.name | |
else | |
@name == other | |
end | |
end | |
end | |
PATH = 'C:\\Users\\toeil\\Documents\\Clock\\' | |
START = Time.now | |
DATE_FORMATTED = START.strftime '%Y_%m_%d_%A' | |
FILENAME = File.join(PATH, "#{DATE_FORMATTED}.yaml") | |
if File.exists? FILENAME | |
file = File.read FILENAME | |
else | |
File.open FILENAME, 'w' do |f| | |
f.write('') | |
end | |
file = File.read FILENAME | |
end | |
projects = YAML::load(file) || Array.new | |
if ARGV.first == 'list' | |
projects.each do |p| | |
puts "#{p.name}:\t%.2f minutes (%.2f hours)" % [p.time_m, p.time_h] | |
end | |
exit! | |
else | |
ARGV.each do |arg| | |
projects.each do |p| | |
p.timestamps.push(Timestamp.new(START)) if p.name == arg | |
end | |
projects.push(Project.new(arg, START)) unless projects.include?(arg) | |
end | |
end | |
while true | |
begin | |
sleep 0.5 | |
# just wait... | |
# we want to write the file on exit | |
rescue SystemExit, Interrupt | |
puts "writing..." | |
_END = Time.now | |
projects.each do |p| | |
if ARGV.include?(p.name) | |
p.timestamps.last.end = _END | |
p.refresh | |
end | |
end | |
File.open FILENAME, 'w' do |f| | |
f.write projects.to_yaml | |
end | |
puts "exiting..." | |
break | |
rescue Exception => e | |
puts e | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment