Browse Source

updated output, help, and README

split the command out into bin and options
added more debug output
added error output for failing exec command
issue-6
Blaine Motsinger 1 year ago
parent
commit
111c01fbde
2 changed files with 38 additions and 19 deletions
  1. 0
    4
      README.md
  2. 38
    15
      src/main.go

+ 0
- 4
README.md View File

@@ -36,7 +36,3 @@ Run timer for 1 hour, 30 minutes, and 2 seconds
```
timer -hr 1 -min 30 -sec 2
```

# AUTHOR

Blaine Motsinger <blaine@renderorange.com>

+ 38
- 15
src/main.go View File

@@ -7,18 +7,25 @@ import (
"fmt"
"os"
"os/exec"
"strings"
"time"
)

func main() {
// redefine Usage
// this is more like what I'm used to
flag.Usage = func() {
fmt.Println("Usage:")
fmt.Printf(" %s [-hr] [-min] [-sec]\n\n", os.Args[0])

fmt.Println("Options:")
flag.PrintDefaults()

fmt.Println("\nExamples:\n")
fmt.Println(" Run timer for 30 minutes")
fmt.Print("\t$ timer -min 30\n\n")

fmt.Println(" Run timer for 1 hour, 30 minutes, and 2 seconds")
fmt.Print("\t$ timer -hr 1 -min 30 -sec 2\n\n")
}

// define the options
@@ -36,13 +43,35 @@ func main() {

// check on supported systems
// we're just exec'ing the command rather than using the lib
// so it can port between macos and linux
// TODO: trim newline from uname system output
// so it can port between macos and linux with less code.
unameCmd := exec.Command("uname")
system, _ := unameCmd.Output()
unameByte, _ := unameCmd.Output()

system := string(unameByte[:])
system = strings.TrimSuffix(system, "\n")

if system != "Linux" && system != "Darwin" {
fmt.Fprintf(os.Stderr, "error: %s is not a supported system\n", system)
os.Exit(1)
}

totalSeconds := calculateSeconds(*hr, *min, *sec)

message := fmt.Sprintf("%v hr %v min %v sec are done", *hr, *min, *sec)

var bin string
var options []string

if system == "Linux" {
bin = "notify-send"
options = []string{"-u", "critical", "Timer", message}
} else {
// there isn't an API to notifications which allows to set an individual notification to "sticky."
// the best we can do here is set the timeout high.
bin = "terminal-notifier"
options = []string{"-title", "Timer", "-message", message, "-timeout", "60"}
}

// print the debug information
if *debug == true {
fmt.Println("# [DEBUG] options:")
@@ -54,6 +83,7 @@ func main() {

fmt.Printf("# [DEBUG] system: %s\n", system)
fmt.Printf("# [DEBUG] total seconds: %v\n", totalSeconds)
fmt.Printf("# [DEBUG] bin options: %s %s\n", bin, options)
os.Exit(0)
}

@@ -65,18 +95,11 @@ func main() {
totalSeconds--
}

message := fmt.Sprintf("%v hr %v min %v sec are done", *hr, *min, *sec)
notifyCmd := exec.Command(bin, options...)

// TODO: initialize notifyCmd outside of the if
// and then run outside the if
if string(system) == "Linux\n" {
notifyCmd := exec.Command("notify-send", "-u", "critical", "Timer", message)
notifyCmd.Run()
} else {
// there isn't an API to notifications which allows to set an individual notification to "sticky."
// the best we can do here is set the timeout high.
notifyCmd := exec.Command("terminal-notifier", "-title", "Timer", "-message", message, "-timeout", "60")
notifyCmd.Run()
if err := notifyCmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}


Loading…
Cancel
Save