ptimelog 0.12.0 → 0.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd30deb9f9246b10264f5e9af3a35c279ffee1c0766947b47d52c4deaf00a608
4
- data.tar.gz: a544d9b97ecca711bd66dc6ecf9325d3110601b6bbaa2b7a499ffda152be4916
3
+ metadata.gz: 0c9d0fcc6e0b89b70b4be7765118af54a493aa561dc6336fb03e070999dbdf95
4
+ data.tar.gz: 3306a2c87d164e509cbc102d7173fb7480d00165a2c8fd56c0c5b1d05e0e4715
5
5
  SHA512:
6
- metadata.gz: 63dd6436e43358e1e59ba11277c470f37880745f7033001c8776d427d2656827007fe4a231551466bcf9328de3532ef04c621b324ccce14dfe253c7e91a93c62
7
- data.tar.gz: 81e2c263b3785429f79ac9df991ecb6c19517620efd6351cafcba13450a7691ed3415545825e0c488a635ea8478099224fc05de383ff5f2d45ce9c6069ead2af
6
+ metadata.gz: 5017e8bd76814d2119faf4a9bd77924d034f7cacfd7496e92a6d8a6d882e124f079ba47cf5319579a252c5c9505ab329c93a97b543d2593c437311e166540429
7
+ data.tar.gz: 44c779a4aeafce366b344231591a0c53bc9101e76116e25866656ffc8c31b0d936b2a545db0ff6b7a11f5c53d757e3c7779880bbaad9e9c69b97a91d5d47605a
data/AGENTS.md CHANGED
@@ -3,6 +3,7 @@
3
3
  - written in Ruby
4
4
  - Tests with RSpec, execute `rspec` to run them
5
5
  - Code-Style is checked with RuboCop, run `rubocop` to check
6
+ - the default rake-task (`rake`) runs both rspec and rubocop in an row
6
7
 
7
8
  ## Ideas
8
9
 
@@ -38,5 +39,5 @@ This describes a time-entry from 10:00 to 11:00 o'clock. The entry's ticket is "
38
39
  - run `rspec` to ensure that
39
40
  - if any spec fails, fix the new implementation until all specs pass
40
41
  - Lastly, run `rubocop` to ensure the code-style is good.
41
- - verify completeness by running `rspec` and `rubocop`.
42
+ - verify completeness by running `rake` (runs `rubocop` and `rspec` together).
42
43
  - Propose a commit-message, summarizing the need for the change
data/README.md CHANGED
@@ -28,7 +28,7 @@ small tooling to transfer timelog-entries from gtimelog's timelog.txt or the tas
28
28
  - [x] avoid duplicate entries
29
29
  - [x] start/end time as indicator?
30
30
  - [x] offer rounding times to the next 5, 10 or 15 minutes
31
- - [x] allow to add entries from the command-line
31
+ - [-] allow to add entries from the command-line (removed in 0.13.0)
32
32
  - [ ] handle time-account and billable better
