Module: Spill::Renderer

Defined in:
lib/spill/renderer.rb

Constant Summary collapse

STYLE_CODES =
{
  bold: "1",
  dim: "2",
  bold_green: "1;32",
  bold_yellow: "1;33",
  bold_cyan: "1;36"
}.freeze

Class Method Summary collapse

Class Method Details

.age(opened_at, now) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/spill/renderer.rb', line 93

def age(opened_at, now)
  days = (now - opened_at) / 86_400
  if days >= 365
    pluralize((days / 365).floor, "year")
  elsif days >= 30
    pluralize((days / 30).floor, "month")
  elsif days >= 7
    pluralize((days / 7).floor, "week")
  elsif days >= 1
    pluralize(days.floor, "day")
  else
    "today"
  end
end

.doing_line(event, now, color) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spill/renderer.rb', line 73

def doing_line(event, now, color)
  case event.kind
  when :dirty_tree
    "uncommitted changes (#{pluralize(event.extra[:files], "file")})"
  when :branch_wip
    if event.extra[:no_upstream]
      "#{event.ref}: not pushed yet (#{pluralize(event.extra[:unpushed], "commit")})"
    else
      "#{event.ref}: #{event.extra[:ahead]} unpushed #{event.extra[:ahead] == 1 ? "commit" : "commits"}"
    end
  when :pr_open
    line = "PR #{event.ref.delete_prefix("#").prepend("#")} open#{title_suffix(event.title)}"
    if event.extra[:opened_at]
      "#{line}#{style(" · #{age(event.extra[:opened_at], now)}", :dim, color)}"
    else
      line
    end
  end
end

.doing_lines(report, color, now) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/spill/renderer.rb', line 39

def doing_lines(report, color, now)
  return [] if report.doing.empty?

  lines = [ "", style("DOING", :bold_yellow, color) ]
  report.doing.each do |entry|
    lines << "  #{style(entry[:repo], :bold_cyan, color)}"
    entry[:items].each { |event| lines << "    #{doing_line(event, now, color)}" }
  end
  lines
end

.done_lines(report, color) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/spill/renderer.rb', line 23

def done_lines(report, color)
  return [] if report.done.empty?

  lines = [ "", style("DONE", :bold_green, color) ]
  report.done.each do |entry|
    lines << "  #{style(entry[:repo], :bold_cyan, color)}"
    entry[:branches].each do |branch|
      count = branch[:commits].size
      lines << "    #{style("#{branch[:name]} · #{pluralize(count, "commit")}", :dim, color)}"
      branch[:commits].each { |commit| lines << "      #{commit.title}" }
    end
    entry[:github].each { |event| lines << "    #{github_line(event)}" }
  end
  lines
end

.empty?(report) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/spill/renderer.rb', line 19

def empty?(report)
  report.done.empty? && report.doing.empty?
end

.explored_lines(report, color) ⇒ Object



63
64
65
66
67
# File 'lib/spill/renderer.rb', line 63

def explored_lines(report, color)
  return [] if report.explored.empty?

  [ "", style("Explored: #{report.explored.join(", ")}", :dim, color) ]
end

.github_line(event) ⇒ Object



57
58
59
60
61
# File 'lib/spill/renderer.rb', line 57

def github_line(event)
  verb = { pr_merged: "merged PR", pr_opened: "opened PR", pr_opened_and_merged: "opened and merged PR",
           review: "reviewed PR", issue_closed: "closed issue", commented: "commented on" }.fetch(event.kind)
  "#{verb} #{event.ref}#{title_suffix(event.title)}"
end

.pluralize(count, noun) ⇒ Object



108
109
110
# File 'lib/spill/renderer.rb', line 108

def pluralize(count, noun)
  "#{count} #{count == 1 ? noun : "#{noun}s"}"
end

.quiet_lines(report, color) ⇒ Object



50
51
52
53
54
55
# File 'lib/spill/renderer.rb', line 50

def quiet_lines(report, color)
  return [] if report.quiet.empty?

  count = report.quiet.size
  [ "", style("#{count} quiet #{count == 1 ? "repo" : "repos"} skipped", :dim, color) ]
end

.render(report, color: false, now: Time.now) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/spill/renderer.rb', line 5

def render(report, color: false, now: Time.now)
  lines = [ "#{style("spill", :bold, color)} · #{now.strftime("%a %b %-d")} · #{report.window.label}" ]
  if empty?(report)
    lines << "" << "Nothing to spill. 🍵"
  else
    lines.concat(done_lines(report, color))
    lines.concat(doing_lines(report, color, now))
    lines.concat(quiet_lines(report, color))
  end
  lines.concat(explored_lines(report, color))
  report.notes.each { |note| lines << "" << style(note, :dim, color) }
  lines.join("\n") << "\n"
end

.style(text, kind, color) ⇒ Object



120
121
122
123
124
# File 'lib/spill/renderer.rb', line 120

def style(text, kind, color)
  return text unless color

  "\e[#{STYLE_CODES.fetch(kind)}m#{text}\e[0m"
end

.title_suffix(title) ⇒ Object



69
70
71
# File 'lib/spill/renderer.rb', line 69

def title_suffix(title)
  title.to_s.empty? ? "" : "#{title}"
end