|
# ~/.timetrap/formatters/det.rb |
|
|
|
require 'paint' |
|
|
|
# Paint.mode = 0 |
|
|
|
class Timetrap::Formatters::Det |
|
include Timetrap::Helpers |
|
|
|
TIMEZONES = [ |
|
[' ', 0.0, nil], |
|
['⠿', 4.0, :blue], |
|
['⠽', 8.0, :green], |
|
['⠕', 10.0, :yellow], |
|
['⠅', 12.0, :red], |
|
['⠂', 24.0, :magenta], |
|
] |
|
|
|
DOTS_PER_HOUR = 4 |
|
WORK_CHAR = '■' |
|
UNKN_CHAR = '?' |
|
|
|
def initialize(entries) |
|
@entries = entries |
|
end |
|
|
|
def output |
|
output = [] |
|
# NOTE: we add an empty entry before all others to simplify looping |
|
@entries.unshift(Timetrap::Entry.new(start: @entries.first.start - (3600 * 24))) |
|
@entries.group_by { |e| e.start.to_date }.each_cons(2) do |(prev_date, _), (date, date_entries)| |
|
(prev_date.next_day..date.prev_day).each do |date| |
|
output << format_progressbar(date, 0) |
|
end |
|
output << format_progressbar(date, date_entries.sum(&:duration)) |
|
date_entries.sort_by(&:duration).reverse.group_by(&method(:grouping_critera)).each do |tag, tag_entries| |
|
output << format_detail(tag_entries.sum(&:duration), tag) |
|
end |
|
end |
|
output.join("\n") |
|
end |
|
|
|
def grouping_critera(entry) |
|
entry.note.split.first |
|
end |
|
|
|
def format_progressbar(date, duration) |
|
hours = duration.fdiv(60*60) |
|
output = '' |
|
output << Paint[date.to_s, (date.cwday > 5 ? :yellow : :green), :faint] |
|
output << ' ' |
|
output << Paint[format_time(duration), TIMEZONES.detect{|t|t[1]>hours}&.[](2), :faint] |
|
output << ' ' |
|
TIMEZONES.each_cons(2) do |(_, ti, _), (back_char, tf, color)| |
|
output << Paint[WORK_CHAR * (DOTS_PER_HOUR * [[hours - ti, 0].max, tf - ti].min).round, color] |
|
output << Paint[back_char * (DOTS_PER_HOUR * [[tf - hours, 0].max, tf - ti].min).round, color, :faint] |
|
end |
|
output << Paint[UNKN_CHAR * DOTS_PER_HOUR * [hours - TIMEZONES.last[1], 0].max, :white, :inverse] |
|
end |
|
|
|
def format_detail(duration, description) |
|
' %s %s' % [format_time(duration), description] |
|
end |
|
|
|
def format_time(duration) |
|
"%2s:%02d" % [duration/3600, (duration%3600)/60] |
|
end |
|
end |