33
33
  - [ ] import time-accounts from ptime (https://time.puzzle.ch/work_items/search.json?q=search%20terms)
34
34
  - [ ] with a dedicated cli?
@@ -48,8 +48,8 @@ small tooling to transfer timelog-entries from gtimelog's timelog.txt or the tas
48
48
  - [ ] time-account/billable/metadata lookup from a note
49
49
  - [x] support show-command: output the entries of a day
50
50
  - [x] support upload-command: send the entries of a day to ptime
51
- - [ ] support add-command: add a new entry to the current day
52
51
  - [ ] support "last"-day for obsidian
52
+ - [x] filter the entries by tag
53
53
 
54
54
  ## Installation
55
55
 
@@ -69,10 +69,10 @@ there.
69
69
  Currently supported actions are
70
70
 
71
71
  - show
72
+ - summarize
72
73
  - upload
73
74
  - confirm
74
75
  - edit
75
- - add
76
76
  - version
77
77
  - help
78
78
  - info
@@ -88,10 +88,68 @@ For reusability in a shell-history the following keywords are supported:
88
88
  - today
89
89
  - yesterday
90
90
  - all
91
+ - a weekday, full or shortened: monday, mon, tue, ...
91
92
 
92
93
  Currently, you need to pass a day. The previously exisiting "last" worked fine
93
94
  to timelog.txt, but not for Obsidian.
94
95
 
96
+ #### Whole months
97
+
98
+ A month-name selects every day of that month, `month` the current one:
99
+
100
+ $ ptimelog show october
101
+ $ ptimelog show oct
102
+ $ ptimelog show month
103
+
104
+ Like a weekday, a month looks backwards: in November 2026, `december` is the
105
+ one of 2025. The current month counts, unfinished as it is. If you want the
106
+ current month, the keyword "month" is there. This nicely highlight that at
107
+ least germanic languages do not have the monthly equivalent of today. "tomonth"
108
+ would have a candidate, but... yeah, fixing that is outside of the scope of
109
+ this project (for now, at least).
110
+
111
+ The output stays grouped by day, and so does `-d`, which also summarizes per
112
+ ticket within a day, not across the month. `upload october` opens a browser-tab
113
+ per entry of the whole month; `confirm october` asks per day and is therefore
114
+ both more annoying and potentially gentler on your RAM. I know, boring...
115
+
116
+ :warn: Only the Obsidian-Datasource supports this. The timelog.txt-Datasource
117
+ refuses with an error instead of silently showing every day it knows. Use "all"
118
+ for now.
119
+
120
+ ### Summarizing a range
121
+
122
+ `show` answers "what did I do, and when". For reporting and invoicing, the
123
+ question is "how much time went into what", and a day is no useful group for
124
+ that. That is what `summarize` is for:
125
+
126
+ $ ptimelog summarize october
127
+ October 2026
128
+ ----------
129
+ 03:00 | HIT-4711: Release | hit client
130
+ 02:00 | HIT-2342: Zeug machen | hit client topic
131
+ 01:00 | Rechnung prüfen
132
+ ----------
133
+ Total work done: 06:00 hours
134
+ ----------------------------
135
+
136
+ There is one title, which is the requested range spelled out, and one total to
137
+ cross-check against. No times of day are listed, they say nothing about an
138
+ amount of work. Durations are implied, so there is no `-d`; `-t` filters as
139
+ everywhere else.
140
+
141
+ Two entries are merged when their ticket **and** their tags are the same, no
142
+ matter which day they were tracked on. The tags decide the time-account, so
143
+ entries which differ in them have to stay apart. An entry without a ticket
144
+ carries nothing to merge it by and therefore stays on its own — if you want it
145
+ joined, add the metadata to the task.
146
+
147
+ This also means `summarize` can split lines differently than `show -d`, which
148
+ merges an untagged entry into a tagged one of the same ticket. `show` is
149
+ unchanged.
150
+
151
+ A single day works as well, it is simply its own title.
152
+
95
153
  ### Edit-Identifier
96
154
 
97
155
  When the action is "edit", the next argument is treated as script that should
@@ -104,27 +162,44 @@ edits an inferer-script, which is the thing that can still be edited.
104
162
 
105
163
  Otherwise, a script to determine the time-account is loaded.
106
164
 
165
+ ### Filtering by Tag
107
166
 
108
- ### Adding entries
167
+ All actions that list entries (currently: show, summarize, upload and confirm)
168
+ take a `-t`/`--tag` parameter to limit the entries to those tagged with it.
109
169
 
110
- In order to add entries with the ptimelog-cli, the complete entry needs to be
111
- quoted on the command-line to count as one argument.
170
+ $ ptimelog show today -t hit
112
171
 
113
- $ ptimelog add 'ticket 1337: Implement requirements -- client coding'
172
+ It can be repeated to filter more intensely. All mentioned tags must apply.
114
173
 
115
- While this requires some knowledge of the file-format, it is no different than
116
- entering the same string in gTimelog. For now, the entry is added to the
117
- timelog.txt as it is passed. By default, the date/time added to the entry is
118
- the one when the command is executed.
174
+ $ ptimelog show today -t hit -t mail
119
175
 
120
- You can prefix a positive or negative signed number to slightly skew the entry
121
- (think: '-5 meeting' or '+5 lunch \*\*') or even set a precise time ('10:30
122
- meeting').
176
+ A day without a single matching entry is skipped instead of being printed as
177
+ empty, which keeps `ptimelog show all -t hit` readable.
123
178
 
124
- $ ptimelog add '-5 meeting: Discuss requirements -- client planning'
125
179
 
126
- :warn: Not available for the Obsidian-Datasource. Tracked time belongs to
127
- a task in Operon, and only Operon may write it.
180
+ ### Adding entries (removed)
181
+
182
+ Up to 0.12.0 there was an add-action that appended an entry to the
183
+ timelog.txt:
184
+
185
+ $ ptimelog add 'ticket 1337: Implement requirements -- client coding'
186
+
187
+ It is gone since 0.13.0. It had been broken for a while: no datasource
188
+ implemented the write it called, so every invocation ended in a
189
+ `NoMethodError`. With Obsidian as datasource it cannot come back in that
190
+ shape at all — tracked time belongs to a task in Operon, and only Operon may
191
+ write it. This is a self-imposed limitation, of course.
192
+
193
+ Patches are welcome, but be warned that this is not a small job. See
194
+ [docs/add-command.md](docs/add-command.md): section 9 describes what a rebuild
195
+ for the timelog.txt backend needs, sections 1 to 8 why the Obsidian side is
196
+ hard. Tracking time in Operon needs a task with correct tracking attributes.
197
+ Adding those is not impossible, but to cumbersome for now, for me.
198
+
199
+ It might come back to support automating events like closing or opening the
200
+ laptop (via a lidswitch-script) and locking or unlocking the screen (via a hook
201
+ in your screen-locker). But this requires some thought and careful planning.
202
+ Currently, I am happy enough using drag&drop in Operon.
128
203
 
129
204
  ### Showing the Version
130
205
 
@@ -152,6 +227,9 @@ These scripts should be located in `~/.config/ptimelog/inferers/` and be named
152
227
  like the first tag used. The script gets the ticket, the description and all
153
228
  remaining tags passed as arguments.
154
229
 
230
+ Only the first tag picks the script. `--tag` does not care about that order
231
+ and matches a tag at any position.
232
+
155
233
  The output of the script should be the ID of the time-account and the
156
234
  billable-state as "true" or "false". Both items need to be separated by
157
235
  whitespace, so you can output those two on the same line or on different lines.
@@ -184,6 +262,11 @@ space-separated string of tags, identical to what the dayplanner-entries used
184
262
  to carry after their `--`. The first tag selects the inferer-script, the rest
185
263
  are passed to it. A task without that key is still listed, but has no account.
186
264
 
265
+ Both keys are inherited: a subtask which carries none of them takes them
266
+ from its parent task, up the whole chain. So the ticket can sit once in the
267
+ frontmatter of a file task, while the time is tracked on its subtasks. A key
268
+ on the subtask itself always wins.
269
+
187
270
  If your vault already uses other names for these keys, map them with
188
271
  `operon_custom_keys`. A key you leave out keeps its default:
189
272
 
@@ -5,20 +5,17 @@ implemented yet.
5
5
 
6
6
  ## 1. Current state
7
7
 
8
- - **Decision:** with Obsidian/Operon as datasource, `add` stays disabled for now.
9
- - **`add` crashes with both backends today.** `Command::Add#execute` calls
10
- `backend.add(entry)` (`lib/ptimelog/command/add.rb:28`). Neither
11
- `Ptimelog::Operon` nor `Ptimelog::Timelog` defines `add`; the timelog
12
- version is commented out (`lib/ptimelog/timelog.rb:50`). So `ptimelog add …`
13
- reaches `previous_entry`, then fails with `NoMethodError`.
14
- - **Already built and committed:** `Operon#previous_entry`
15
- (`lib/ptimelog/operon.rb:44`). It returns the running timer's start, else the
8
+ - **Decision:** `add` is removed. It crashed with both backends — `Command::Add`
9
+ called `backend.add(entry)`, which neither `Ptimelog::Operon` nor
10
+ `Ptimelog::Timelog` implemented — so no working behaviour was lost. The
11
+ command, its spec and the commented-out write-methods in `Timelog` are gone
12
+ since 0.13.0.
13
+ - **The timelog.txt side is a separate, much smaller job.** See section 9.
14
+ - **Already built and kept:** `Operon#previous_entry`
15
+ (`lib/ptimelog/operon.rb:51`). It returns the running timer's start, else the
16
16
  end of the day's last session, else `operon_start_time` (default `08:00`).
17
- The entry is hidden (`start **`) and has specs.
18
- - **README** already states that `add` is not available for the Obsidian
19
- datasource.
20
- - **Tests:** the general-flow spec in `spec/ptimelog/command/add_spec.rb` is
21
- commented out, and the `with obsidian+operon as backend` context is empty.
17
+ The entry is hidden (`start **`) and has specs. `Timelog.previous_entry` is
18
+ kept as well. Both are the hook a reimplementation starts from.
22
19
 
23
20
  ## 2. Hard limits from the Operon docs
24
21
 
@@ -134,7 +131,109 @@ Three ways to determine the destination:
134
131
 
135
132
  ## 8. Possible small step before the full design
136
133
 
137
- Today "disabled" means a `NoMethodError`. A one-method `Operon#add` raising a
138
- `Ptimelog::Error` ("adding entries is not supported for the Obsidian
139
- datasource") would turn that into a clear message — same pattern as
134
+ Today "disabled" means `ptimelog add` is an unknown command. Once `add` comes
135
+ back for timelog.txt (section 9), Obsidian needs an explicit refusal: a
136
+ one-method `Operon#add` raising a `Ptimelog::Error` ("adding entries is not
137
+ supported for the Obsidian datasource") — same pattern as
140
138
  `Operon::VaultNotFound`, with one spec.
139
+
140
+ ## 9. Rebuilding `add` for the timelog.txt backend
141
+
142
+ The Obsidian/Operon side above is the hard problem. The timelog.txt side is
143
+ not: it is one appended line. `Command::Add` and
144
+ `spec/ptimelog/command/add_spec.rb` were deleted in 0.13.0, together with the
145
+ commented-out write-methods in `lib/ptimelog/timelog.rb`. What follows is what
146
+ a patch has to restore.
147
+
148
+ ### The old code
149
+
150
+ The last version that actually wrote to timelog.txt:
151
+
152
+ git show fe45a2a^:lib/ptimelog/command/add.rb
153
+
154
+ `fe45a2a` ("Switch to cmdparse for CLI and add Obsidian as datasource") turned
155
+ that into the `Entry`-based `Command::Add`, which never worked because no
156
+ backend implemented `add`. `9adbc86` ("Store the copied parts from the
157
+ add-command for later") copied the five write-methods into `Timelog` as
158
+ comments; the deletion commit removed them. Both are still readable in git.
159
+
160
+ ### The CLI contract to restore
161
+
162
+ $ ptimelog add 'ticket 1337: Implement requirements -- client coding'
163
+ $ ptimelog add '-5 meeting: Discuss requirements -- client planning'
164
+ $ ptimelog add '10:30 meeting: Discuss requirements -- client planning'
165
+
166
+ The whole entry is one quoted argument, in gTimelog's own format. A signed
167
+ prefix shifts by minutes, `HH:MM` or `H:MM` sets the time outright. Prefixing
168
+ an offset *before* a time does not work and never did (`-3 9:42 foo` parses
169
+ `9:42 foo` as the description) — the old spec pinned that as expected
170
+ behaviour.
171
+
172
+ The resulting line, appended to `timelog_txt`:
173
+
174
+ YYYY-MM-DD HH:MM: ticket: description -- tags
175
+
176
+ timelog.txt records when a task *ended*, so only the finish time is written.
177
+ The start is implicit from the preceding line.
178
+
179
+ ### Still in place
180
+
181
+ - `Timelog.timelog_txt` — the configured path, expanded
182
+ - `Timelog.previous_entry` — last non-empty, non-`**` line as an `Entry`
183
+ - `Timelog#tokenize` / `#parse` — the read side, unchanged
184
+ - `Operon#previous_entry` — the Operon equivalent, spec'd
185
+ - `DataSource#backend` — picks `Timelog.instance` or `Operon`
186
+
187
+ ### What has to be built
188
+
189
+ 1. **`Timelog#add`** — append the line, preceded by an empty line when the day
190
+ changed. Five short methods in the old code; `timelog_txt.open('a')` is the
191
+ whole write.
192
+ 2. **A refusal for Operon.** `Command::Add` resolved its backend through
193
+ `DataSource`, and with `obsidian: true` that is an `Operon` instance. Either
194
+ guard on `DataSource#type == :timelog` or give `Operon#add` a raising stub
195
+ (section 8). Without one of the two, `add` fails with `NoMethodError`
196
+ again — which is how it died.
197
+ 3. **Re-registration** — `autoload :Add` in `lib/ptimelog.rb` and
198
+ `parser.add_command` in `exe/ptimelog`.
199
+
200
+ ### Traps
201
+
202
+ 1. **Do not route the write through `Entry`.** `Entry#finish_time=` rounds via
203
+ `round_time` and `rounding` defaults to 15 (`lib/ptimelog/entry.rb`). An
204
+ `add` at 10:37 would be written as `10:30` — permanent loss in the raw file,
205
+ which is the one file that should stay exact. The old code passed a string
206
+ for this reason. There is no `Entry#to_timelog`, only `from_timelog`; if you
207
+ add one, keep it off the rounding writers.
208
+ 2. **The empty-line check is narrow.** The old condition was
209
+ `previous_entry.date == NamedDate.new.named_date('yesterday')`. After a week
210
+ off, the previous entry is not from yesterday and the day separator is
211
+ skipped. Compare against "not today" instead.
212
+ 3. **`previous_entry` is day-only.** Since `show october`, `NamedDate` can
213
+ return a range of days, and `DataSource` hands it straight to
214
+ `Operon.new`. `Operon#previous_entry` has no caller in `lib/` today, so it
215
+ never sees one; `add` would be the first. Guard it there (or in
216
+ `Operon#previous_entry` itself) — unguarded, it writes the whole range into
217
+ `entry.date` and the appended line is garbage.
218
+ 4. **`previous_entry` skips `**`-lines,** so the start it reports after a lunch
219
+ break is the pre-lunch finish. Harmless for timelog.txt, where no start is
220
+ written; it matters for anything that displays or uploads the entry.
221
+ 5. **A missing or empty timelog.txt** raises `Errno::ENOENT` or trips
222
+ `tokenize(nil)` in `previous_entry`. The old spec had a "does not need
223
+ existing entries" case; it was never implemented.
224
+ 6. **`timelog` defaults to `true`** (`lib/ptimelog/configuration.rb`), and
225
+ `DataSource#type` checks it before `obsidian`. A vault user who never set
226
+ `timelog: false` gets the timelog backend.
227
+
228
+ ### Acceptance
229
+
230
+ The old spec file listed these as pending; they are a good checklist:
231
+
232
+ 1. appends exactly one line, the rest of the file byte-identical
233
+ 2. appends an empty line when the day changed
234
+ 3. `-5`, `+5`, `HH:MM`, `H:MM`, and `H:MM -3` combined, all under Timecop
235
+ 4. offset-before-time parses as description, not as a time
236
+ 5. the written time survives a read-back through `tokenize`
237
+
238
+ Roughly 15 lines of implementation plus specs, half an hour, for the
239
+ timelog.txt backend alone. Operon stays out — see sections 1 through 8.
data/exe/ptimelog CHANGED
@@ -13,9 +13,9 @@ parser.add_command(CmdParse::VersionCommand.new)
13
13
  parser.add_command(CmdParse::HelpCommand.new, default: true)
14
14
  parser.add_command(Ptimelog::Command::Info.new)
15
15
 
16
- parser.add_command(Ptimelog::Command::Add.new)
17
16
  parser.add_command(Ptimelog::Command::Edit.new)
18
17
  parser.add_command(Ptimelog::Command::Show.new)
18
+ parser.add_command(Ptimelog::Command::Summarize.new)
19
19
  parser.add_command(Ptimelog::Command::Upload.new)
20
20
  parser.add_command(Ptimelog::Command::Confirm.new)
21
21
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'shellwords'
4
+
3
5
  module Ptimelog
4
6
  module Command
5
7
  # show entries of one day or all of them
@@ -11,25 +13,23 @@ module Ptimelog
11
13
  @durations = false
12
14
  @dry_run = false
13
15
  @entries = {}
16
+ @tags = []
14
17
  @config = Configuration.instance
15
18
 
16
19
  options.on('-d', '--durations', 'Summarize durations by ticket') { @durations = true }
20
+ options.on('-t', '--tag TAG', 'Only entries with this tag, repeat to narrow down') { |tag| @tags << tag }
17
21
  options.on('-n', '--dry-run', 'Do not execute, just dry-run') { @dry_run = true }
18
22
  end
19
23
 
20
- def execute(maybe_named_day) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
24
+ def execute(maybe_named_day)
21
25
  @day = Ptimelog::NamedDate.new.named_date(maybe_named_day)
22
26
  @entries = Ptimelog::DataSource.new(@config, @day).entries
23
27
 
24
28
  @entries.each do |date, list|
25
- next if list.empty?
26
-
27
- valids = Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
28
- list.select(&:valid?).sort_by { |entry| entry.ticket.to_s }
29
- ).sort_by(&:start_time)
30
- total_duration = duration(valids.sum(&:duration))
29
+ valids = selected_entries(list)
30
+ next if valids.empty? # an empty or filtered-out day is no day to confirm
31
31
 
32
- output(date, valids, total_duration)
32
+ output(date, valids, Ptimelog::Entry.format_duration(valids.sum(&:duration)))
33
33
 
34
34
  upload(date) if ask('Upload this day? [Yn]', 'y').downcase == 'y'
35
35
  end
@@ -37,6 +37,13 @@ module Ptimelog
37
37
 
38
38
  private
39
39
 
40
+ def selected_entries(list)
41
+ Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
42
+ list.select { |entry| entry.valid? && entry.tagged_with?(@tags) }
43
+ .sort_by { |entry| entry.ticket.to_s }
44
+ ).sort_by(&:start_time)
45
+ end
46
+
40
47
  def output(date, entries, total_duration)
41
48
  puts date,
42
49
  '----------'
@@ -66,7 +73,8 @@ module Ptimelog
66
73
  end
67
74
 
68
75
  def upload(date)
69
- options = @durations ? '-d' : ''
76
+ # the tags are escaped, as they come from the command-line and go back to a shell
77
+ options = [('-d' if @durations), *@tags.map { |tag| "-t #{tag.shellescape}" }].compact.join(' ')
70
78
  cmd = "#{$PROGRAM_NAME} upload #{date} #{options}"
71
79
 
72
80
  if @dry_run
@@ -75,8 +83,6 @@ module Ptimelog
75
83
  system(cmd)
76
84
  end
77
85
  end
78
-
79
- def duration(durations) = Time.at(durations).utc.strftime('%H:%M')
80
86
  end
81
87
  end
82
88
  end
@@ -11,13 +11,15 @@ module Ptimelog
11
11
  @debug = false
12
12
  @durations = false
13
13
  @entries = {}
14
+ @tags = []
14
15
  @config = Configuration.instance
15
16
 
16
17
  options.on('-d', '--durations', 'Summarize durations by ticket') { @durations = true }
18
+ options.on('-t', '--tag TAG', 'Only entries with this tag, repeat to narrow down') { |tag| @tags << tag }
17
19
  options.on('--debug', 'Show debugging output') { @debug = true }
18
20
  end
19
21
 
20
- def execute(maybe_named_day) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
22
+ def execute(maybe_named_day) # rubocop:disable Metrics/AbcSize
21
23
  debug("Requested Day: #{maybe_named_day}")
22
24
  @day = Ptimelog::NamedDate.new.named_date(maybe_named_day)
23
25
  debug("Resolved Day: #{@day}")
@@ -27,19 +29,22 @@ module Ptimelog
27
29
  debug("Found Entries: #{@entries.values.sum(&:size)}")
28
30
 
29
31
  @entries.each do |date, list|
30
- next if list.empty?
32
+ valids = selected_entries(list)
33
+ next if valids.empty? # an empty or filtered-out day is no day to report
31
34
 
32
- valids = Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
33
- list.select(&:valid?).sort_by { |entry| entry.ticket.to_s }
34
- ).sort_by(&:start_time)
35
- total_duration = duration(valids.sum(&:duration))
36
-
37
- output(date, valids, total_duration)
35
+ output(date, valids, Ptimelog::Entry.format_duration(valids.sum(&:duration)))
38
36
  end
39
37
  end
40
38
 
41
39
  private
42
40
 
41
+ def selected_entries(list)
42
+ Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
43
+ list.select { |entry| entry.valid? && entry.tagged_with?(@tags) }
44
+ .sort_by { |entry| entry.ticket.to_s }
45
+ ).sort_by(&:start_time)
46
+ end
47
+
43
48
  def output(date, entries, total_duration)
44
49
  puts date,
45
50
  '----------'
@@ -60,8 +65,6 @@ module Ptimelog
60
65
 
61
66
  warn msg
62
67
  end
63
-
64
- def duration(durations) = Time.at(durations).utc.strftime('%H:%M')
65
68
  end
66
69
  end
67
70
  end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ptimelog
4
+ module Command
5
+ # Report the time of a day or a month by ticket, for invoicing and cross-checking
6
+ class Summarize < CmdParse::Command
7
+ def initialize
8
+ super('summarize', takes_commands: false)
9
+
10
+ @tags = []
11
+ @config = Configuration.instance
12
+
13
+ options.on('-t', '--tag TAG', 'Only entries with this tag, repeat to narrow down') { |tag| @tags << tag }
14
+ end
15
+
16
+ def execute(maybe_named_day)
17
+ named_date = Ptimelog::NamedDate.new
18
+ day = named_date.named_date(maybe_named_day)
19
+ entries = selected_entries(Ptimelog::DataSource.new(@config, day).entries)
20
+
21
+ return if entries.empty? # an empty or filtered-out range is nothing to report
22
+
23
+ output(named_date.title(day), groups(entries))
24
+ end
25
+
26
+ private
27
+
28
+ def selected_entries(entries)
29
+ entries.flat_map { |_date, list| list }
30
+ .select { |entry| entry.valid? && entry.tagged_with?(@tags) }
31
+ end
32
+
33
+ # The ticket is what gets invoiced and the tags decide its account, so both have to
34
+ # match. An entry without a ticket carries nothing to merge it by and stays on its own.
35
+ def groups(entries)
36
+ ticketed, ticketless = entries.partition { |entry| entry.ticket.to_s != '' }
37
+
38
+ ticketed.group_by { |entry| [entry.ticket, entry.tags.to_a.sort] }.values +
39
+ ticketless.zip # a group of one, each
40
+ end
41
+
42
+ # Tags, account and billability are equal within a group, only the descriptions can differ
43
+ def line(group)
44
+ entry = group.first.dup
45
+ entry.description = group.map(&:description).uniq.join(', ')
46
+
47
+ [duration(group), Ptimelog::Entry::SEPARATOR, entry.label].join(' ')
48
+ end
49
+
50
+ def output(title, groups)
51
+ puts title,
52
+ '----------'
53
+
54
+ groups.each { |group| puts line(group) }
55
+
56
+ puts '----------',
57
+ "Total work done: #{duration(groups.flatten)} hours",
58
+ '----------------------------',
59
+ nil
60
+ end
61
+
62
+ def duration(entries) = Ptimelog::Entry.format_duration(entries.sum(&:duration))
63
+ end
64
+ end
65
+ end
@@ -11,9 +11,11 @@ module Ptimelog
11
11
 
12
12
  @durations = false
13
13
  @entries = {}
14
+ @tags = []
14
15
  @config = Configuration.instance
15
16
 
16
17
  options.on('-d', '--durations', 'Only upload durations') { @durations = true }
18
+ options.on('-t', '--tag TAG', 'Only entries with this tag, repeat to narrow down') { |tag| @tags << tag }
17
19
  end
18
20
 
19
21
  def execute(maybe_named_day)
@@ -28,16 +30,20 @@ module Ptimelog
28
30
  private
29
31
 
30
32
  def upload_date(date, list)
31
- return if list.empty?
32
-
33
- valids = Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
34
- list.select(&:valid?).sort_by { |entry| entry.ticket.to_s }
35
- )
33
+ valids = selected_entries(list)
34
+ return if valids.empty? # an empty or filtered-out day is nothing to upload
36
35
 
