timet 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 86b753ecfeaeaa477431908419a5a20c1446d146f52dbd72a2a2cda1e4664873
4
+ data.tar.gz: e5a13c6eef4c17aacbb907df19731cd2f736ddb3ef9b2155f9c4c35416123ffc
5
+ SHA512:
6
+ metadata.gz: 9a94bc189c1c507fd918d229a020dcdf674e5d7b0ce6cff127e9968d727c514aaecaa1c35d8c1cdd1f722505c7d01bca9fbf5ec187d369528aea35f3120c61b2
7
+ data.tar.gz: b67dd26bd7e5a8725051ef657cde914f2b2b6ef00771af08c86c2b04da0762a9df8cb9ea5e58296c74928258e808eb23d88ee27a733c2a5a3a5fabd5bef96d50
data/.reek.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ detectors:
3
+ TooManyStatements:
4
+ max_statements: 7
5
+ UncommunicativeVariableName:
6
+ enabled: false
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,17 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ NewCops: enable
4
+ SuggestExtensions: false
5
+ Exclude:
6
+ - timet.gemspec
7
+
8
+ Metrics/MethodLength:
9
+ Max: 12
10
+
11
+ Style/StringLiterals:
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ EnforcedStyle: double_quotes
16
+
17
+ require: rubocop-rspec
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-08-01
4
+
5
+ - Initial release
@@ -0,0 +1,131 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at [here](https://github.com/frankvielma/timet/blob/main/timet.gemspec#L9) .
63
+ All complaints will be reviewed and investigated promptly and fairly.
64
+
65
+ All community leaders are obligated to respect the privacy and security of the
66
+ reporter of any incident.
67
+
68
+ ## Enforcement Guidelines
69
+
70
+ Community leaders will follow these Community Impact Guidelines in determining
71
+ the consequences for any action they deem in violation of this Code of Conduct:
72
+
73
+ ### 1. Correction
74
+
75
+ **Community Impact**: Use of inappropriate language or other behavior deemed
76
+ unprofessional or unwelcome in the community.
77
+
78
+ **Consequence**: A private, written warning from community leaders, providing
79
+ clarity around the nature of the violation and an explanation of why the
80
+ behavior was inappropriate. A public apology may be requested.
81
+
82
+ ### 2. Warning
83
+
84
+ **Community Impact**: A violation through a single incident or series of
85
+ actions.
86
+
87
+ **Consequence**: A warning with consequences for continued behavior. No
88
+ interaction with the people involved, including unsolicited interaction with
89
+ those enforcing the Code of Conduct, for a specified period of time. This
90
+ includes avoiding interactions in community spaces as well as external channels
91
+ like social media. Violating these terms may lead to a temporary or permanent
92
+ ban.
93
+
94
+ ### 3. Temporary Ban
95
+
96
+ **Community Impact**: A serious violation of community standards, including
97
+ sustained inappropriate behavior.
98
+
99
+ **Consequence**: A temporary ban from any sort of interaction or public
100
+ communication with the community for a specified period of time. No public or
101
+ private interaction with the people involved, including unsolicited interaction
102
+ with those enforcing the Code of Conduct, is allowed during this period.
103
+ Violating these terms may lead to a permanent ban.
104
+
105
+ ### 4. Permanent Ban
106
+
107
+ **Community Impact**: Demonstrating a pattern of violation of community
108
+ standards, including sustained inappropriate behavior, harassment of an
109
+ individual, or aggression toward or disparagement of classes of individuals.
110
+
111
+ **Consequence**: A permanent ban from any sort of public interaction within the
112
+ community.
113
+
114
+ ## Attribution
115
+
116
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
117
+ version 2.1, available at
118
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
119
+
120
+ Community Impact Guidelines were inspired by
121
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
122
+
123
+ For answers to common questions about this code of conduct, see the FAQ at
124
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
125
+ [https://www.contributor-covenant.org/translations][translations].
126
+
127
+ [homepage]: https://www.contributor-covenant.org
128
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
129
+ [Mozilla CoC]: https://github.com/mozilla/diversity
130
+ [FAQ]: https://www.contributor-covenant.org/faq
131
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Frank Vielma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,190 @@
1
+ ![timet workflow](https://github.com/frankvielma/timet/actions/workflows/ci.yml/badge.svg)
2
+
3
+
4
+ # Timet
5
+
6
+ Timet is a command line time tracking with reports. It's simple to track your hours for work with Timet, whether you're curious about how you allocate your time.
7
+
8
+ Timet utilizes SQLite to store your time tracking data. This means your data is stored locally and securely, with no need for external databases or cloud storage. This makes Timet lightweight, fast, and perfect for users who value privacy and control over their data.
9
+
10
+ While a YAML file might seem like a simple option for storing time tracking data, Timet leverages SQLite for several key advantages:
11
+
12
+ - Structured Data
13
+ - Scalability
14
+ - Data Integrity
15
+ - Querying and Reporting
16
+
17
+ In addition, if possible, export your time tracking data to CSV for analysis and sharing.
18
+
19
+ ## Requirements
20
+
21
+ * Ruby version: >= 3.0.0
22
+ * sqlite3: > 1.7
23
+
24
+
25
+ Old versions of Ruby and Sqlite:
26
+
27
+ * [Ruby >= 2.7](https://github.com/frankvielma/timet/tree/ruby-2.7.0)
28
+
29
+ * [Ruby >= 2.4](https://github.com/frankvielma/timet/tree/ruby-2.4.0)
30
+
31
+
32
+
33
+ ## Installation
34
+
35
+ Install the gem and add to the application's Gemfile by executing:
36
+
37
+ $ bundle add timet
38
+
39
+ If bundler is not being used to manage dependencies, install the gem by executing:
40
+
41
+ $ gem install timet
42
+
43
+ ## Usage
44
+
45
+ Using timet or tt as an alias is possible
46
+
47
+ - **timet start [tag]**: Starts tracking time for a task labeled with the provided [tag]. Example:
48
+ ```shell
49
+ timet start task1
50
+
51
+ or
52
+
53
+ tt start task1
54
+ ```
55
+
56
+ ```
57
+ Tracked time report:
58
+ +-------+--------+---------------------+-------------------+------------+
59
+ | Id | Tag | Start Time | End Time | Duration |
60
+ +-------+--------+---------------------+-------------------+------------+
61
+ | 1 | task1 | 2024-08-09 14:55:07 | - | 00:00:00 |
62
+ +-------+--------+---------------------+-------------------+------------+
63
+ | Total: | 00:00:00 |
64
+ +-------+--------+---------------------+-------------------+------------+
65
+ ```
66
+
67
+ - **timet stop**: Stops tracking the current task, records the elapsed time, and displays the total time spent on all tasks.
68
+
69
+ ```shell
70
+ timet stop
71
+ ```
72
+
73
+ ```
74
+ Tracked time report:
75
+ +-------+--------+---------------------+---------------------+------------+
76
+ | Id | Tag | Start Time | End Time | Duration |
77
+ +-------+--------+---------------------+--------------------+-------------+
78
+ | 1 | task1 | 2024-08-09 14:55:07 | 2024-08-09 14:56:20 | 00:01:13 |
79
+ +-------+--------+---------------------+---------------------+------------+
80
+ | Total: | 00:01:13 |
81
+ +-------+--------+---------------------+---------------------+------------+
82
+ ```
83
+
84
+ - **timet resume**: It allows users to quickly resume tracking a task that was previously in progress.
85
+ ```
86
+ Tracked time report:
87
+ +-------+--------+---------------------+---------------------+------------+
88
+ | Id | Tag | Start Time | End Time | Duration |
89
+ +-------+--------+---------------------+--------------------+-------------+
90
+ | 2 | task1 | 2024-08-09 16:45:12 | - | 00:00:00 |
91
+ | 1 | task1 | 2024-08-09 14:55:07 | 2024-08-09 14:56:20 | 00:01:13 |
92
+ +-------+--------+---------------------+---------------------+------------+
93
+ | Total: | 00:01:13 |
94
+ +-------+--------+---------------------+---------------------+------------+
95
+ ```
96
+
97
+ - **timet summary today (t)**: Display a report of tracked time for today.
98
+
99
+ ```shell
100
+ timet summary today
101
+
102
+ or
103
+
104
+ tt su t
105
+ ```
106
+
107
+ - **timet summary yesterday (y)**: Display a report of tracked time for yesterday.
108
+
109
+ ```shell
110
+ timet summary yesterday
111
+
112
+ or
113
+
114
+ tt su y
115
+ ```
116
+
117
+ - **timet summary week (w)**: Display a report of tracked time for the week.
118
+
119
+ ```shell
120
+ timet summary week
121
+
122
+ or
123
+
124
+ tt su w
125
+ ```
126
+
127
+ - **timet summary resume (r)**: Resume tracking the last task.
128
+
129
+ ```shell
130
+ timet summary resume
131
+
132
+ or
133
+
134
+ tt su r
135
+ ```
136
+
137
+ - **timet summary resume (r)**: Resume tracking the last month.
138
+
139
+ ```shell
140
+ timet summary month
141
+
142
+ or
143
+
144
+ tt su m
145
+ ```
146
+
147
+ - **timet su t --csv=[filename]**: Display a report of tracked time for today and export it to filename.csv
148
+
149
+ ```shell
150
+ tt su t --csv=summary_today.csv
151
+ ```
152
+
153
+ - **timet delete [id]**: Delete a task
154
+
155
+ ```shell
156
+ timet delete [id]
157
+
158
+ or
159
+
160
+ tt d [id]
161
+ ```
162
+
163
+ - **timet cancel**: Cancel active time tracking
164
+
165
+ ```shell
166
+ timet cancel
167
+
168
+ or
169
+
170
+ tt c
171
+ ```
172
+
173
+
174
+ ## Development
175
+
176
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
177
+
178
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
179
+
180
+ ## Contributing
181
+
182
+ Bug reports and pull requests are welcome on GitHub at https://github.com/frankvielma/timet. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/frankvielma/timet/blob/master/CODE_OF_CONDUCT.md).
183
+
184
+ ## License
185
+
186
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
187
+
188
+ ## Code of Conduct
189
+
190
+ Everyone interacting in the Timet project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/frankvielma/timet/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/timet ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative "../lib/timet"
5
+
6
+ Timet::Application.start(ARGV)
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "version"
4
+ require "thor"
5
+ require "tty-prompt"
6
+
7
+ module Timet
8
+ # Tracks time spent on various tasks.
9
+ class Application < Thor
10
+ def initialize(*args)
11
+ super
12
+ @db = Timet::Database.new
13
+ end
14
+
15
+ desc "start [tag]", "start time tracking"
16
+ def start(tag)
17
+ start = Time.now.to_i
18
+ @db.insert_item(start, tag) if %i[no_items complete].include?(@db.last_item_status)
19
+ summary
20
+ end
21
+
22
+ desc "stop", "stop time tracking"
23
+ def stop
24
+ stop = Time.now.to_i
25
+ @db.update(stop) if @db.last_item_status == :incomplete
26
+ result = @db.last_item
27
+
28
+ return unless result
29
+
30
+ summary
31
+ end
32
+
33
+ desc "resume (r)", "resume last task"
34
+ def resume
35
+ if @db.last_item_status == :incomplete
36
+ puts "A task is currently being tracked."
37
+ else
38
+ last_task = @db.last_item&.last
39
+ start last_task if last_task
40
+ end
41
+ end
42
+
43
+ desc "summary (su) [filter] [tag] --csv=csv_filename",
44
+ "Display a summary of tracked time filter => [today (t), yesterday (y), week (w), month (m)] [tag] and export to csv_filename"
45
+ option :csv, type: :string, desc: "Export to CSV file"
46
+ def summary(filter = nil, tag = nil)
47
+ csv_filename = options[:csv]
48
+ summary = TimeReport.new(@db, filter, tag, csv_filename)
49
+ summary.display
50
+ summary.export_sheet if csv_filename
51
+ end
52
+
53
+ desc "delete (d) [id]", "delete a task"
54
+ def delete(id)
55
+ item = @db.find_item(id)
56
+ return puts "No tracked time found for id: #{id}" unless item
57
+
58
+ TimeReport.new(@db, nil, nil).row(item)
59
+ return unless TTY::Prompt.new.yes?("Are you sure you want to delete this entry?")
60
+
61
+ delete_item_and_print_message(id, "Deleted #{id}")
62
+ end
63
+
64
+ desc "cancel (c)", "cancel active time tracking"
65
+ def cancel
66
+ id = @db.fetch_last_id
67
+ return puts "There is no active time tracking" if @db.last_item_status == :complete
68
+
69
+ delete_item_and_print_message(id, "Canceled active time tracking #{id}")
70
+ end
71
+
72
+ def self.exit_on_failure?
73
+ true
74
+ end
75
+
76
+ private
77
+
78
+ def delete_item_and_print_message(id, message)
79
+ @db.delete_item(id)
80
+ puts message
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sqlite3"
4
+
5
+ module Timet
6
+ # Provides database access for managing time tracking data.
7
+ class Database
8
+ DEFAULT_DATABASE_PATH = File.join(Dir.home, ".timet.db")
9
+
10
+ def initialize(database_path = DEFAULT_DATABASE_PATH)
11
+ @db = SQLite3::Database.new(database_path)
12
+ create_table
13
+ end
14
+
15
+ # Creates the items table if it doesn't already exist
16
+ def create_table
17
+ execute_sql(<<-SQL)
18
+ CREATE TABLE IF NOT EXISTS items (
19
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
20
+ start INTEGER,
21
+ end INTEGER,
22
+ tag TEXT
23
+ );
24
+ SQL
25
+ end
26
+
27
+ # Inserts a new item into the items table
28
+ def insert_item(start, tag)
29
+ execute_sql("INSERT INTO items (start, tag) VALUES (?, ?)", [start, tag])
30
+ end
31
+
32
+ # Updates the end time of the last item
33
+ def update(stop)
34
+ last_id = fetch_last_id
35
+ return unless last_id
36
+
37
+ execute_sql("UPDATE items SET end = ? WHERE id = ?", [stop, last_id])
38
+ end
39
+
40
+ def delete_item(id)
41
+ execute_sql("DELETE FROM items WHERE id = #{id}")
42
+ end
43
+
44
+ # Fetches the ID of the last inserted item
45
+ def fetch_last_id
46
+ result = execute_sql("SELECT id FROM items ORDER BY id DESC LIMIT 1").first
47
+ result ? result[0] : nil
48
+ end
49
+
50
+ def last_item
51
+ execute_sql("SELECT * FROM items ORDER BY id DESC LIMIT 1").first
52
+ end
53
+
54
+ def last_item_status
55
+ result = execute_sql("SELECT id, end FROM items ORDER BY id DESC LIMIT 1")
56
+ StatusHelper.determine_status(result)
57
+ end
58
+
59
+ def find_item(id)
60
+ execute_sql("select * from items where id=#{id}").first
61
+ end
62
+
63
+ # Calculates the total time elapsed since the last recorded time.
64
+ def total_time
65
+ last_item = execute_sql("SELECT * FROM items ORDER BY id DESC LIMIT 1").first
66
+ return "00:00:00" unless last_item
67
+
68
+ start_time = last_item[1]
69
+ end_time = last_item[2]
70
+
71
+ total_seconds = end_time ? end_time - start_time : Time.now.to_i - start_time
72
+ seconds_to_hms(total_seconds)
73
+ end
74
+
75
+ def all_items
76
+ execute_sql("SELECT * FROM items where start >= '#{Date.today.to_time.to_i}' ORDER BY id DESC")
77
+ end
78
+
79
+ # Executes a SQL query and returns the result
80
+ def execute_sql(sql, params = [])
81
+ @db.execute(sql, params)
82
+ rescue SQLite3::SQLException => e
83
+ puts "Error: #{e.message}"
84
+ []
85
+ end
86
+
87
+ # Closes the database connection
88
+ def close
89
+ @db&.close
90
+ end
91
+
92
+ # Converts a given number of seconds into a human-readable HH:MM:SS format.
93
+ def seconds_to_hms(seconds)
94
+ hours, remainder = seconds.divmod(3600)
95
+ minutes, seconds = remainder.divmod(60)
96
+
97
+ format "%<hours>02d:%<minutes>02d:%<seconds>02d", hours: hours, minutes: minutes, seconds: seconds
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Determines the status of a time tracking result based on the presence and end time of items.
4
+ module StatusHelper
5
+ def self.determine_status(result)
6
+ return :no_items if result.empty?
7
+
8
+ last_item_end = result.first[1]
9
+ return :incomplete unless last_item_end
10
+
11
+ :complete
12
+ end
13
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This module provides helper functions for working with time and dates.
4
+ # It includes methods for:
5
+ # - formatting timestamps into a specific format
6
+ # - calculating the duration between two timestamps
7
+ # - converting a Date object to a timestamp
8
+ module TimeHelper
9
+ def self.format_time(timestamp)
10
+ timestamp&.then { |time| Time.at(time).strftime("%Y-%m-%d %H:%M:%S").ljust(19) }
11
+ end
12
+
13
+ def self.calculate_duration(start_time, end_time)
14
+ start_time = Time.at(start_time)
15
+ end_time = end_time ? Time.at(end_time) : Time.now
16
+
17
+ (end_time - start_time).to_i
18
+ end
19
+
20
+ def self.date_to_timestamp(date)
21
+ date.to_time.to_i
22
+ end
23
+
24
+ def self.calculate_end_time(start_date, end_date)
25
+ end_date ||= start_date + 1
26
+ date_to_timestamp(end_date)
27
+ end
28
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "csv"
5
+ require_relative "time_helper"
6
+ require_relative "status_helper"
7
+
8
+ module Timet
9
+ # The TimeReport class is responsible for displaying a report of tracked time
10
+ # entries. It allows filtering the report by time periods (today, yesterday,
11
+ # week) and displays a formatted table with the relevant information.
12
+ class TimeReport
13
+ attr_reader :db, :items, :filename
14
+
15
+ def initialize(db, filter, tag, csv)
16
+ @db = db
17
+ @filename = csv
18
+ @items = filter ? filter_items(filter, tag) : @db.all_items
19
+ end
20
+
21
+ def display
22
+ return puts "No tracked time found for the specified filter." if items.empty?
23
+
24
+ format_table_header
25
+ items.each do |item|
26
+ display_time_entry(item)
27
+ end
28
+ puts format_table_separator
29
+ total
30
+ end
31
+
32
+ def row(item)
33
+ format_table_header
34
+ display_time_entry(item)
35
+ puts format_table_separator
36
+ total
37
+ end
38
+
39
+ def export_sheet
40
+ header = %w[ID Start End Tag]
41
+
42
+ CSV.open("#{filename}.csv", "w") do |csv|
43
+ csv << header
44
+
45
+ items.each do |row|
46
+ # Convert start and end times from timestamps to ISO 8601 format
47
+ start_time = Time.at(row[1]).strftime("%Y-%m-%d %H:%M:%S")
48
+ end_time = Time.at(row[2]).strftime("%Y-%m-%d %H:%M:%S")
49
+
50
+ # Write the row with formatted times
51
+ csv << [row[0], start_time, end_time, row[3]]
52
+ end
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def display_time_entry(item)
59
+ return puts "Missing time entry data." unless item
60
+
61
+ id, start_time_value, end_time_value, tag_name = item
62
+ duration = TimeHelper.calculate_duration(start_time_value, end_time_value)
63
+ start_time = TimeHelper.format_time(start_time_value)
64
+ end_time = TimeHelper.format_time(end_time_value) || "-".rjust(19)
65
+ puts format_table_row(id, tag_name[0..5], start_time, end_time, duration)
66
+ end
67
+
68
+ def total
69
+ total = @items.map do |item|
70
+ TimeHelper.calculate_duration(item[1], item[2])
71
+ end.sum
72
+ puts "|#{" " * 52}\033[94mTotal: | #{@db.seconds_to_hms(total).rjust(10)} |\033[0m"
73
+ puts format_table_separator
74
+ end
75
+
76
+ def format_table_header
77
+ header = <<~TABLE
78
+ Tracked time report:
79
+ #{format_table_separator}
80
+ \033[32m| Id | Tag | Start Time | End Time | Duration |\033[0m
81
+ #{format_table_separator}
82
+ TABLE
83
+ puts header
84
+ end
85
+
86
+ def format_table_separator
87
+ "+-------+--------+---------------------+---------------------+------------+"
88
+ end
89
+
90
+ def format_table_row(*row)
91
+ id, tag, start_time, end_time, duration = row
92
+ "| #{id.to_s.rjust(5)} | #{tag.ljust(6)} | #{start_time} | #{end_time} | " \
93
+ "#{@db.seconds_to_hms(duration).rjust(10)} |"
94
+ end
95
+
96
+ def filter_items(filter, tag)
97
+ today = Date.today
98
+ case filter
99
+ when "today", "t"
100
+ filter_by_date_range(today, nil, tag)
101
+ when "yesterday", "y"
102
+ filter_by_date_range(today - 1, nil, tag)
103
+ when "week", "w"
104
+ filter_by_date_range(today - 7, today + 1, tag)
105
+ when "month", "m"
106
+ filter_by_date_range(today - 30, today + 1, tag)
107
+ else
108
+ puts "Invalid filter. Supported filters: today, yesterday, week"
109
+ []
110
+ end
111
+ end
112
+
113
+ def filter_by_date_range(start_date, end_date = nil, tag = nil)
114
+ start_time = TimeHelper.date_to_timestamp(start_date)
115
+ end_time = TimeHelper.calculate_end_time(start_date, end_date)
116
+ query = "start >= #{start_time} and start < #{end_time} and tag like '%#{tag}%'"
117
+ @db.execute_sql(
118
+ "select * from items where #{query} ORDER BY id DESC"
119
+ )
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Timet
4
+ VERSION = "0.1.0"
5
+ end
data/lib/timet.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "timet/application"
4
+ require_relative "timet/database"
5
+ require_relative "timet/time_report"
data/sig/timet.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Timet
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Frank Vielma
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-prompt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '1.7'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '2'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '1.7'
61
+ description: It's a time tracking app that keeps track of how long you spend doing
62
+ different things.
63
+ email:
64
+ - frankvielma@gmail.com
65
+ executables:
66
+ - timet
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - ".reek.yml"
71
+ - ".rspec"
72
+ - ".rubocop.yml"
73
+ - CHANGELOG.md
74
+ - CODE_OF_CONDUCT.md
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - bin/timet
79
+ - lib/timet.rb
80
+ - lib/timet/application.rb
81
+ - lib/timet/database.rb
82
+ - lib/timet/status_helper.rb
83
+ - lib/timet/time_helper.rb
84
+ - lib/timet/time_report.rb
85
+ - lib/timet/version.rb
86
+ - sig/timet.rbs
87
+ homepage: https://github.com/frankvielma/timet/blob/main/README.md
88
+ licenses:
89
+ - MIT
90
+ metadata:
91
+ homepage_uri: https://github.com/frankvielma/timet/blob/main/README.md
92
+ source_code_uri: https://github.com/frankvielma/timet
93
+ changelog_uri: https://github.com/frankvielma/timet/blob/main/CHANGELOG.md
94
+ rubygems_mfa_required: 'true'
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 3.0.0
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubygems_version: 3.3.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Command line time tracking with reports
114
+ test_files: []