Class: Timet::TimeBlockChart

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

Overview

The TimeBlockChart class is responsible for generating and printing a visual representation of time blocks for a given set of data. It uses character mapping to represent different time ranges and provides methods to print the chart with headers, footers, and colored blocks.

Example usage:

time_block = {
  "2023-10-01" => { "08" => [3600, "work"], "09" => [1800, "break"] },
  "2023-10-02" => { "10" => [4500, "work"] }
}
colors = { "work" => 31, "break" => 32 }
chart = TimeBlockChart.new(table)
chart.print_time_block_chart(table, colors)

Constant Summary collapse

SEPARATOR_CHAR =

Separator character for the chart

''
DATE_WEEK_CONTENT_WIDTH =

Width of the date/week string content (e.g., “02 2023-10-01 Fri”)

17
DATE_WEEK_BORDER_WIDTH =

Width of the ‘┆- ’ part after the date/week string

3
TOTAL_HOURS_COLUMN_WIDTH =

Width of the total hours column including the border

4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ void

Note:
  • The ‘@time_block` instance variable is populated by processing the time entries from the table.

  • The ‘@start_hour` and `@end_hour` instance variables are calculated based on the earliest and latest hours present in the time block data.

Initializes a new TimeBlockChart instance.

This method sets up the time block chart by processing the time entries from the provided table and determining the start and end hours for the chart based on the time block data.

Parameters:

  • table (Table)

    The table instance containing the time entries to be processed.

See Also:



50
51
52
53
54
55
56
57
58
59
# File 'lib/timet/time_block_chart.rb', line 50

def initialize(table)
  @time_block = table.process_time_entries(display: false)
  hours = @time_block.values.map(&:keys).flatten.uniq
  if hours.empty?
    @start_hour = @end_hour = 0
    return
  end
  @start_hour = hours.min.to_i
  @end_hour   = hours.max.to_i
end

Instance Attribute Details

#end_hourInteger (readonly)

The ending hour of the time block

Returns:

  • (Integer)

    the current value of end_hour



23
24
25
# File 'lib/timet/time_block_chart.rb', line 23

def end_hour
  @end_hour
end

#start_hourInteger (readonly)

The starting hour of the time block

Returns:

  • (Integer)

    the current value of start_hour



23
24
25
# File 'lib/timet/time_block_chart.rb', line 23

def start_hour
  @start_hour
end

Instance Method Details

Note:
  • The method first prints the header of the chart, which includes the time range.

  • It then prints the time blocks, using the provided color mapping to visually distinguish between different tags.

This method returns an undefined value.

Prints the time block chart.

This method formats and prints the time block chart, including the header and the time blocks for each entry. The chart is color-coded based on the provided color mapping for different tags.

Examples:

Print a time block chart

# Assuming 'table' is an instance of Timet::Table
chart = TimeBlockChart.new(table)
colors = { "work" => 0, "break" => 1 } # Example color mapping
chart.print_time_block_chart(colors)

Parameters:

  • colors (Hash)

    A mapping of tags to colors, used to color-code the time blocks.

See Also:

  • #print_header
  • #print_blocks


82
83
84
85
86
87
# File 'lib/timet/time_block_chart.rb', line 82

def print_time_block_chart(colors)
  return puts 'No time-block data to display.' if @no_data

  print_header
  print_blocks(colors)
end