class Chronolog::Engine
Public Class Methods
new(file)
click to toggle source
# File lib/chronolog/engine.rb, line 7 def initialize(file) @csv = CSV.new(file, col_sep: " ") setup_variables read_csv end
Public Instance Methods
print(unit: "hours", time: "now")
click to toggle source
# File lib/chronolog/engine.rb, line 30 def print(unit: "hours", time: "now") stopped_at = Chronic.parse(time) started_at = @started_at || stopped_at current = stopped_at - started_at print_header(format("Time logged for %s", stopped_at.strftime("%F"))) %w[year month day session].each do |period| next if period == "session" && time != "now" duration = (current + previous(period, started_at)) / unit_length(unit) left = Rainbow(format("%- 10s", "#{period.capitalize}:")) right = Rainbow(format("% 9.2f %s", duration, unit)).cyan puts "#{left} #{right}" end end
start(time: "now")
click to toggle source
# File lib/chronolog/engine.rb, line 14 def start(time: "now") raise "Received 'start' after 'start'" unless @started_at.nil? @started_at = Chronic.parse(time) @csv << ["started", "at", @started_at.to_i] print_header(format("Started session at %s", @started_at)) end
Also aliased as: started
stop(time: "now")
click to toggle source
# File lib/chronolog/engine.rb, line 21 def stop(time: "now") raise "Received 'stop' before 'start'" if @started_at.nil? @stopped_at = Chronic.parse(time) @csv << ["stopped", "at", @stopped_at.to_i] update_variables @started_at = nil print_header(format("Stopped session at %s", @stopped_at)) end
Also aliased as: stopped
Protected Instance Methods
previous(period, started_at)
click to toggle source
# File lib/chronolog/engine.rb, line 83 def previous(period, started_at) return 0 if period == "session" h = instance_variable_get("@#{period}s") k = started_at.send(period) h.fetch(k, 0) end
print_header(text)
click to toggle source
# File lib/chronolog/engine.rb, line 49 def print_header(text) puts Rainbow(text).yellow end
read_csv()
click to toggle source
# File lib/chronolog/engine.rb, line 60 def read_csv @csv.each do |row| timestamp = row[2].to_i case row[0] when "started" @started_at = Time.at(timestamp) when "stopped" @stopped_at = Time.at(timestamp) raise "Found a 'stop' without a 'start'" if @started_at.nil? raise "Found a 'stop' before a 'start'" if @started_at > @stopped_at update_variables @started_at = nil end end end
setup_variables()
click to toggle source
# File lib/chronolog/engine.rb, line 53 def setup_variables @years = Hash.new(0) @months = Hash.new(0) @days = Hash.new(0) @csv.rewind end
unit_length(unit)
click to toggle source
# File lib/chronolog/engine.rb, line 90 def unit_length(unit) case unit when "seconds" 1.0 when "kiloseconds" 1000.0 when "hours" 3600.0 when "days" 86400.0 when "centidays" 864.0 else raise "Unsupported time unit" end end
update_variables()
click to toggle source
# File lib/chronolog/engine.rb, line 76 def update_variables duration = @stopped_at - @started_at @years[@started_at.year] += duration @months[@started_at.month] += duration @days[@started_at.day] += duration end