Class: Timet::Table
- Inherits:
-
Object
- Object
- Timet::Table
- 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
-
#filter ⇒ Object
readonly
Returns the value of attribute filter.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
-
#display_pomodoro_label ⇒ void
Displays a blinking “Pomodoro” label if the sum of the compacted values in the 6th column of @items is positive.
-
#display_time_entry(item, date = nil) ⇒ void
Displays a single time entry in the report.
-
#format_end_time(end_time, id, duration) ⇒ String
Formats the end time of the time entry.
-
#format_mark(id) ⇒ String
Formats the mark for the time entry.
-
#format_notes(notes) ⇒ String
Formats the notes column of the time tracking report table.
-
#format_table_row(*row) ⇒ String
Formats a table row with the given row data.
-
#header ⇒ void
Formats the header of the time tracking report table.
-
#initialize(filter, items, db) ⇒ Table
constructor
A new instance of Table.
-
#process_time_block_item(item, time_block) ⇒ Hash
Processes a single time block item and updates the time block structure.
-
#process_time_entries(display: true) ⇒ Hash
Processes time entries and generates a time block structure.
-
#separator ⇒ String
Formats the separator line for the time tracking report table.
-
#table ⇒ String
Generates and displays a table summarizing time entries, including headers, time blocks, and total durations.
-
#total ⇒ void
Displays the total duration of the tracked time entries.
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
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
12 13 14 |
# File 'lib/timet/table.rb', line 12 def filter @filter end |
#items ⇒ Object (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_label ⇒ void
-
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.
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
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.
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
-
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.
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
-
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.
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
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.
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
-
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.
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 |
#header ⇒ void
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.
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
-
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.
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) date_line = TimeHelper.(start_time) time_block[date_line] = Timet::Utils.add_hashes(time_block[date_line], block_hour) time_block end |
#process_time_entries(display: true) ⇒ Hash
-
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.
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 |
#separator ⇒ String
The method returns a string representing the separator line for the table.
Formats the separator line for the time tracking report table.
74 75 76 |
# File 'lib/timet/table.rb', line 74 def separator '+-------+------------+--------+----------+----------+----------+' end |
#table ⇒ String
-
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.
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 |
#total ⇒ void
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.
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 |