37
36
  puts "Uploading #{date}"
38
37
  valids.each { |entry| open_browser(entry) }
39
38
  end
40
39
 
40
+ def selected_entries(list)
41
+ Ptimelog::Joiner.new(compact_on_ticket_only: @durations).join_similar(
42
+ list.select { |entry| entry.valid? && entry.tagged_with?(@tags) }
43
+ .sort_by { |entry| entry.ticket.to_s }
44
+ )
45
+ end
46
+
41
47
  def open_browser(entry)
42
48
  xdg_open "'#{@config[:base_url]}/ordertimes/new?#{url_options(entry)}'", silent: true
43
49
  end
@@ -19,7 +19,7 @@ module Ptimelog
19
19
 
20
20
  def entries
21
21
  case type
22
- when :obsidian then { @day => backend.entries }
22
+ when :obsidian then backend.entries.group_by(&:date)
23
23
  when :timelog then Ptimelog::Day.new(@day).entries
24
24
  else
25
25
  []
data/lib/ptimelog/day.rb CHANGED
@@ -4,6 +4,8 @@ module Ptimelog
4
4
  # Wrap loading of all entries of a day
5
5
  class Day
6
6
  def initialize(date)
7
+ raise(MonthNotSupported, date) if date.is_a?(Range)
8
+
7
9
  @date = NamedDate.new.date(date)
