-
Notifications
You must be signed in to change notification settings - Fork 34
Add --break option to finish subcommand #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d526627
Add --break option to finish subcommand
justincampbell 6d53dbb
Refactor: eliminate duplicate code in finish --break
justincampbell 895262d
Refactor: move shared break functions to util.go
justincampbell c471651
Fix: remove redundant breakFlag check in finish command
justincampbell 1e0ae65
Fix: default break timer to user settings when --break used without v…
justincampbell 54855f3
Add tests for finish --break and utility functions
justincampbell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,20 +2,26 @@ package cmd | |
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/open-pomodoro/openpomodoro-cli/format" | ||
| "github.com/open-pomodoro/openpomodoro-cli/hook" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var breakFlag string | ||
|
|
||
| func init() { | ||
| command := &cobra.Command{ | ||
| Use: "finish", | ||
| Short: "Finish the current Pomodoro", | ||
| RunE: finishCmd, | ||
| } | ||
|
|
||
| command.Flags().StringVarP(&breakFlag, "break", "b", "", "take a break after finishing (duration in minutes)") | ||
| command.Flags().Lookup("break").NoOptDefVal = " " | ||
|
|
||
| RootCmd.AddCommand(command) | ||
| } | ||
|
|
||
|
|
@@ -32,5 +38,32 @@ func finishCmd(cmd *cobra.Command, args []string) error { | |
| return err | ||
| } | ||
|
|
||
| return client.Finish() | ||
| if err := client.Finish(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if cmd.Flags().Changed("break") { | ||
| breakDuration := settings.DefaultBreakDuration | ||
|
|
||
| trimmedFlag := strings.TrimSpace(breakFlag) | ||
| if trimmedFlag != "" { | ||
|
Comment on lines
+48
to
+49
|
||
| var err error | ||
| breakDuration, err = parseDurationMinutes(trimmedFlag) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if err := hook.Run(client, "break"); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := wait(breakDuration); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return hook.Run(client, "stop") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/justincampbell/go-countdown" | ||
| "github.com/justincampbell/go-countdown/format" | ||
| ) | ||
|
|
||
| // wait displays a countdown timer for the specified duration | ||
| func wait(d time.Duration) error { | ||
| err := countdown.For(d, time.Second).Do(func(c *countdown.Countdown) error { | ||
| fmt.Printf("\r%s", format.MinSec(c.Remaining())) | ||
| return nil | ||
| }) | ||
|
|
||
| fmt.Println() | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| // parseDurationMinutes parses a duration string, defaulting to minutes if no unit is specified | ||
| func parseDurationMinutes(s string) (time.Duration, error) { | ||
| if _, err := strconv.Atoi(s); err == nil { | ||
| s = fmt.Sprintf("%sm", s) | ||
| } | ||
| return time.ParseDuration(s) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestParseDurationMinutes(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| expected time.Duration | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "integer defaults to minutes", | ||
| input: "5", | ||
| expected: 5 * time.Minute, | ||
| }, | ||
| { | ||
| name: "explicit minutes", | ||
| input: "10m", | ||
| expected: 10 * time.Minute, | ||
| }, | ||
| { | ||
| name: "seconds", | ||
| input: "30s", | ||
| expected: 30 * time.Second, | ||
| }, | ||
| { | ||
| name: "hours", | ||
| input: "1h", | ||
| expected: 1 * time.Hour, | ||
| }, | ||
| { | ||
| name: "complex duration", | ||
| input: "1h30m", | ||
| expected: 90 * time.Minute, | ||
| }, | ||
| { | ||
| name: "invalid duration", | ||
| input: "invalid", | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "zero", | ||
| input: "0", | ||
| expected: 0, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := parseDurationMinutes(tt.input) | ||
| if tt.wantErr { | ||
| require.Error(t, err) | ||
| } else { | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.expected, got) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a space character as the NoOptDefVal is unclear and could be confusing. Consider using an empty string or a more explicit default value like "default" to make the intent clearer.