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.



212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/timet/application_helper.rb', line 212

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,
    report: options[:report],
    search: options[:search]
  }
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.



170
171
172
173
# File 'lib/timet/application_helper.rb', line 170

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.



260
261
262
263
# File 'lib/timet/application_helper.rb', line 260

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.



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.

Parameters:

  • report (Report)

    The report to be exported.

  • options (Hash)

    The options to pass to the exporter.



270
271
272
273
274
275
276
277
278
# File 'lib/timet/application_helper.rb', line 270

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



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



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

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.



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

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:



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.

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.



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

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.



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')
    message = show_message(tag)
    # Escape for AppleScript
    escaped_message = message.gsub('\\', '\\\\').gsub('"', '\"')
    applescript_command = "display notification \"#{escaped_message}\""
    system('osascript', '-e', applescript_command)
  end
  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.



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.

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.



153
154
155
# File 'lib/timet/application_helper.rb', line 153

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