8
10
  end
9
11
 
@@ -35,7 +37,7 @@ module Ptimelog
35
37
  entry = Entry.from_timelog(line)
36
38
  entry.start_time = start
37
39
 
38
- entries << entry if entry.valid? && entry.selected?
40
+ entries << entry if entry.valid?
39
41
 
40
42
  start = entry.finish_time # store previous ending for nice display of next entry
41
43
  end
@@ -25,13 +25,22 @@ module Ptimelog
25
25
  end
26
26
 
27
27
  class << self
28
+ # TODO: this only covers one backend, move into timelog-class
28
29
  def from_timelog(matched_line)
29
30
  entry = new
30
31
  entry.from_timelog(matched_line)
31
32
  entry
32
33
  end
34
+
35
+ def format_duration(seconds)
36
+ full_days, last_day = seconds.divmod(86_400)
37
+ hours, minutes = Time.at(last_day).utc.strftime('%H:%M').split(':')
38
+
39
+ format('%<hours>02d:%<minutes>s', hours: (full_days * 24) + hours.to_i, minutes: minutes)
40
+ end
33
41
  end
34
42
 
43
+ # TODO: this only covers one backend, move into timelog-class
35
44
  def from_timelog(matched_line)
36
45
  self.date = matched_line[:date]
37
46
  self.ticket = matched_line[:ticket]
