Skip to content

Instantly share code, notes, and snippets.

@paolobrasolin
Last active October 15, 2021 15:19
Show Gist options
  • Save paolobrasolin/97fef8eef0b158c67443c664d69f3eba to your computer and use it in GitHub Desktop.
Save paolobrasolin/97fef8eef0b158c67443c664d69f3eba to your computer and use it in GitHub Desktop.
Timetrap formatter

Place the formatter in ~/.timetrap/formatters/det.rb and then run

t week -f det

to obtain

2019-11-18 10:23 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■⠅⠅⠅⠅⠅⠅⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
            4:36 pendami
            5:47 bertib
2019-11-19  7:54 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■⠕⠕⠕⠕⠕⠕⠕⠕⠅⠅⠅⠅⠅⠅⠅⠅⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
            7:54 pendami
2019-11-20  8:56 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■⠕⠕⠕⠕⠅⠅⠅⠅⠅⠅⠅⠅⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
            8:46 pendamy
            0:10 dsmarm
2019-11-21  8:18 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■⠕⠕⠕⠕⠕⠕⠕⠅⠅⠅⠅⠅⠅⠅⠅⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
            7:59 pendami
            0:19 bertib
2019-11-22  9:11 ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■⠕⠕⠕⠅⠅⠅⠅⠅⠅⠅⠅⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂⠂
            8:57 pendami
            0:13 bertib
# ~/.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
require 'paint'
# Paint.mode = 0
class Timetrap::Formatters::Tot
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment