class Xfel::Timew::Table

Report's terminal output.

Public Class Methods

new() click to toggle source
# File lib/xfel/timew/table.rb, line 9
def initialize
  @data = {}
  @table = Terminal::Table.new({ headings: %w[Project Hours Total] })
end

Public Instance Methods

add(worklog) click to toggle source
# File lib/xfel/timew/table.rb, line 47
def add(worklog)
  project = worklog[:project]
  ticket = worklog[:key]

  @data[project] = {} unless @data.key?(project)
  @data[project][ticket] = 0 unless @data[project].key?(ticket)
  @data[project][ticket] += worklog[:duration]
end
data_to_table() click to toggle source
# File lib/xfel/timew/table.rb, line 31
def data_to_table
  total = 0
  @data.each do |project, tickets|
    total += project_to_table(project, tickets)
    @table.add_separator
  end
  @table.add_row(['', '', time_str(total)])
end
project_to_table(project, tickets) click to toggle source
# File lib/xfel/timew/table.rb, line 20
def project_to_table(project, tickets)
  @table.add_row([project, '', ''])
  project_total = 0
  tickets.each do |key, duration|
    project_total += duration
    @table.add_row(["└── #{key}", time_str(duration), ''])
  end
  @table.add_row(['', '', time_str(project_total)])
  project_total
end
render() click to toggle source
# File lib/xfel/timew/table.rb, line 40
def render
  data_to_table
  @table.align_column(1, :right)
  @table.align_column(2, :right)
  puts @table
end
time_str(seconds) click to toggle source
# File lib/xfel/timew/table.rb, line 14
def time_str(seconds)
  hours = seconds / 3600
  minutes = seconds / 60 % 60
  format("#{hours}h %02dm", minutes)
end