@@ -92,8 +101,8 @@ module Ptimelog
92
101
  # hide lunch and breaks
93
102
  def hidden? = @description.to_s.end_with?('**') || !publishable?
94
103
 
95
- # or something like @tags.to_a.include?('special-team'), still needs to be implemented...
96
- def selected? = true
104
+ # filter all mentioned tags
105
+ def tagged_with?(wanted) = wanted.all? { |tag| tag_list.include?(tag) }
97
106
 
98
107
  def billable? = @billable == BILLABLE
99
108
 
@@ -114,17 +123,21 @@ module Ptimelog
114
123
  format('%.02f', duration / 3600.0)
115
124
  end
116
125
 
117
- def to_s
126
+ def label
118
127
  billable = billable? ? '($)' : nil
119
128
 
120
129
  tags = tag_list.join(' ') if tag_list.any?
121
130
  desc = [@ticket, @description].compact.join(': ')
122
131
  acc = [@account, billable].compact.join(' ') if @account
123
132
 
133
+ [desc, tags, acc].compact.join(" #{SEPARATOR} ")
134
+ end
135
+
136
+ def to_s
124
137
  [
125
138
  @start_time, '-', @finish_time, SEPARATOR,
126
139
  "#{duration_hours}h", SEPARATOR,
127
- [desc, tags, acc].compact.join(" #{SEPARATOR} "),
140
+ label,
128
141
  ].compact.join(' ')
