Class: Timet::Table

Inherits:
Object
  • Object
show all
Includes:
TimeReportHelper
Defined in:
lib/timet/table.rb

Overview

This class is responsible for formatting the output of the ‘timet` application. It provides methods for formatting the table header, separators, and rows.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TimeReportHelper

#export_csv, #export_icalendar

Constructor Details

#initialize(filter, items, db) ⇒ Table

Returns a new instance of Table.



14
15
16
17
18
# File 'lib/timet/table.rb', line 14

def initialize(filter, items, db)
  @filter = filter
  @items = items
  @db = db
end

Instance Attribute Details

#filterObject (readonly)

Returns the value of attribute filter.



12
13
14
# File 'lib/timet/table.rb', line 12

def filter
  @filter
end

#itemsObject (readonly)

Returns the value of attribute items.



12
13
14
# File 'lib/timet/table.rb', line 12

def items
  @items
end

Instance Method Details

#display_pomodoro_labelvoid

Note:
  • The method relies on the ‘@items` instance variable, which should be an array of arrays.

  • The 6th column of each sub-array in ‘@items` is expected to contain numeric values.

  • The method uses the ‘blue.blink` color formatting, which assumes the presence of a `String` extension or

gem that supports color formatting.

This method returns an undefined value.

Displays a blinking “Pomodoro” label if the sum of the compacted values in the 6th column of @items is positive.

Examples:

display_pomodoro_label

See Also:



291
292
293
294
295
# File 'lib/timet/table.rb', line 291

def display_pomodoro_label
  return unless @items.map { |x| x[5] }.compact.sum.positive?

  puts "#{'P'.blue.blink}omodoro"
end

#display_time_entry(item, date = nil) ⇒ void

Note:

The method formats and prints the row for the time entry.

This method returns an undefined value.

Displays a single time entry in the report.

Examples:

Display a time entry

display_time_entry(item, '2021-10-01')

Parameters:

  • item (Array)

    The item to display.

  • date (String, nil) (defaults to: nil)

    The date to display. If nil, the date is not displayed.



142
143
144
145
146
147
148
149
150
151
# File 'lib/timet/table.rb', line 142

def display_time_entry(item, date = nil)
  return puts 'Missing time entry data.' unless item

  id, start_time_value, end_time_value, tag_name, notes = item
  duration = TimeHelper.calculate_duration(start_time_value, end_time_value)
  start_time = TimeHelper.format_time(start_time_value)
  end_time = TimeHelper.format_time(end_time_value)
  start_date = date || (' ' * 10)
  puts format_table_row(id, tag_name[0..5], start_date, start_time, end_time, duration, notes)
end

#format_end_time(end_time, id, duration) ⇒ String

Note:
  • The method relies on the ‘@db` instance variable, which should be an object with a `find_item` method.

  • If the ‘pomodoro` value is positive and the end time is not set, a blinking `timet` is added.

Formats the end time of the time entry.

Examples:

format_end_time('09:00:00', 1, 3600)
#=> "09:00:00"

Parameters:

  • end_time (String)

    The end time of the time entry.

  • id (Integer)

    The ID of the time entry.

  • duration (Integer)

    The duration of the time entry in seconds.

Returns:

  • (String)

    The formatted end time.

See Also:



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/timet/table.rb', line 205

def format_end_time(end_time, id, duration)
  end_time = end_time ? end_time.split[1] : '-'
  pomodoro = @db.find_item(id)[5] || 0

  if pomodoro.positive? && end_time == '-'
    delta = @db.seconds_to_hms((@db.find_item(id)[5] * 60) - duration)
    timet = "\e]8;;Session ends\a#{delta}\e]8;;\a".green
    end_time = timet.to_s.blink
  end

  end_time
end

#format_mark(id) ⇒ String

Note:
  • The method relies on the ‘@db` instance variable, which should be an object with a `find_item` method.

  • If the ‘pomodoro` value is positive, a special mark is added.

Formats the mark for the time entry.

Examples:

format_mark(1)
#=> "|"

Parameters:

  • id (Integer)

    The ID of the time entry.

Returns:

  • (String)

    The formatted mark.

See Also:



233
234
235
236
237
238
# File 'lib/timet/table.rb', line 233

def format_mark(id)
  pomodoro = @db.find_item(id)[5] || 0
  mark = '|'
  mark = "#{''.white}#{'P'.blue.blink}" if pomodoro.positive?
  mark
end

#format_notes(notes) ⇒ String

Note:

The method truncates the notes to a maximum of 20 characters and pads them to a fixed width.

Formats the notes column of the time tracking report table.

Examples:

Format notes

format_notes('This is a long note that needs to be truncated')

Parameters:

  • notes (String, nil)

    The notes to be formatted.

Returns:

  • (String)

    The formatted notes.



249
250
251
252
253
254
255
256
# File 'lib/timet/table.rb', line 249

def format_notes(notes)
  spaces = 80
  return ' ' * spaces unless notes

  max_length = spaces - 3
  notes = "#{notes.slice(0, max_length)}..." if notes.length > max_length
  notes.ljust(spaces)
end

#format_table_row(*row) ⇒ String

