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

Instance Method Details

#build_options(time_scope, tag) ⇒ Hash

Builds a hash of options to be used when initializing a TimeReport instance.

Examples:

Build options with a filter and tag

build_options('today', 'work') # => { filter: 'today', tag: 'work', csv: nil, ics: nil }

Parameters:

  • time_scope (String, nil)

    The filter to apply when fetching items. Possible values include ‘today’, ‘yesterday’, ‘week’, ‘month’, or a date range in the format ‘YYYY-MM-DD..YYYY-MM-DD’.

  • tag (String, nil)

    The tag to filter the items by.

Returns:

  • (Hash)

    A hash containing the filter, tag, CSV filename, and iCalendar filename.



198
199
200
201
202
203
204
205
206
207
# File 'lib/timet/application_helper.rb', line 198

def build_options(time_scope, tag)
  csv_filename = options[:csv]&.split('.')&.first
  ics_filename = options[:ics]&.split('.')&.first
  {
    filter: time_scope,
    tag: tag,
    csv: csv_filename,
    ics: ics_filename
  }
end

#delete_item_and_print_message(id, message) ⇒ void

Note:

The method deletes the tracking item from the database using ‘@db.delete_item(id)`.

Note:

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.

Examples:

Delete a tracking item with ID 1 and print a confirmation message

delete_item_and_print_message(1, 'Deleted item 1')

Parameters:

  • id (Integer)

    The ID of the tracking item to be deleted.

  • message (String)

    The message to be printed after the item is deleted.



156
157
158
159
# File 'lib/timet/application_helper.rb', line 156

def delete_item_and_print_message(id, message)
  @db.delete_item(id)
  puts message
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.

Examples:

Display and export the report to CSV and iCalendar

display_and_export_report(report, { csv: 'report.csv', ics: 'icalendar.ics' })

Parameters:

  • report (TimeReport)

    The TimeReport instance to display and export.

  • options (Hash)

    A hash containing the options for exporting the report.

Options Hash (options):

  • :csv (String, nil)

    The filename to use when exporting the report to CSV.

  • :ics (String, nil)

    The filename to use when exporting the report to iCalendar.



244
245
246
247
# File 'lib/timet/application_helper.rb', line 244

def display_and_export_report(report, options)
  report.display
  export_report(report, options)
end

#display_item(item) ⇒ void

Note:

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.

Examples:

Display the details of a tracking item

display_item(item)

Parameters:

  • item (Hash)

    The tracking item to be displayed.



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.

Parameters:

  • report (Report)

    The report to be exported.

  • options (Hash)

    The options to pass to the exporter.



254
255
256
257
258
259
260
261
262
# File 'lib/timet/application_helper.rb', line 254

def export_report(report, options)
  items = report.items
  if items.any?
    ReportExporter.export_csv_report(report, options)
    ReportExporter.export_icalendar_report(report, options)
  else
    puts 'No items found to export'
  end
end

#field_value(item, field) ⇒ String, Time

Note:

The method retrieves the index of the field from ‘Timet::Application::FIELD_INDEX`.

Note:

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`.

Examples:

Retrieve the value of the ‘notes’ field

field_value(item, 'notes')

Parameters:

  • item (Hash)

    The tracking item.

  • field (String)

    The field to retrieve the value for.

Returns:

  • (String, Time)

    The value of the specified field. If the field is ‘start’ or ‘end’, it returns the value



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.timestamp_to_time(value) if %w[start end].include?(field)

  value
end

#play_sound_and_notify(time, tag) ⇒ void

Note:

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.

Examples:

play_sound_and_notify(1500, 'work')

Parameters:

  • time (Integer)

    The duration in seconds to wait before playing the sound and sending the notification.

  • tag (String)

    The tag associated with the Pomodoro session.

Raises:

  • (RuntimeError)

    If the operating system is not supported.



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

Note:

The method retrieves the current value of the field using ‘field_value`.

Note:

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.

Examples:

Prompt for a new value for the ‘notes’ field

prompt_for_new_value(item, 'notes')

Parameters:

  • item (Hash)

    The tracking item to be edited.

  • field (String)

    The field to be updated.

Returns:

  • (String)

    The new value entered by the user.



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

Note:

The method fetches the specified or last completed item using ‘@db.find_item` or `@db.last_item`.

Note:

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.

Examples:

Resume the last completed task

resume_complete_task

Resume a specific task by ID

resume_complete_task(123)

Parameters:

  • id (Integer, nil)

    The ID of the tracking item to resume. If nil, the last completed item is used.

See Also:



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.

Parameters:

  • time (Integer)

    The duration of the Pomodoro session in seconds.

  • tag (String)

    A tag or label for the session, used in the notification message.



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 '#{show_message(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.

Parameters:

  • time (Integer)

    The duration of the Pomodoro session in seconds.

  • _tag (String)

    A tag or label for the session, not used in the notification message on macOS.



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 \"#{show_message(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_editString

Note:

The method uses ‘TTY::Prompt.new` to display a list of available fields for the user to select from.

Note:

The method returns the selected field in lowercase.

Prompts the user to select a field to edit from a list of available fields.

Examples:

Prompt for a field to edit

select_field_to_edit

Returns:

  • (String)

    The selected field in lowercase.



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.

Examples:

show_message("work")
# => "Pomodoro session complete (work). Time for a break."

Parameters:

  • tag (String)

    The tag associated with the completed Pomodoro session.

Returns:

  • (String)

    A message indicating the completion of the Pomodoro session and suggesting a break.



139
140
141
# File 'lib/timet/application_helper.rb', line 139

def show_message(tag)
  "Pomodoro session complete (#{tag}). Time for a break."
end