129
142
  end
130
143
 
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ptimelog
4
+ # A month resolves to a range, which NamedDate would turn into :all further down
5
+ class MonthNotSupported < Ptimelog::Error
6
+ def initialize(day)
7
+ super(<<~MSG)
8
+ The timelog-datasource cannot show a whole month (#{day}).
9
+
10
+ Please request a single day, e.g. 'ptimelog show #{day.first}'
11
+ or just everything with 'ptimelog show all'
12
+ MSG
13
+ end
14
+ end
15
+ end
@@ -9,31 +9,59 @@ module Ptimelog
9
9
  named_date(arg) || :all
10
10
  end
11
11
 
12
- def named_date(date) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/AbcSize
12
+ def named_date(date) # rubocop:disable Metrics/CyclomaticComplexity
13
13
  case date.to_s
14
14
  when 'yesterday' then yesterday
15
15
  when 'today' then Date.today.to_s
16
16
  when 'last', '' then (last_entry || yesterday).to_s
17
- when 'mon', 'monday' then previous_weekday('monday')
18
- when 'tue', 'tuesday' then previous_weekday('tuesday')
19
- when 'wed', 'wednesday' then previous_weekday('wednesday')
20
- when 'thu', 'thursday' then previous_weekday('thursday')
21
- when 'fri', 'friday' then previous_weekday('friday')
22
- when 'sat', 'saturday' then previous_weekday('saturday')
23
- when 'sun', 'sunday' then previous_weekday('sunday')
17
+ when 'month' then month(Date.today.year, Date.today.month)
18
+ when /\A[a-z]{3,9}\z/ then weekday_or_month(date.to_s)
24
19
  when /\d{4}(-\d{2}){2}/ then date.to_s
25
20
  end
26
21
  end
27
22
 
23
+ # for now, every range we build is a month, so we cheat a little.
24
+ def title(date)
25
+ if date.is_a?(Range)
26
+ Date.parse(date.first).strftime('%B %Y')
27
+ else
28
+ date.to_s
29
+ end
30
+ end
31
+
28
32
  private
29
33
 
30
- def previous_weekday(date)
34
+ def weekday_or_month(name)
35
+ if (valid_weekday = index_of(name, Date::DAYNAMES, Date::ABBR_DAYNAMES))
36
+ previous_weekday(valid_weekday)
37
+ elsif (valid_month = index_of(name, Date::MONTHNAMES, Date::ABBR_MONTHNAMES))
38
+ previous_month(valid_month)
39
+ end
40
+ end
41
+
42
+ def index_of(name, names, abbreviated_names)
43
+ names.index(name.capitalize) || abbreviated_names.index(name.capitalize)
44
+ end
45
+
46
+ def previous_weekday(wday)
31
47
  Date.today.prev_day(7)
32
48
  .step(Date.today.prev_day)
33
- .find { |d| d.send(:"#{date}?") }
49
+ .find { |date| date.wday == wday }
34
50
  .to_s
35
51
  end
36
52
 
53
+ def previous_month(number)
54
+ return unless number
55
+
56
+ year = Date.today.year - (number >= Date.today.month ? 1 : 0)
57
+
58
+ month(year, number)
59
+ end
60
+
61
+ def month(year, month)
62
+ (Date.new(year, month, 1).to_s..Date.new(year, month, -1).to_s)
63
+ end
64
+
37
65
  def last_entry
38
66
  timelog.to_h.keys.compact.sort[-2]
39
67
  end
@@ -44,10 +44,11 @@ module Ptimelog
44
44
  # A session belongs to the day it started on. Plans and running timers are no time yet
45
45
  def entries
46
46
  tasks.flat_map { |task| entries_of(task) }
47
- .sort_by(&:start_time)
47
+ .sort_by { |entry| [entry.date, entry.start_time] }
48
48
  end
49
49
 
50
50
  # What a new entry would continue from: a running timer, the last session, the start of the day
51
+ # shortcut: only meaningful for a single day, guard when a caller may pass a range
51
52
  def previous_entry
52
53
  start = running_timer || entries.last&.finish_time || @configuration['operon_start_time'] || '08:00'
53
54
 
@@ -85,7 +86,7 @@ module Ptimelog
85
86
 
86
87
  # Every task of the vault, an inline one on its line, a file task in its frontmatter
87
88
  def tasks
88
- markdown_files.flat_map do |fn|
89
+ @tasks ||= markdown_files.flat_map do |fn|
89
90
  frontmatter, body = split_frontmatter(File.read(fn, encoding: 'UTF-8'))
90
91
 
91
92
  file_tasks(frontmatter, fn) + inline_tasks(body)
@@ -146,25 +147,43 @@ module Ptimelog
146
147
  started && Time.parse(started).strftime('%R')
147
148
  end
148
149
 
150
+ # @day is a single date or a range of them, a whole month
149
151
  def on_day?(datetime)
150
- Time.parse(datetime).strftime('%F') == @day
152
+ day = Time.parse(datetime).strftime('%F')
153
+
154
+ @day.is_a?(Range) ? @day.cover?(day) : @day == day
151
155
  end
152
156
 
153
157
  def entry_for(session, task) # rubocop:disable Metrics/AbcSize
154
158
  fields = task[:fields]
159
+ start = Time.parse(session[:start])
155
160
 
156
161
  Ptimelog::Entry.new.tap do |entry|
157
- entry.date = @day
158
- entry.ticket = fields[custom_key('ticket')]
162
+ entry.date = start.strftime('%F')
163
+ entry.ticket = field(fields, 'ticket')
159
164
  entry.description = task[:description]
160
- entry.tags = fields[custom_key('inferer')]
161
- entry.start_time = Time.parse(session[:start]).strftime('%R')
165
+ entry.tags = field(fields, 'inferer')
166
+ entry.start_time = start.strftime('%R')
162
167
  entry.finish_time = Time.parse(session[:stop]).strftime('%R')
163
168
 
164
169
  entry.infer_ptime_settings
165
170
  end
166
171
  end
167
172
 
173
+ # A subtask only carries what differs from its parent, ticket and tags stay up there
174
+ def field(fields, name, seen = [])
175
+ return if seen.include?(fields) # a hand-edited note can make two tasks each others parent
176
+
177
+ parent = tasks_by_id[fields['parentTask']]
178
+
179
+ fields[custom_key(name)] || (parent && field(parent, name, seen << fields))
180
+ end
181
+
182
+ # A task without an operonId can be tracked, but never be a parent
183
+ def tasks_by_id
184
+ @tasks_by_id ||= tasks.to_h { |task| [task[:fields]['operonId'], task[:fields]] }.except(nil)
185
+ end
186
+
168
187
  def custom_key(name) = self.class.custom_keys.fetch(name)
169
188
  end
170
189
  end
@@ -46,15 +46,6 @@ module Ptimelog
46
46
  timelog_txt.read
47
47
  end
48
48
 
49
- # TODO: spec and activate this code
50
- # def add(_entry)
51
- # @new_lines = []
52
- # add_empty_line if @timelog.previous_entry.date == yesterday
53
- # add_entry(*parse_task(task))
54
- #
55
- # save_file
56
- # end
57
-
58
49
  def parse(data)
59
50
  data.split("\n")
60
51
  .map { |line| tokenize(line) }
@@ -72,44 +63,5 @@ module Ptimelog
72
63
  regexp = /^#{re_date} #{re_time}: #{re_tick}?#{re_desc}#{re_tags}?$/
73
64
  line.match(regexp)
74
65
  end
75
-
76
- # TODO: spec and maybe activate this code. this might be covered by the "add"-command already.
77
- # def parse_task(line)
78
- # matches = line.match('(?<time>\d{1,2}:\d{2} )?(?<offset>[+-]\d+ )?(?<task>.*)')
79
- # formatted_time = if matches[:time]
80
- # Time.parse(matches[:time])
81
- # else
82
- # Time.now
83
- # end
84
- # .localtime
85
- # .then { |time| time + (matches[:offset].to_i * 60) }
86
- # .strftime('%F %R')
87
- #
88
- # [formatted_time, matches[:task]]
89
- # end
90
-
91
- # TODO: spec and activate this code.
92
- # def add_entry(date_time, task)
93
- # @new_lines << "#{date_time}: #{task}"
94
- # end
95
-
96
- # TODO: spec and activate this code.
97
- # def add_empty_line
98
- # @new_lines << ''
99
- # end
100
-
101
- # TODO: spec and activate this code.
102
- # def save_file
103
- # @timelog.timelog_txt.open('a') do |log|
104
- # @new_lines.each do |line|
105
- # log << "#{line}\n"
106
- # end
107
- # end
108
- # end
109
-
110
- # TODO: spec and activate this code.
111
- # def yesterday
112
- # NamedDate.new.named_date('yesterday')
113
- # end
114
66
  end
115
67
  end
@@ -23,6 +23,6 @@
23
23
  # correct subtitle of this documentation-block would be "and a Banner not named
24
24
  # Bruce."
25
25
  module Ptimelog
26
- VERSION = '0.12.0'
26
+ VERSION = '0.13.0'
27
27
  BANNER = 'Move time-entries from gTimelog or Obsidian to PuzzleTime'
28
28
  end
data/lib/ptimelog.rb CHANGED
@@ -4,7 +4,6 @@ $LOAD_PATH.unshift File.dirname(__FILE__)
4
4
 
5
5
  # Autoloading and such
6
6
  module Ptimelog
7
- autoload :App, 'ptimelog/app'
8
7
  autoload :Configuration, 'ptimelog/configuration'
9
8
  autoload :DataSource, 'ptimelog/data_source'
10
9
  autoload :DeprecationWarning, 'ptimelog/deprecation_warning'
@@ -24,15 +23,15 @@ module Ptimelog
24
23
  # timelog.txt
25
24
  autoload :Day, 'ptimelog/day'
26
25
  autoload :Timelog, 'ptimelog/timelog'
26
+ autoload :MonthNotSupported, 'ptimelog/month_not_supported'
27
27
 
28
28
  # Collection of commands available at the CLI
29
29
  module Command
30
- autoload :Add, 'ptimelog/command/add'
31
30
  autoload :Confirm, 'ptimelog/command/confirm'
32
31
  autoload :Edit, 'ptimelog/command/edit'
33
32
  autoload :Info, 'ptimelog/command/info'
34
33
  autoload :Show, 'ptimelog/command/show'
34
+ autoload :Summarize, 'ptimelog/command/summarize'
35
35
  autoload :Upload, 'ptimelog/command/upload'
36
- autoload :Version, 'ptimelog/command/version'
37
36
  end
38
37
  end
data/opencode.json CHANGED
@@ -12,6 +12,9 @@
12
12
  "permission": {
13
13
  "bash": {
14
14
  "*": "ask",
15
+ "bundle exec rspec*": "allow",
16
+ "bundle exec rubocop*": "allow",
17
+ "bundle exec rake*": "allow",
15
18
  "rspec*": "allow",
16
19
  "rubocop*": "allow",
17
20
  "rake*": "allow",
@@ -23,12 +26,13 @@
23
26
  "git add*": "ask",
24
27
  "git commit*": "ask",
25
28
  "git push*": "deny",
26
- "ls*": "allow",
27
- "tail *": "allow",
28
- "head *": "allow",
29
- "grep*": "allow",
30
29
  "cat*": "allow",
31
- "pwd": "allow"
30
+ "echo*": "allow",
31
+ "grep*": "allow",
32
+ "head *": "allow",
33
+ "ls*": "allow",
34
+ "pwd": "allow",
35
+ "tail *": "allow"
32
36
  },
33
37
  }
34
38
  }
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ptimelog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Viehweger
8
+ autorequire:
8
9
  bindir: exe
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2026-11-30 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: cmdparse
@@ -219,6 +220,7 @@ dependencies:
219
220
  - - ">="
