Created
June 25, 2012 12:02
-
-
Save chintanparikh/2988196 to your computer and use it in GitHub Desktop.
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/env ruby | |
require 'time' | |
require 'yaml' | |
time_file = 'time_log.yml' | |
time_temp_file = 'time_temp.yml' | |
def get_previous_time f | |
f['Time'] | |
end | |
def get_project f | |
f['Current Project'] | |
end | |
def store_temp_info(temp_file, current_project, time) | |
file = File.open(temp_file, 'w') | |
file.write "Current Project: #{current_project}\n" | |
file.write "Time: #{time}" | |
end | |
def get_temp_info(temp_file, current_time) | |
file = YAML.load_file(temp_file) | |
previous_time = get_previous_time(file) | |
current_time = current_time | |
temp_info = Hash.new | |
temp_info[:time_difference] = current_time - previous_time | |
temp_info[:project] = get_project(file) | |
temp_info | |
end | |
def update_time_log(log_file, project, time_difference) | |
time_log = File.size?(log_file) ? YAML.load_file(log_file) : Hash.new | |
total_time = time_log.has_key?(project) ? time_log[project] : 0 | |
time_log[project] = total_time + time_difference | |
time_log_file = File.open(log_file, 'w') do |file| | |
YAML.dump(time_log, file) | |
end | |
end | |
def clear_file(file) | |
File.open(file, 'w') do |f| | |
f.write('') | |
end | |
end | |
case ARGV[0] | |
when "start" | |
unless File.size? time_temp_file | |
store_temp_info(time_temp_file, ARGV[1], Time.now) | |
puts "Working on: #{ARGV[1]}" | |
else | |
info = get_temp_info(time_temp_file, Time.now) | |
puts "You are already working on #{info[:project]}. Please end that first" | |
end | |
when "end" | |
if File.size? time_temp_file | |
info = get_temp_info(time_temp_file, Time.now) | |
update_time_log(time_file, info[:project], info[:time_difference]) | |
clear_file time_temp_file | |
puts "Worked on #{info[:project]} for #{info[:time_difference]/3600} hours" | |
else | |
puts "You need to start something first!" | |
end | |
when "break" | |
if File.size? time_temp_file | |
info = get_temp_info(time_temp_file, Time.now) | |
update_time_log(time_file, info[:project], info[:time_difference]) | |
store_temp_info(time_temp_file, info[:project], Time.now) | |
puts "Hurry back!" | |
else | |
puts "You don't have a project started to take a break from" | |
end | |
when "back" | |
info = get_temp_info(time_temp_file, Time.now) | |
puts "Welcome back. You are working on #{info[:project]}" | |
store_temp_info(time_temp_file, info[:project], Time.now) | |
when "status" | |
if File.size? time_temp_file | |
info = get_temp_info(time_temp_file, Time.now) | |
puts "You have been working on #{info[:project]} for #{info[:time_difference]/3600} hours" | |
else | |
puts "You are not working on anything at the moment" | |
end | |
when "get" | |
if File.size? time_file | |
projects = YAML.load_file(time_file) | |
projects.each do |project, time| | |
puts "You have worked on #{project} for #{time/3600} hours \n" | |
end | |
else | |
puts "You have not worked on anything so far" | |
end | |
when "clear" | |
clear_file time_file | |
puts 'Cleared log' | |
when "finish" | |
if File.size? time_temp_file | |
info = get_temp_info(time_temp_file, Time.now) | |
update_time_log(time_file, info[:project], info[:time_difference]) | |
clear_file time_temp_file | |
puts "Worked on #{info[:project]} for #{info[:time_difference]/3600} hours \n" | |
end | |
puts "Log: \n" | |
if File.size? time_file | |
projects = YAML.load_file(time_file) | |
projects.each do |project, time| | |
puts "You have worked on #{project} for #{time/3600} hours \n" | |
end | |
else | |
puts "You have not worked on anything so far \n" | |
end | |
clear_file time_file | |
puts 'Cleared log' | |
else | |
puts "Sorry, command not recognized. Please try again" | |
end | |
-- errors -- | |
/home/chintanparikh/bin/timetracker: 1: /home/chintanparikh/bin/timetracker: require: not found | |
/home/chintanparikh/bin/timetracker: 2: /home/chintanparikh/bin/timetracker: require: not found | |
/home/chintanparikh/bin/timetracker: 18: /home/chintanparikh/bin/timetracker: time_file: not found | |
/home/chintanparikh/bin/timetracker: 19: /home/chintanparikh/bin/timetracker: time_temp_file: not found | |
/home/chintanparikh/bin/timetracker: 21: /home/chintanparikh/bin/timetracker: def: not found | |
/home/chintanparikh/bin/timetracker: 22: /home/chintanparikh/bin/timetracker: f[Time]: not found | |
/home/chintanparikh/bin/timetracker: 23: /home/chintanparikh/bin/timetracker: end: not found | |
/home/chintanparikh/bin/timetracker: 25: /home/chintanparikh/bin/timetracker: def: not found | |
/home/chintanparikh/bin/timetracker: 26: /home/chintanparikh/bin/timetracker: f[Current Project]: not found | |
/home/chintanparikh/bin/timetracker: 27: /home/chintanparikh/bin/timetracker: end: not found | |
/home/chintanparikh/bin/timetracker: 29: /home/chintanparikh/bin/timetracker: Syntax error: "(" unexpected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment