Class: Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/printer.rb

Overview

Printer for work log entries

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, people = nil) ⇒ Printer

Initializes the printer with a list of people.

Parameters:

  • configuration (Configuration)

    The configuration.

  • people (Array<Person>) (defaults to: nil)

    An array of Person objects.



12
13
14
15
# File 'lib/printer.rb', line 12

def initialize(configuration, people = nil)
  @configuration = configuration
  @people = people || {}
end

Instance Attribute Details

#peopleObject (readonly)

Returns the value of attribute people.



7
8
9
# File 'lib/printer.rb', line 7

def people
  @people
end

Instance Method Details

#no_entries(start_date, end_date) ⇒ void

This method returns an undefined value.

Print a message when no entries are found.

Parameters:

  • start_date (Date)
  • end_date (Date)


55
56
57
58
59
60
61
62
63
64
# File 'lib/printer.rb', line 55

def no_entries(start_date, end_date)
  if start_date == end_date
    date_string = start_date.strftime('%a, %B %-d, %Y')
    puts "No entries found for #{Rainbow(date_string).gold}."
  else
    start_date_string = start_date.strftime('%a, %B %-d, %Y')
    end_date_string = end_date.strftime('%a, %B %-d, %Y')
    puts "No entries found between #{Rainbow(start_date_string).gold} and #{Rainbow(end_date_string).gold}."
  end
end

This method returns an undefined value.

Prints a whole day of work log entries. If date_inline is true, the date is printed inline with the time. If epics_only is true, only epic entries are printed.

Examples:

printer.print_day(daily_log, date_inline: true, epics_only: false, project: 'xyz')
printer.print_day(daily_log, date_inline: false, epics_only: true)

Parameters:

  • daily_log (DailyLog)

    The daily log containing entries.

  • date_inline (Boolean) (defaults to: false)

    If true, the date is printed inline with the time.

  • epics_only (Boolean) (defaults to: false)

    If true, only epic entries are printed.

  • filter (Hash) (defaults to: {})

    A hash of filters to apply to the entries.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/printer.rb', line 28

def print_day(daily_log, date_inline = false, epics_only = false, filter = {})
  daily_log.date = Date.strptime(daily_log.date, '%Y-%m-%d') unless daily_log.date.respond_to?(:strftime)

  date_string = daily_log.date.strftime('%a, %B %-d, %Y')
  preamble = "Work log for #{Rainbow(date_string).gold}"
  puts preamble unless date_inline

  # Print only if there are entries to show, especially when epics_only is true
  entries_count = daily_log.entries.count do |entry|
    (!epics_only || entry.epic?) && filter.compact.all? { |k, v| entry.send(k) == v }
  end

  puts Rainbow('-' * preamble.gsub(/\e\[[0-9;]*m/, '').length).gold if entries_count.positive?

  daily_log.entries.each do |entry|
    next if epics_only && !entry.epic?

    next unless filter.compact.all? { |k, v| entry.send(k) == v }

    print_entry(daily_log, entry, date_inline)
  end
end

#print_entry(daily_log, entry, date_inline = false) ⇒ Object

Prints a single entry, formats the date and time.

Parameters:

  • daily_log (DailyLog)
  • entry (LogEntry)
  • date_inline (Boolean) (defaults to: false)

    If true, the date is printed inline with the time.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/printer.rb', line 70

def print_entry(daily_log, entry, date_inline = false)
  # Backwards compatibility: convert strings to Date/Time objects if necessary
  entry.time = Time.strptime(entry.time, '%H:%M:%S') unless entry.time.respond_to?(:strftime)

  # Convert to local time zone
  entry.time = entry.time.getlocal(@configuration.timezone) if @configuration.timezone

  time_string = if date_inline
                  "#{daily_log.date.strftime('%a, %Y-%m-%d')} #{entry.time.strftime('%H:%M')}"
                else
                  entry.time.strftime('%H:%M')
                end
  print '  ' unless date_inline
  puts "#{Rainbow(time_string).gold} #{entry.message_string(@people)}"
end

#terminal_widthInteger

Returns the width of the terminal.

Returns:

  • (Integer)

    The width of the terminal in number of characters.



88
89
90
# File 'lib/printer.rb', line 88

def terminal_width
  IO.console.winsize[1]
end