220
221
  - !ruby/object:Gem::Version
221
222
  version: '0'
223
+ description:
222
224
  email:
223
225
  - kronn@kronn.de
224
226
  executables:
@@ -229,16 +231,16 @@ files:
229
231
  - AGENTS.md
230
232
  - Gemfile
231
233
  - LICENSE.txt
232
- - PLAN-ADD-COMMAND.md
233
234
  - README.md
234
235
  - Rakefile
236
+ - docs/add-command.md
235
237
  - exe/ptimelog
236
238
  - lib/ptimelog.rb
237
- - lib/ptimelog/command/add.rb
238
239
  - lib/ptimelog/command/confirm.rb
239
240
  - lib/ptimelog/command/edit.rb
240
241
  - lib/ptimelog/command/info.rb
241
242
  - lib/ptimelog/command/show.rb
243
+ - lib/ptimelog/command/summarize.rb
242
244
  - lib/ptimelog/command/upload.rb
243
245
  - lib/ptimelog/configuration.rb
244
246
  - lib/ptimelog/data_source.rb
@@ -247,6 +249,7 @@ files:
247
249
  - lib/ptimelog/entry.rb
248
250
  - lib/ptimelog/error.rb
249
251
  - lib/ptimelog/joiner.rb
252
+ - lib/ptimelog/month_not_supported.rb
250
253
  - lib/ptimelog/named_date.rb
