Skip to content

Commit

Permalink
Allow omitting unit (#36)
Browse files Browse the repository at this point in the history
* Add "s" to arg[0] if it was a number without unit

* Add default time unit information to README
  • Loading branch information
lucassperez authored Sep 19, 2022
1 parent 38dc44d commit 880f473
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

---

Timer is a small CLI, similar to the `sleep` everyone already knows and love,
Timer is a small CLI, similar to the `sleep` everyone already knows and love,
with a couple of extra features:

- a progress bar indicating the progression of said timer
Expand All @@ -22,6 +22,11 @@ man timer
timer --help
```

It is possible to pass a time unit for `<duration>`.

Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
If no unit is passed, it defaults to seconds ("s").

## Install

**homebrew**:
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strconv"
"time"

"github.com/charmbracelet/bubbles/key"
Expand Down Expand Up @@ -112,6 +113,7 @@ var rootCmd = &coral.Command{
SilenceUsage: true,
Args: coral.ExactArgs(1),
RunE: func(cmd *coral.Command, args []string) error {
addSuffixIfArgIsNumber(&(args[0]), "s")
duration, err := time.ParseDuration(args[0])
if err != nil {
return err
Expand Down Expand Up @@ -156,3 +158,10 @@ func main() {
os.Exit(1)
}
}

func addSuffixIfArgIsNumber(s *string, suffix string) {
_, err := strconv.ParseFloat(*s, 64)
if err == nil {
*s = *s + suffix
}
}

0 comments on commit 880f473

Please sign in to comment.