Class: Timet::Application
- Inherits:
-
Thor
- Object
- Thor
- Timet::Application
- Includes:
- ApplicationHelper, TimeHelper, ValidationEditHelper
- Defined in:
- lib/timet/application.rb
Overview
Application class that defines CLI commands for time tracking:
-
start: Start time tracking with optional notes
-
stop: Stop time tracking
-
resume: Resume the last task
-
summary: Display a summary of tracked time and export to CSV
-
edit: Edit a task
-
delete: Delete a task
-
cancel: Cancel active time tracking
Constant Summary collapse
- FIELD_INDEX =
{ 'notes' => 4, 'tag' => 3, 'start' => 1, 'end' => 2 }.freeze
- VALID_STATUSES_FOR_INSERTION =
%i[no_items complete].freeze
- VALID_ARGUMENTS =
%w[ cancel delete edit help resume start stop summary sync version ].freeze
- BUCKET =
'timet'
Constants included from ValidationEditHelper
ValidationEditHelper::TIME_FIELDS
Class Method Summary collapse
-
.exit_on_failure? ⇒ Boolean
Determines whether the application should exit when a command fails.
Instance Method Summary collapse
-
#cancel ⇒ void
Cancels the active time tracking session by deleting the last tracking item.
-
#delete(id) ⇒ void
Deletes a specific tracking item by its ID after confirming with the user.
-
#edit(id = nil, field = nil, new_value = nil) ⇒ void
Edits a specific tracking item by its ID, allowing the user to modify fields such as notes, tag, start time, or end time.
-
#initialize(*args) ⇒ Application
constructor
A new instance of Application.
-
#resume(id = nil) ⇒ void
Resumes the last tracking session if it was completed.
-
#start(tag, notes = nil, pomodoro = nil) ⇒ void
Starts a new tracking session with the given tag and optional notes.
-
#stop ⇒ void
Stops the current tracking session if there is one in progress.
-
#summary(time_scope = nil, tag = nil) ⇒ void
Generates a summary of tracking items based on the provided time_scope and tag, and optionally exports the summary to a CSV file and/or an iCalendar file.
- #sync ⇒ Object
- #version ⇒ Object
Methods included from TimeHelper
append_tag_to_hour_blocks, beginning_of_day, calculate_block_end_time_and_seconds, calculate_duration, calculate_end_time, count_seconds_per_hour_block, create_new_datetime, current_timestamp, date_to_timestamp, extract_date, format_time, format_time_string, parse_time_components, timestamp_to_date, timestamp_to_time, update_time_field, valid_time?
Methods included from ApplicationHelper
#build_options, #delete_item_and_print_message, #display_and_export_report, #display_item, #export_report, #field_value, #play_sound_and_notify, #prompt_for_new_value, #resume_complete_task, #run_linux_session, #run_mac_session, #select_field_to_edit, #show_message
Methods included from ValidationEditHelper
#check_collision_with_next_item, #check_collision_with_previous_item, #validate_and_update, #validate_time, #validate_time_collisions
Methods included from TimeValidationHelper
#adjust_end_datetime, #create_new_datetime, #determine_base_date_time, #parse_time_string, #validate_end_time, #validate_future_date, #validate_start_time
Constructor Details
#initialize(*args) ⇒ Application
Returns a new instance of Application.
53 54 55 56 57 |
# File 'lib/timet/application.rb', line 53 def initialize(*args) super config = args[2] || {} # Third argument is the config hash initialize_database(config) end |
Class Method Details
.exit_on_failure? ⇒ Boolean
This method is typically used in command-line applications to control the behavior when a command fails.
Returning ‘true` means that the application will exit immediately if a command fails, which is useful for
Determines whether the application should exit when a command fails.
ensuring that errors are handled gracefully.
337 338 339 |
# File 'lib/timet/application.rb', line 337 def self.exit_on_failure? true end |
Instance Method Details
#cancel ⇒ void
The method fetches the ID of the last tracking item using ‘@db.fetch_last_id`.
It checks if the last item is in progress by comparing ‘@db.item_status` with `:complete`.
If the last item is in progress, it deletes the item and prints a confirmation message using
If there is no active time tracking, it prints a message indicating that there is no active time tracking.
This method returns an undefined value.
Cancels the active time tracking session by deleting the last tracking item.
item and displaying a confirmation message.
‘delete_item_and_print_message(id, “Canceled active time tracking #id”)`.
320 321 322 323 324 325 |
# File 'lib/timet/application.rb', line 320 def cancel id = @db.fetch_last_id return puts 'There is no active time tracking' if @db.item_status == :complete (id, "Canceled active time tracking #{id}") end |
#delete(id) ⇒ void
The method first attempts to find the tracking item by its ID using ‘@db.find_item(id)`.
If the item is found, it displays the item details using ‘TimeReport.new(@db).show_row(item)`.
The method then prompts the user for confirmation using ‘TTY::Prompt.new.yes?(’Are you sure you want
If the user confirms, the method deletes the item and prints a confirmation message using
This method returns an undefined value.
Deletes a specific tracking item by its ID after confirming with the user.
and displaying a confirmation message.
to delete this entry?‘)`. `delete_item_and_print_message(id, “Deleted #id”)`.
296 297 298 299 300 301 302 303 304 |
# File 'lib/timet/application.rb', line 296 def delete(id) item = @db.find_item(id) return puts "No tracked time found for id: #{id}" unless item TimeReport.new(@db).show_row(item) return unless TTY::Prompt.new.yes?('Are you sure you want to delete this entry?') (id, "Deleted #{id}") end |
#edit(id = nil, field = nil, new_value = nil) ⇒ void
The method first attempts to find the tracking item by its ID using ‘@db.find_item(id)`.
If the item is found, it displays the current item details using ‘display_item(item)`.
If the field or new value is not provided, the user is prompted to select a field to edit and enter
The method then validates and updates the item using ‘validate_and_update(item, field, new_value)`.
Finally, it displays the updated item details using ‘display_item(updated_item)`.
This method returns an undefined value.
Edits a specific tracking item by its ID, allowing the user to modify fields such as notes, tag, start time, or end time.
If not provided, the user will be prompted to select a field. prompted to enter a new value.
and displaying the updated item.
a new value.
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/timet/application.rb', line 263 def edit(id = nil, field = nil, new_value = nil) id = @db.fetch_last_id if id.nil? item = @db.find_item(id) return puts "No tracked time found for id: #{id}" unless item display_item(item) if field.nil? || new_value.nil? field = select_field_to_edit new_value = prompt_for_new_value(item, field) end updated_item = validate_and_update(item, field, new_value) @db.update_item(id, field, updated_item[FIELD_INDEX[field]]) display_item(updated_item) end |
#resume(id = nil) ⇒ void
The method checks the status of the last tracking item using ‘@db.item_status`.
If the last item is in progress, it prints a message indicating that a task is currently being tracked.
If the last item is complete, it fetches the last item using ‘@db.find_item` or `@db.last_item`,
This method returns an undefined value.
Resumes the last tracking session if it was completed.
or providing feedback.
retrieves the tag and notes, and calls the ‘start` method to resume the tracking session.
193 194 195 196 197 198 199 200 |
# File 'lib/timet/application.rb', line 193 def resume(id = nil) case @db.item_status(id) when :in_progress puts 'A task is currently being tracked.' when :complete resume_complete_task(id) end end |
#start(tag, notes = nil, pomodoro = nil) ⇒ void
The method uses ‘TimeHelper.current_timestamp` to get the current timestamp for the start time.
The method calls ‘play_sound_and_notify` if a Pomodoro time is provided.
The method calls ‘summary` to generate a summary after inserting the tracking item.
This method returns an undefined value.
Starts a new tracking session with the given tag and optional notes.
This method initializes a new tracking session by inserting a new item into the database with the provided tag and optional notes. If a Pomodoro time is specified, it will also trigger a sound and notification after the specified time has elapsed.
defaults to the value in ‘options`. `options`.
playing a sound, sending a notification, and generating a summary.
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/timet/application.rb', line 133 def start(tag, notes = nil, pomodoro = nil) start_time = TimeHelper. notes = [:notes] || notes pomodoro = ([:pomodoro] || pomodoro).to_i return puts 'A task is currently being tracked.' unless VALID_STATUSES_FOR_INSERTION.include?(@db.item_status) @db.insert_item(start_time, tag, notes, pomodoro, start_time, start_time) DiscordNotifier.pomodoro_started(pomodoro) if pomodoro.positive? # Notify that a Pomodoro session has started play_sound_and_notify(pomodoro * 60, tag) if pomodoro.positive? summary end |
#stop ⇒ void
The method checks if the last tracking item is in progress by calling ‘@db.item_status`.
If the last item is in progress, it fetches the last item’s ID using ‘@db.fetch_last_id` and updates it
The method always generates a summary after stopping the tracking session.
This method returns an undefined value.
Stops the current tracking session if there is one in progress. After stopping the tracking session, it displays a summary of the tracked time.
and generating a summary.
with the current timestamp.
160 161 162 163 164 165 166 167 |
# File 'lib/timet/application.rb', line 160 def stop return unless @db.item_status == :in_progress last_id = @db.fetch_last_id @db.find_item(last_id) # Fetch the item to get pomodoro duration @db.update_item(last_id, 'end', TimeHelper.) summary end |
#summary(time_scope = nil, tag = nil) ⇒ void
This method returns an undefined value.
Generates a summary of tracking items based on the provided time_scope and tag, and optionally exports the summary to a CSV file and/or an iCalendar file.
and exporting the report.
224 225 226 227 228 229 230 231 |
# File 'lib/timet/application.rb', line 224 def summary(time_scope = nil, tag = nil) = (time_scope, tag) report = TimeReport.new(@db, ) display_and_export_report(report, ) return unless [:report] report.print_tag_explanation_report end |
#sync ⇒ Object
354 355 356 357 358 |
# File 'lib/timet/application.rb', line 354 def sync puts 'Syncing database with remote storage...' puts 'Sync method called' DatabaseSyncHelper.sync(@db, BUCKET) end |