Class: Timet::Table
- Inherits:
-
Object
- Object
- Timet::Table
- 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.
Constructor Details
#initialize(filter, items, db) ⇒ Table
Returns a new instance of Table.
10 11 12 13 14 |
# File 'lib/timet/table.rb', line 10 def initialize(filter, items, db) @filter = filter @items = items @db = db end |
Instance Attribute Details
#filter ⇒ Object (readonly)
Returns the value of attribute filter.
8 9 10 |
# File 'lib/timet/table.rb', line 8 def filter @filter end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
8 9 10 |
# File 'lib/timet/table.rb', line 8 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.blinkcolor formatting, which assumes the presence of aStringextension 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.
287 288 289 290 291 |
# File 'lib/timet/table.rb', line 287 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.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/timet/table.rb', line 138 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_itemmethod. -
If the
pomodorovalue is positive and the end time is not set, a blinkingtimetis added.
Formats the end time of the time entry.
201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/timet/table.rb', line 201 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_itemmethod. -
If the
pomodorovalue is positive, a special mark is added.
Formats the mark for the time entry.
229 230 231 232 233 234 |
# File 'lib/timet/table.rb', line 229 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.
245 246 247 248 249 250 251 252 |
# File 'lib/timet/table.rb', line 245 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, andformat_notesmethods are used to format specific parts of the row.
Formats a table row with the given row data.
175 176 177 178 179 180 181 182 |
# File 'lib/timet/table.rb', line 175 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.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/timet/table.rb', line 51 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.
118 119 120 121 122 123 124 125 |
# File 'lib/timet/table.rb', line 118 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_entryto display the time entry ifdisplayistrue. -
It processes each time block item using
process_time_block_item. -
The
TimeHelper.extract_datemethod 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.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/timet/table.rb', line 91 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.
70 71 72 |
# File 'lib/timet/table.rb', line 70 def separator '+-------+------------+--------+----------+----------+----------+' end |
#table ⇒ String
-
The method relies on the
header,process_time_entries,separator, andtotalmethods. -
The
headermethod is responsible for printing the table header. -
The
process_time_entriesmethod processes the time entries and returns the time block. -
The
separatormethod returns a string representing the separator line. -
The
totalmethod prints the total duration.
Generates and displays a table summarizing time entries, including headers, time blocks, and total durations.
34 35 36 37 38 39 40 |
# File 'lib/timet/table.rb', line 34 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.
262 263 264 265 266 267 268 269 |
# File 'lib/timet/table.rb', line 262 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 |