Skip to content

Commit c2c97bd

Browse files
committed
feat: add --title (-t) to set work session title
- refactor parseArguments to return error instead of bool - document flag usage in README Closes #7
1 parent cf3ab9a commit c2c97bd

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,23 @@ _Note: Actual notification appearance varies by operating system and desktop env
6565
Work sessions:
6666

6767
```bash
68-
pomo # work session
69-
pomo 30m # 30m work session
70-
pomo 45m 15m # 45m work with 15m break
68+
pomo # work session
69+
pomo 30m # 30m work session
70+
pomo 45m 15m # 45m work with 15m break
71+
pomo -t "write report" # work session with custom title (or --title)
7172
```
7273

7374
Break sessions:
7475

7576
```bash
76-
pomo break # break session
77-
pomo break 10m # 10m break session
77+
pomo break # break session
78+
pomo break 10m # 10m break session
7879
```
7980

8081
View statistics:
8182

8283
```bash
83-
pomo stats # View your productivity stats
84+
pomo stats # View your productivity stats
8485
```
8586

8687
## Installation

cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ func Execute() error {
4040
}
4141

4242
func init() {
43+
rootCmd.Flags().StringP(
44+
"title",
45+
"t",
46+
"",
47+
"work session title",
48+
)
49+
4350
initLogging()
4451
initConfig()
4552
beeep.AppName = config.AppName

cmd/runner.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"fmt"
55
"log"
6-
"os"
76
"time"
87

98
"github.com/Bahaaio/pomo/config"
@@ -15,9 +14,13 @@ import (
1514
func runTask(taskType config.TaskType, cmd *cobra.Command) {
1615
task := taskType.GetTask()
1716

18-
if !parseArguments(cmd.Flags().Args(), task, &config.C.Break) {
17+
if err := parseArguments(cmd.Flags().Args(), task, &config.C.Break); err != nil {
1918
_ = cmd.Usage()
20-
die(nil)
19+
die(err)
20+
}
21+
22+
if err := parseFlags(cmd); err != nil {
23+
die(err)
2124
}
2225

2326
log.Printf("starting %v session: %v", taskType.GetTask().Title, taskType.GetTask().Duration)
@@ -35,24 +38,37 @@ func runTask(taskType config.TaskType, cmd *cobra.Command) {
3538
}
3639

3740
// parses the arguments and sets the duration
38-
// returns false if the duration could not be parsed
39-
func parseArguments(args []string, task *config.Task, breakTask *config.Task) bool {
41+
// returns an error if the duration is invalid
42+
func parseArguments(args []string, task *config.Task, breakTask *config.Task) error {
4043
if len(args) > 0 {
4144
var err error
4245
task.Duration, err = time.ParseDuration(args[0])
4346
if err != nil {
44-
fmt.Fprintf(os.Stderr, "\ninvalid duration: '%v'\n\n", args[0])
45-
return false
47+
return fmt.Errorf("invalid duration: '%v'", args[0])
4648
}
4749

4850
if len(args) > 1 {
4951
breakTask.Duration, err = time.ParseDuration(args[1])
5052
if err != nil {
51-
fmt.Fprintf(os.Stderr, "\ninvalid break duration: '%v'\n\n", args[1])
52-
return false
53+
return fmt.Errorf("invalid break duration: '%v'", args[1])
5354
}
5455
}
5556
}
5657

57-
return true
58+
return nil
59+
}
60+
61+
// parses the flags and sets the title
62+
func parseFlags(cmd *cobra.Command) error {
63+
title, err := cmd.Flags().GetString("title")
64+
if err != nil {
65+
return fmt.Errorf("could not parse title flag: %w", err)
66+
}
67+
68+
// discard empty title
69+
if title != "" {
70+
config.C.Work.Title = title
71+
}
72+
73+
return nil
5874
}

0 commit comments

Comments
 (0)