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.
212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/timet/application_helper.rb', line 212 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, report: [:report], search: [:search] } 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.
170 171 172 173 |
# File 'lib/timet/application_helper.rb', line 170 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.
260 261 262 263 |
# File 'lib/timet/application_helper.rb', line 260 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.
19 20 21 |
# File 'lib/timet/application_helper.rb', line 19 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.
270 271 272 273 274 275 276 277 278 |
# File 'lib/timet/application_helper.rb', line 270 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`.
70 71 72 73 74 75 76 |
# File 'lib/timet/application_helper.rb', line 70 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.
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/timet/application_helper.rb', line 97 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.
36 37 38 39 40 |
# File 'lib/timet/application_helper.rb', line 36 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.
194 195 196 197 198 199 200 |
# File 'lib/timet/application_helper.rb', line 194 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.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/timet/application_helper.rb', line 113 def run_linux_session(time, tag) pid = fork do sleep Integer(time) system('tput', 'bel') DiscordNotifier.pomodoro_ended(time / 60) # Notify that a Pomodoro session has ended system('tt', 'stop') system('notify-send', '--icon=clock', (tag)) end Process.detach(pid) end |
#run_mac_session(time, tag) ⇒ void
This method returns an undefined value.
Runs a Pomodoro session on a macOS system.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/timet/application_helper.rb', line 129 def run_mac_session(time, tag) pid = fork do sleep Integer(time) system('afplay', '/System/Library/Sounds/Basso.aiff') DiscordNotifier.pomodoro_ended(time / 60) # Notify that a Pomodoro session has ended system('tt', 'stop') = (tag) # Escape for AppleScript = .gsub('\\', '\\\\').gsub('"', '\"') applescript_command = "display notification \"#{}\"" system('osascript', '-e', applescript_command) end 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.
51 52 53 54 |
# File 'lib/timet/application_helper.rb', line 51 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.
153 154 155 |
# File 'lib/timet/application_helper.rb', line 153 def (tag) "Pomodoro session complete (#{tag}). Time for a break." end |