diff --git a/z/entry.go b/z/entry.go index 949b32c..1a273a5 100644 --- a/z/entry.go +++ b/z/entry.go @@ -3,6 +3,8 @@ package z import ( "errors" "time" + "fmt" + "github.com/gookit/color" ) type Entry struct { @@ -80,5 +82,37 @@ func (entry *Entry) SetFinishFromString(finish string) (time.Time, error) { } func (entry *Entry) IsFinishedAfterBegan() (bool) { - return (entry.Finish.IsZero() == false && entry.Begin.Before(entry.Finish)) + return (entry.Finish.IsZero() || entry.Begin.Before(entry.Finish)) +} + +func (entry *Entry) GetOutputForTrack(isRunning bool) (string) { + outputPrefix := "began tracking" + if isRunning == false { + outputPrefix = "tracked" + } + + if entry.Task != "" && entry.Project != "" { + return fmt.Sprintf("▷ %s %s on %s\n", outputPrefix, color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project)) + } else if entry.Task != "" && entry.Project == "" { + return fmt.Sprintf("▷ %s %s\n", outputPrefix, color.FgLightWhite.Render(entry.Task)) + } else if entry.Task == "" && entry.Project != "" { + return fmt.Sprintf("▷ %s task on %s\n", outputPrefix, color.FgLightWhite.Render(entry.Project)) + } else { + return fmt.Sprintf("▷ %s task\n", outputPrefix) + } +} + +func (entry *Entry) GetOutputForFinish() (string) { + trackDiff := entry.Finish.Sub(entry.Begin) + trackDiffOut := time.Time{}.Add(trackDiff) + + if entry.Task != "" && entry.Project != "" { + return fmt.Sprintf("□ finished tracking %s on %s for %sh\n", color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), trackDiffOut.Format("15:04")) + } else if entry.Task != "" && entry.Project == "" { + return fmt.Sprintf("□ finished tracking %s for %sh\n", color.FgLightWhite.Render(entry.Task), trackDiffOut.Format("15:04")) + } else if entry.Task == "" && entry.Project != "" { + return fmt.Sprintf("□ finished tracking task on %s for %sh\n", color.FgLightWhite.Render(entry.Project), trackDiffOut.Format("15:04")) + } + + return fmt.Sprintf("□ finished tracking task\n") } diff --git a/z/finish.go b/z/finish.go index 0d57c51..b350349 100644 --- a/z/finish.go +++ b/z/finish.go @@ -6,7 +6,6 @@ import ( "fmt" "time" "github.com/spf13/cobra" - "github.com/gookit/color" ) var finishCmd = &cobra.Command{ @@ -59,18 +58,7 @@ var finishCmd = &cobra.Command{ log.Fatal(err) } - trackDiff := runningEntry.Finish.Sub(runningEntry.Begin) - trackDiffOut := time.Time{}.Add(trackDiff) - - if runningEntry.Task != "" && runningEntry.Project != "" { - fmt.Printf("□ finished tracking %s on %s for %sh\n", color.FgLightWhite.Render(runningEntry.Task), color.FgLightWhite.Render(runningEntry.Project), trackDiffOut.Format("15:04")) - } else if runningEntry.Task != "" && runningEntry.Project == "" { - fmt.Printf("□ finished tracking %s for %sh\n", color.FgLightWhite.Render(runningEntry.Task), trackDiffOut.Format("15:04")) - } else if runningEntry.Task == "" && runningEntry.Project != "" { - fmt.Printf("□ finished tracking task on %s for %sh\n", color.FgLightWhite.Render(runningEntry.Project), trackDiffOut.Format("15:04")) - } else { - fmt.Printf("□ finished tracking task\n") - } + fmt.Printf(runningEntry.GetOutputForFinish()) return }, } diff --git a/z/track.go b/z/track.go index e3d5278..4da62d7 100644 --- a/z/track.go +++ b/z/track.go @@ -5,7 +5,6 @@ import ( "log" "fmt" "github.com/spf13/cobra" - "github.com/gookit/color" ) var trackCmd = &cobra.Command{ @@ -37,20 +36,7 @@ var trackCmd = &cobra.Command{ log.Fatal(err) } - outputPrefix := "began tracking" - if isRunning == false { - outputPrefix = "tracked" - } - - if newEntry.Task != "" && newEntry.Project != "" { - fmt.Printf("▷ %s %s on %s\n", outputPrefix, color.FgLightWhite.Render(newEntry.Task), color.FgLightWhite.Render(newEntry.Project)) - } else if newEntry.Task != "" && newEntry.Project == "" { - fmt.Printf("▷ %s %s\n", outputPrefix, color.FgLightWhite.Render(newEntry.Task)) - } else if newEntry.Task == "" && newEntry.Project != "" { - fmt.Printf("▷ %s task on %s\n", outputPrefix, color.FgLightWhite.Render(newEntry.Project)) - } else { - fmt.Printf("▷ %s task\n", outputPrefix) - } + fmt.Printf(newEntry.GetOutputForTrack(isRunning)) return }, }