Module: Timet::ApplicationHelper
- Included in:
- Application
- Defined in:
- lib/timet/application_helper.rb
Overview
Provides helper methods for the Timet application.
Defined Under Namespace
Classes: ReportExporter
Instance Method Summary collapse
-
#build_options(time_scope, tag) ⇒ Hash
Builds a hash of options to be used when initializing a TimeReport instance.
-
#delete_item_and_print_message(id, message) ⇒ void
Deletes a tracking item from the database by its ID and prints a confirmation message.
-
#display_and_export_report(report, options) ⇒ void
Displays the report and exports it to a CSV file and/or an iCalendar file if specified.
-
#display_item(item) ⇒ void
Displays the details of a tracking item.
-
#export_report(report, options) ⇒ void
Exports the given report in CSV and iCalendar formats if there are items, otherwise prints a message.
-
#field_value(item, field) ⇒ String, Time
Retrieves the value of a specific field from a tracking item.
-
#play_sound_and_notify(time, tag) ⇒ void
Plays a sound and sends a notification after a specified time.
-
#prompt_for_new_value(item, field) ⇒ String
Prompts the user to enter a new value for a specific field of a tracking item.
-
#resume_complete_task(id) ⇒ void
Resumes a tracking session for a completed task.
-
#run_linux_session(time, tag) ⇒ void
Runs a Pomodoro session on a Linux system.
-
#run_mac_session(time, tag) ⇒ void
Runs a Pomodoro session on a macOS system.
-
#select_field_to_edit ⇒ String
Prompts the user to select a field to edit from a list of available fields.
-
#show_message(tag) ⇒ String
Generates a message indicating that a Pomodoro session is complete and it’s time for a break.
Instance Method Details
#build_options(time_scope, tag) ⇒ Hash
Builds a hash of options to be used when initializing a TimeReport instance.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/timet/application_helper.rb', line 198 def (time_scope, tag) csv_filename = [:csv]&.split('.')&.first ics_filename = [:ics]&.split('.')&.first { filter: time_scope, tag: tag, csv: csv_filename, ics: ics_filename } end |
#delete_item_and_print_message(id, message) ⇒ void
The method deletes the tracking item from the database using ‘@db.delete_item(id)`.
After deleting the item, the method prints the provided message using ‘puts message`.
This method returns an undefined value.
Deletes a tracking item from the database by its ID and prints a confirmation message.
and printing a message.
156 157 158 159 |
# File 'lib/timet/application_helper.rb', line 156 def (id, ) @db.delete_item(id) puts end |
#display_and_export_report(report, options) ⇒ void
This method returns an undefined value.
Displays the report and exports it to a CSV file and/or an iCalendar file if specified.
and exporting the report.
244 245 246 247 |
# File 'lib/timet/application_helper.rb', line 244 def display_and_export_report(report, ) report.display export_report(report, ) end |
#display_item(item) ⇒ void
The method initializes a ‘TimeReport` object with the database and calls `show_row` to display the
This method returns an undefined value.
Displays the details of a tracking item.
item details.
17 18 19 |
# File 'lib/timet/application_helper.rb', line 17 def display_item(item) TimeReport.new(@db).show_row(item) end |
#export_report(report, options) ⇒ void
This method returns an undefined value.
Exports the given report in CSV and iCalendar formats if there are items, otherwise prints a message.
254 255 256 257 258 259 260 261 262 |
# File 'lib/timet/application_helper.rb', line 254 def export_report(report, ) items = report.items if items.any? ReportExporter.export_csv_report(report, ) ReportExporter.export_icalendar_report(report, ) else puts 'No items found to export' end end |
#field_value(item, field) ⇒ String, Time
The method retrieves the index of the field from ‘Timet::Application::FIELD_INDEX`.
If the field is ‘start’ or ‘end’, the method converts the value to a Time object
Retrieves the value of a specific field from a tracking item.
as a Time object.
using ‘TimeHelper.timestamp_to_time`.
68 69 70 71 72 73 74 |
# File 'lib/timet/application_helper.rb', line 68 def field_value(item, field) index = Timet::Application::FIELD_INDEX[field] value = item[index] return TimeHelper.(value) if %w[start end].include?(field) value end |
#play_sound_and_notify(time, tag) ⇒ void
This method uses platform-specific commands and assumes the presence of certain utilities (e.g., ‘notify-send` on Linux, `afplay` on macOS). Ensure these utilities are available on the respective operating systems.
This method returns an undefined value.
Plays a sound and sends a notification after a specified time.
This method is designed to work on Linux and macOS. It triggers a sound and a notification after the specified time has elapsed. On Linux, it also stops a Pomodoro session and sends a desktop notification. On macOS, it plays a system sound and displays a notification.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/timet/application_helper.rb', line 95 def play_sound_and_notify(time, tag) platform = RUBY_PLATFORM.downcase if platform.include?('linux') run_linux_session(time, tag) elsif platform.include?('darwin') run_mac_session(time, tag) else puts 'Unsupported operating system' end end |
#prompt_for_new_value(item, field) ⇒ String
The method retrieves the current value of the field using ‘field_value`.
The method uses ‘TTY::Prompt.new` to prompt the user for a new value, displaying the current value
Prompts the user to enter a new value for a specific field of a tracking item.
in the prompt.
34 35 36 37 38 |
# File 'lib/timet/application_helper.rb', line 34 def prompt_for_new_value(item, field) current_value = field_value(item, field) prompt = TTY::Prompt.new(active_color: :green) prompt.ask("Update #{field} (#{current_value}):") end |
#resume_complete_task(id) ⇒ void
The method fetches the specified or last completed item using ‘@db.find_item` or `@db.last_item`.
If the item is found, it retrieves the tag and notes and calls the ‘start` method to resume the
This method returns an undefined value.
Resumes a tracking session for a completed task.
tracking session.
180 181 182 183 184 185 186 |
# File 'lib/timet/application_helper.rb', line 180 def resume_complete_task(id) item = id ? @db.find_item(id) : @db.last_item return unless item tag, notes = item.values_at(Application::FIELD_INDEX['tag'], Application::FIELD_INDEX['notes']) start(tag, notes) end |
#run_linux_session(time, tag) ⇒ void
This method returns an undefined value.
Runs a Pomodoro session on a Linux system.
111 112 113 114 115 116 |
# File 'lib/timet/application_helper.rb', line 111 def run_linux_session(time, tag) notification_command = "notify-send --icon=clock '#{(tag)}'" command = "sleep #{time} && tput bel && tt stop 0 && #{notification_command} &" pid = Kernel.spawn(command) Process.detach(pid) end |
#run_mac_session(time, tag) ⇒ void
This method returns an undefined value.
Runs a Pomodoro session on a macOS system.
123 124 125 126 127 128 |
# File 'lib/timet/application_helper.rb', line 123 def run_mac_session(time, tag) notification_command = "osascript -e 'display notification \"#{(tag)}\"'" command = "sleep #{time} && afplay /System/Library/Sounds/Basso.aiff && tt stop 0 && #{notification_command} &" pid = Kernel.spawn(command) Process.detach(pid) end |
#select_field_to_edit ⇒ String
The method uses ‘TTY::Prompt.new` to display a list of available fields for the user to select from.
The method returns the selected field in lowercase.
Prompts the user to select a field to edit from a list of available fields.
49 50 51 52 |
# File 'lib/timet/application_helper.rb', line 49 def select_field_to_edit prompt = TTY::Prompt.new(active_color: :green) prompt.select('Edit Field?', Timet::Application::FIELD_INDEX.keys.map(&:capitalize), active_color: :cyan).downcase end |
#show_message(tag) ⇒ String
Generates a message indicating that a Pomodoro session is complete and it’s time for a break.
139 140 141 |
# File 'lib/timet/application_helper.rb', line 139 def (tag) "Pomodoro session complete (#{tag}). Time for a break." end |