Skip to content

Commit

Permalink
Make klog start --resume fall back to previous record
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaen committed Oct 6, 2023
1 parent 546684d commit 2806a7e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
40 changes: 32 additions & 8 deletions klog/app/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"github.com/jotaen/klog/klog/app"
"github.com/jotaen/klog/klog/app/cli/lib"
"github.com/jotaen/klog/klog/parser/reconciling"
"github.com/jotaen/klog/klog/parser/txt"
"github.com/jotaen/klog/klog/service"
)

type Start struct {
Expand Down Expand Up @@ -34,14 +36,16 @@ func (opt *Start) Run(ctx app.Context) app.Error {
ctx.Config().DefaultShouldTotal.Map(func(s klog.ShouldTotal) {
additionalData.ShouldTotal = s
})
spy := PreviousRecordSpy{}
return lib.Reconcile(ctx, lib.ReconcileOpts{OutputFileArgs: opt.OutputFileArgs, WarnArgs: opt.WarnArgs},
[]reconciling.Creator{
spy.phonyCreator(date),
reconciling.NewReconcilerAtRecord(date),
reconciling.NewReconcilerForNewRecord(date, opt.DateFormat(ctx.Config()), additionalData),
},

func(reconciler *reconciling.Reconciler) error {
summary, sErr := opt.Summary(reconciler.Record)
summary, sErr := opt.Summary(reconciler.Record, spy.PreviousRecord)
if sErr != nil {
return sErr
}
Expand All @@ -50,7 +54,7 @@ func (opt *Start) Run(ctx app.Context) app.Error {
)
}

func (opt *Start) Summary(currentRecord klog.Record) (klog.EntrySummary, app.Error) {
func (opt *Start) Summary(currentRecord klog.Record, previousRecord klog.Record) (klog.EntrySummary, app.Error) {
// Check for conflicting flags.
if opt.SummaryText != nil && opt.Resume {
return nil, app.NewErrorWithCode(
Expand All @@ -73,18 +77,38 @@ func (opt *Start) Summary(currentRecord klog.Record) (klog.EntrySummary, app.Err

// Return summary of last entry from current record, if it has any entries.
if len(currentRecord.Entries()) > 0 {
return LastEntrySummary(currentRecord), nil
return lastEntrySummary(currentRecord), nil
}

//// Return summary of last entry from last record, if exists.
//if maybeLastRecord != nil {
// return LastEntrySummary(maybeLastRecord), nil
//}
// Return summary of last entry from previous record, if exists.
if previousRecord != nil {
return lastEntrySummary(previousRecord), nil
}

return nil, nil
}

func LastEntrySummary(r klog.Record) klog.EntrySummary {
func lastEntrySummary(r klog.Record) klog.EntrySummary {
entriesCount := len(r.Entries())
return r.Entries()[entriesCount-1].Summary()
}

type PreviousRecordSpy struct {
PreviousRecord klog.Record
}

// phonyCreator is a no-op “pass-through” creator, whose only purpose it is to hook into
// the reconciler-creation mechanism, to get a handle on the records for determining
// the previous record.
func (p *PreviousRecordSpy) phonyCreator(currentDate klog.Date) reconciling.Creator {
return func(records []klog.Record, _ []txt.Block) *reconciling.Reconciler {
for _, r := range service.Sort(records, false) {
if r.Date().IsAfterOrEqual(currentDate) {
continue
}
p.PreviousRecord = r
return nil
}
return nil
}
}
21 changes: 20 additions & 1 deletion klog/app/cli/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ default_rounding = 60m
}

func TestStartWithResume(t *testing.T) {
t.Run("No previous entry -> Empty entry summary", func(t *testing.T) {
t.Run("No previous entry, no previous record -> Empty entry summary", func(t *testing.T) {
state, err := NewTestingContext()._SetRecords(`1623-12-13
`)._SetNow(1623, 12, 13, 12, 49)._Run((&Start{
Resume: true,
Expand All @@ -275,6 +275,25 @@ func TestStartWithResume(t *testing.T) {
`, state.writtenFileContents)
})

t.Run("No previous entry, but previous record -> Take over from previous record", func(t *testing.T) {
state, err := NewTestingContext()._SetRecords(`
1623-12-12
14:00 - 15:00 Did something
10m Some activity
`)._SetNow(1623, 12, 13, 12, 49)._Run((&Start{
Resume: true,
}).Run)
require.Nil(t, err)
assert.Equal(t, `
1623-12-12
14:00 - 15:00 Did something
10m Some activity
1623-12-13
12:49 - ? Some activity
`, state.writtenFileContents)
})

t.Run("No previous entry summary -> Empty entry summary", func(t *testing.T) {
state, err := NewTestingContext()._SetRecords(`1623-12-13
8:13 - 9:44
Expand Down

0 comments on commit 2806a7e

Please sign in to comment.