Note:
  • The method relies on the ‘@db` instance variable, which should be an object with `find_item`

and ‘seconds_to_hms` methods.

  • The ‘format_end_time`, `format_mark`, and `format_notes` methods are used to format specific parts of the row.

Formats a table row with the given row data.

Examples:

row = [1, 'work', '2024-10-21', '08:00:00', '09:00:00', 3600, 'Completed task A']
format_table_row(*row)
#=> "|      1| 2024-10-21 | work   | 08:00:00 | 09:00:00 |   1:00:00 | Completed task A  |"

Parameters:

  • row (Array)

    The row data to format, containing the following elements:

    • id [Integer] The ID of the time entry.

    • tag [String] The tag associated with the time entry.

    • start_date [String] The start date of the time entry.

    • start_time [String] The start time of the time entry.

    • end_time [String] The end time of the time entry.

    • duration [Integer] The duration of the time entry in seconds.

    • notes [String] Any notes associated with the time entry.

Returns:

  • (String)

    The formatted table row.

See Also:



179
180
181
182
183
184
185
186
# File 'lib/timet/table.rb', line 179

def format_table_row(*row)
  id, tag, start_date, start_time, end_time, duration, notes = row
  end_time = format_end_time(end_time, id, duration)
  mark = format_mark(id)

  "| #{id.to_s.rjust(6)}| #{start_date} | #{tag.ljust(6)} | #{start_time.split[1]} | " \
    "#{end_time.rjust(8)} | #{@db.seconds_to_hms(duration).rjust(8)} #{mark} #{format_notes(notes)}"
end

#headervoid

Note:

The method constructs a string representing the table header and prints it.

This method returns an undefined value.

Formats the header of the time tracking report table.

the formatted header.

Examples:

Format and print the table header

header


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

def header
  title = "Tracked time report [#{@filter.blink.red}]:"
  header = <<~TABLE
    #{title}
    #{separator}
    \033[32m| Id    | Date       | Tag    | Start    | End      | Duration | Notes\033[0m
    #{separator}
  TABLE
  puts header
end

#process_time_block_item(item, time_block) ⇒ Hash

Note:
  • The method extracts the start time, end time, and tag from the item.

  • It calculates the number of seconds per hour block using ‘TimeHelper.count_seconds_per_hour_block`.

  • It converts the start time to a date using ‘TimeHelper.timestamp_to_date`.

  • It updates the time block structure by adding the new block hour to the existing structure.

Processes a single time block item and updates the time block structure.

Parameters:

  • item (Array)

    An array containing the item details, including start time, end time, and tag.

  • time_block (Hash)

    The current time block structure.

Returns:

  • (Hash)

    The updated time block structure.

See Also:



122
123
124
125
126
127
128
129
# File 'lib/timet/table.rb', line 122

def process_time_block_item(item, time_block)
  _, start_time, end_time, tag = item

  block_hour = TimeHelper.count_seconds_per_hour_block(start_time, end_time, tag)
   = TimeHelper.timestamp_to_date(start_time)
  time_block[] = Timet::Utils.add_hashes(time_block[], block_hour)
  time_block
end

#process_time_entries(display: true) ⇒ Hash

Note:
  • The method uses ‘display_time_entry` to display the time entry if `display` is `true`.

  • It processes each time block item using ‘process_time_block_item`.

  • The ‘TimeHelper.extract_date` method is used to extract the date from the items.

Processes time entries and generates a time block structure.

This method iterates over each item in the ‘items` array, displays the time entry (if enabled), and processes the time block item to build a nested hash representing the time block structure.

Parameters:

  • display (Boolean) (defaults to: true)

    Whether to display the time entry during processing. Defaults to ‘true`.

Returns:

  • (Hash)

    A nested hash representing the time block structure, where keys are dates and values are processed time block items.

See Also:



95
96
97
98
99
100
101
102
103
104
# File 'lib/timet/table.rb', line 95

def process_time_entries(display: true)
  time_block = Hash.new { |hash, key| hash[key] = {} }

  @items.each_with_index do |item, idx|
    display_time_entry(item, TimeHelper.extract_date(@items, idx)) if display
    time_block = process_time_block_item(item, time_block)
  end

  time_block
end

#separatorString

Note:

The method returns a string representing the separator line for the table.

Formats the separator line for the time tracking report table.

Examples:

Get the formatted table separator

separator # => '+-------+------------+--------+----------+----------+----------+------------+'

Returns:

  • (String)

    The formatted separator line.



74
75
76
# File 'lib/timet/table.rb', line 74

def separator
  '+-------+------------+--------+----------+----------+----------+'
end

#tableString

Note:
  • The method relies on the ‘header`, `process_time_entries`, `separator`, and `total` methods.

  • The ‘header` method is responsible for printing the table header.

  • The ‘process_time_entries` method processes the time entries and returns the time block.

  • The ‘separator` method returns a string representing the separator line.

  • The ‘total` method prints the total duration.

Generates and displays a table summarizing time entries, including headers, time blocks, and total durations.

Examples:

table

Returns:

  • (String)

    The time block string.

See Also:



38
39
40
41
42
43
44
# File 'lib/timet/table.rb', line 38

def table
  header
  time_block = process_time_entries
  puts separator
  total
  time_block
end

#totalvoid

Note:

The method calculates and prints the total duration of the tracked time entries.

This method returns an undefined value.

Displays the total duration of the tracked time entries.

Examples:

Display the total duration

total


266
267
268
269
270
271
272
273
# File 'lib/timet/table.rb', line 266

def total
  total = @items.map do |item|
    TimeHelper.calculate_duration(item[1], item[2])
  end.sum
  puts "|#{' ' * 43}#{'Total:'.blue}  | #{@db.seconds_to_hms(total).rjust(8).blue} |"
  puts separator
  display_pomodoro_label
end