251
254
  - lib/ptimelog/null_pathname.rb
252
255
  - lib/ptimelog/operon.rb
@@ -259,6 +262,7 @@ licenses:
259
262
  - MIT
260
263
  metadata:
261
264
  rubygems_mfa_required: 'true'
265
+ post_install_message:
262
266
  rdoc_options: []
263
267
  require_paths:
264
268
  - lib
@@ -273,7 +277,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
273
277
  - !ruby/object:Gem::Version
274
278
  version: '0'
275
279
  requirements: []
276
- rubygems_version: 4.0.19
280
+ rubygems_version: 3.5.22
281
+ signing_key:
277
282
  specification_version: 4
278
283
  summary: Move time-entries from gTimelog or Obsidian to PuzzleTime
279
284
  test_files: []
@@ -1,68 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'pathname'
4
-
5
- module Ptimelog
6
- module Command
7
- # add a new entrie with the current date and time
8
- class Add < CmdParse::Command
9
- def initialize
10
- super('add', takes_commands: false)
11
-
12
- @data_source = DataSource.new(
13
- Configuration.instance,
14
- NamedDate.new.named_date('today')
15
- )
16
-
17
- options.on('--debug', 'Show debugging output') { @debug = true }
18
- end
19
-
20
- def execute(task_line)
21
- start = backend.previous_entry.finish_time
22
- finish, task = parse_task(task_line)
23
- ticket, desc = task.split(':', 2)
24
-
25
- entry = create_entry(start, finish, ticket, desc)
26
- debug(entry.to_s)
27
-
28
- backend.add(entry)
29
- end
30
-
31
- private
32
-
33
- def parse_task(line)
34
- matches = line.match('(?<time>\d{1,2}:\d{2} )?(?<offset>[+-]\d+ )?(?<task>.*)')
35
- formatted_time = if matches[:time]
36
- Time.parse(matches[:time])
37
- else
38
- Time.now
39
- end
40
- .localtime
41
- .then { |time| time + (matches[:offset].to_i * 60) }
42
- .strftime('%R')
43
-
44
- [formatted_time, matches[:task]]
45
- end
46
-
47
- def create_entry(start, finish, ticket, desc)
48
- Entry.new.tap do |e|
49
- e.date = Date.today
50
- e.start_time = start
51
- e.finish_time = finish
52
- e.ticket = ticket
53
- e.description = desc
54
- end
55
- end
56
-
57
- def backend
58
- @data_source.backend
59
- end
60
-
61
- def debug(msg)
62
- return unless @debug
63
-
64
- warn msg
65
- end
66
- end
67
- end
68
- end