From ce8d2766ffbda4551667760a85d37010b68534ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Thu, 15 Oct 2020 21:56:04 +0100 Subject: [PATCH] Refactored output --- z/entry.go | 39 +++++++++++++++++++++++++++------------ z/finish.go | 8 ++++---- z/track.go | 8 ++++---- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/z/entry.go b/z/entry.go index 1a273a5..c6e840b 100644 --- a/z/entry.go +++ b/z/entry.go @@ -85,34 +85,49 @@ func (entry *Entry) IsFinishedAfterBegan() (bool) { return (entry.Finish.IsZero() || entry.Begin.Before(entry.Finish)) } -func (entry *Entry) GetOutputForTrack(isRunning bool) (string) { - outputPrefix := "began tracking" - if isRunning == false { +func (entry *Entry) GetOutputForTrack(isRunning bool, wasRunning bool) (string) { + var outputPrefix string = "" + var outputSuffix string = "" + + now := time.Now() + trackDiffNow := now.Sub(entry.Begin) + trackDiffNowOut := time.Time{}.Add(trackDiffNow) + + if isRunning == true && wasRunning == false { + outputPrefix = "began tracking" + } else if isRunning == true && wasRunning == true { + outputPrefix = "tracking" + outputSuffix = fmt.Sprintf(" for %sh", color.FgLightWhite.Render(trackDiffNowOut.Format("15:04"))) + } else if isRunning == false && wasRunning == 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)) + return fmt.Sprintf("▷ %s %s on %s%s\n", outputPrefix, color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), outputSuffix) } else if entry.Task != "" && entry.Project == "" { - return fmt.Sprintf("▷ %s %s\n", outputPrefix, color.FgLightWhite.Render(entry.Task)) + return fmt.Sprintf("▷ %s %s%s\n", outputPrefix, color.FgLightWhite.Render(entry.Task), outputSuffix) } 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) + return fmt.Sprintf("▷ %s task on %s%s\n", outputPrefix, color.FgLightWhite.Render(entry.Project), outputSuffix) } + + return fmt.Sprintf("▷ %s task%s\n", outputPrefix, outputSuffix) } func (entry *Entry) GetOutputForFinish() (string) { + var outputSuffix string = "" + trackDiff := entry.Finish.Sub(entry.Begin) trackDiffOut := time.Time{}.Add(trackDiff) + outputSuffix = fmt.Sprintf(" for %sh", color.FgLightWhite.Render(trackDiffOut.Format("15:04"))) + 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")) + return fmt.Sprintf("□ finished tracking %s on %s%s\n", color.FgLightWhite.Render(entry.Task), color.FgLightWhite.Render(entry.Project), outputSuffix) } else if entry.Task != "" && entry.Project == "" { - return fmt.Sprintf("□ finished tracking %s for %sh\n", color.FgLightWhite.Render(entry.Task), trackDiffOut.Format("15:04")) + return fmt.Sprintf("□ finished tracking %s%s\n", color.FgLightWhite.Render(entry.Task), outputSuffix) } 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 on %s%s\n", color.FgLightWhite.Render(entry.Project), outputSuffix) } - return fmt.Sprintf("□ finished tracking task\n") + return fmt.Sprintf("□ finished tracking task%s\n", outputSuffix) } diff --git a/z/finish.go b/z/finish.go index 4b72ed3..8bfd3ef 100644 --- a/z/finish.go +++ b/z/finish.go @@ -9,8 +9,8 @@ import ( var finishCmd = &cobra.Command{ Use: "finish", - Short: "Finish currently running tracker", - Long: "Finishing a currently running tracker.", + Short: "Finish currently running activity", + Long: "Finishing tracking of currently running activity.", Run: func(cmd *cobra.Command, args []string) { user := GetCurrentUser() @@ -68,8 +68,8 @@ var finishCmd = &cobra.Command{ func init() { rootCmd.AddCommand(finishCmd) - finishCmd.Flags().StringVarP(&begin, "begin", "b", "", "Time the entry should begin at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).") - finishCmd.Flags().StringVarP(&finish, "finish", "s", "", "Time the entry should finish at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).\nMust be after --begin time.") + finishCmd.Flags().StringVarP(&begin, "begin", "b", "", "Time the activity should begin at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).") + finishCmd.Flags().StringVarP(&finish, "finish", "s", "", "Time the activity should finish at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).\nMust be after --begin time.") finishCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be assigned") finishCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be assigned") diff --git a/z/track.go b/z/track.go index 6c794b6..cf6ee87 100644 --- a/z/track.go +++ b/z/track.go @@ -9,7 +9,7 @@ import ( var trackCmd = &cobra.Command{ Use: "track", Short: "Tracking time", - Long: "Add a new tracking entry, which can either be kept running until 'finish' is being called or parameterized to be a finished entry.", + Long: "Track new activity, which can either be kept running until 'finish' is being called or parameterized to be a finished activity.", Run: func(cmd *cobra.Command, args []string) { user := GetCurrentUser() @@ -38,15 +38,15 @@ var trackCmd = &cobra.Command{ os.Exit(1) } - fmt.Printf(newEntry.GetOutputForTrack(isRunning)) + fmt.Printf(newEntry.GetOutputForTrack(isRunning, false)) return }, } func init() { rootCmd.AddCommand(trackCmd) - trackCmd.Flags().StringVarP(&begin, "begin", "b", "", "Time the entry should begin at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).") - trackCmd.Flags().StringVarP(&finish, "finish", "s", "", "Time the entry should finish at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).\nMust be after --begin time.") + trackCmd.Flags().StringVarP(&begin, "begin", "b", "", "Time the activity should begin at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).") + trackCmd.Flags().StringVarP(&finish, "finish", "s", "", "Time the activity should finish at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).\nMust be after --begin time.") trackCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be assigned") trackCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be assigned") trackCmd.Flags().BoolVarP(&force, "force", "f", false, "Force begin tracking of a new task \neven though another one is still running \n(ONLY IF YOU KNOW WHAT YOU'RE DOING!)")