Skip to content

Commit

Permalink
feat: add time placeholder for active subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Jun 23, 2024
1 parent 6e97ce1 commit c35faab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
version: 2

release:
draft: true

before:
hooks:
- go mod tidy
Expand All @@ -23,7 +26,6 @@ signs:
- "--yes" # needed on cosign 2.0.0+
artifacts: all


brews:
- name: hours
repository:
Expand Down
19 changes: 14 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Accepts an argument, which can be one of the following:
Note: If a task log continues past midnight in your local timezone, it
will be reported on the day it ends.
`,
`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var period string
Expand Down Expand Up @@ -223,7 +223,7 @@ Accepts an argument, which can be one of the following:
Note: If a task log continues past midnight in your local timezone, it'll
appear in the log for the day it ends.
`,
`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var period string
Expand Down Expand Up @@ -254,7 +254,7 @@ Accepts an argument, which can be one of the following:
Note: If a task log continues past midnight in your local timezone, it'll
be considered in the stats for the day it ends.
`,
`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var period string
Expand All @@ -271,6 +271,16 @@ be considered in the stats for the day it ends.
var activeCmd = &cobra.Command{
Use: "active",
Short: "Show the task being actively tracked by \"hours\"",
Long: `Show the task being actively tracked by "hours".
You can pass in a template using the --template/-t flag, which supports the
following placeholders:
{{task}}: for the task summary
{{time}}: for the time spent so far on the active log entry
eg. hours active -t ' {{task}} ({{time}}) '
`,
Run: func(cmd *cobra.Command, args []string) {
ui.ShowActiveTask(db, os.Stdout, activeTemplate)
},
Expand Down Expand Up @@ -303,8 +313,7 @@ Error: %s`, author, repoIssuesUrl, err)
statsCmd.Flags().BoolVarP(&recordsOutputPlain, "plain", "p", false, "whether to output stats without any formatting")
statsCmd.Flags().BoolVarP(&recordsInteractive, "interactive", "i", false, "whether to view stats interactively")

activeCmd.Flags().StringVarP(&activeTemplate, "template", "t", ui.ActiveTaskPlaceholder,
fmt.Sprintf("string template to use for outputting active task; use \"%s\" as placeholder for the task", ui.ActiveTaskPlaceholder))
activeCmd.Flags().StringVarP(&activeTemplate, "template", "t", ui.ActiveTaskPlaceholder, "string template to use for outputting active task")

rootCmd.AddCommand(generateCmd)
rootCmd.AddCommand(reportCmd)
Expand Down
9 changes: 8 additions & 1 deletion internal/ui/active.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"io"
"os"
"strings"
"time"
)

const (
ActiveTaskPlaceholder = "{{task}}"
ActiveTaskPlaceholder = "{{task}}"
ActiveTaskTimePlaceholder = "{{time}}"
)

func ShowActiveTask(db *sql.DB, writer io.Writer, template string) {
Expand All @@ -24,6 +26,11 @@ func ShowActiveTask(db *sql.DB, writer io.Writer, template string) {
return
}

now := time.Now()
timeSpent := now.Sub(activeTaskDetails.lastLogEntryBeginTs).Seconds()
timeSpentStr := humanizeDuration(int(timeSpent))

activeStr := strings.Replace(template, ActiveTaskPlaceholder, activeTaskDetails.taskSummary, 1)
activeStr = strings.Replace(activeStr, ActiveTaskTimePlaceholder, timeSpentStr, 1)
fmt.Fprint(writer, activeStr)
}

0 comments on commit c35faab

Please sign in to comment.