Class: Doing::ByDayExport

Inherits:
Object show all
Defined in:
lib/doing/plugins/export/byday.rb

Class Method Summary collapse

Class Method Details

.render(wwid, items, variables: {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/doing/plugins/export/byday.rb', line 18

def self.render(wwid, items, variables: {})
  return if items.nil?

  days = {}

  items.each do |item|
    date = item.date.strftime('%Y-%m-%d')
    days[date] ||= []
    days[date].push(item)
  end

  totals = {}
  total = 0
  tag_totals = Hash.new(0)

  days.each do |day, day_items|
    day_items.each do |item|
      totals[day] ||= 0
      duration = item.interval || 0
      totals[day] += duration
      total += duration

      item.title.scan(/(?mi)@(\S+?)(\(.*\))?(?=\s|$)/).each do |m|
        tag = m[0].downcase
        next if tag == 'done'

        tag_totals[tag] += duration
      end
    end
  end

  budgets = Doing.setting('budgets', {}) || {}
  budgets = budgets.transform_keys { |k| k.to_s.downcase }
  budgets_total = 0

  budget_fmt = lambda do |secs|
    secs = secs.to_i
    return '0h' if secs <= 0

    minutes = (secs / 60).to_i
    hours = (minutes / 60).to_i
    mins = (minutes % 60).to_i
    return format('%<h>dh', h: hours) if mins.zero?
    return format('%<m>dm', m: mins) if hours.zero?

    format('%<h>dh%<m>dm', h: hours, m: mins)
  end

  budgets.each do |tag, budget_secs|
    used = tag_totals[tag].to_i
    remaining = budget_secs.to_i - used
    remaining = 0 if remaining.negative?
    budgets_total += remaining
  end

  width = wwid.config['plugins']['byday']['item_width'].to_i || 60
  divider = "{wd}+{xk}#{'-' * 10}{wd}+{xk}#{'-' * width}{wd}+{xk}#{'-' * 8}{wd}+{x}"
  out = []
  out << divider
  out << "{wd}|{xm}date      {wd}|{xbw}item#{' ' * (width - 4)}{wd}|{xy}duration{wd}|{x}"
  out << divider
  days.each do |day, day_items|
    first = day_items.slice!(0, 1)[0]
    interval = wwid.get_interval(first, formatted: true) || '00:00:00'
    title = first.title.tag('done', remove: true).trunc(width - 2).ljust(width)
    out << "{wd}|{xm}#{day}{wd}|{xbw}#{title}{wd}|{xy}#{interval}{wd}|{x}"
    day_items.each do |item|
      interval = wwid.get_interval(item, formatted: true) || '00:00:00'
      title = item.title.tag('done', remove: true).trunc(width - 2).ljust(width)
      out << "{wd}|          |{xbw}#{title}{wd}|{xy}#{interval}{wd}|{x}"
    end
    day_total = "Total: #{totals[day].time_string(format: :clock)}"
    if budgets_total.positive?
      day_total += " (total budgets left #{budget_fmt.call(budgets_total)})"
    end
    out << divider
    out << "{wd}|{xg}#{day_total.rjust(width + 20)}{wd}|{x}"
    out << divider
  end
  all_total = "Grand Total: #{total.time_string(format: :clock)}"
  if budgets_total.positive?
    all_total += " (total budgets left #{budget_fmt.call(budgets_total)})"
  end
  out << "{wd}|{xrb}#{all_total.rjust(width + 20)}{wd}|{x}"
  out << divider
  Doing::Color.template(out.join("\n"))
end

.settingsObject



9
10
11
12
13
14
15
16
# File 'lib/doing/plugins/export/byday.rb', line 9

def self.settings
  {
    trigger: 'byday',
    config: {
      'item_width' => 60
    }
  }
end