-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (71 loc) · 1.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bufio"
"fmt"
"os"
"time"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal"
)
func main() {
app := cli.NewApp()
app.Name = "f-trigger"
app.Usage = "Trigger events based on a frequency on stdin"
app.Action = run
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "delimiter, d",
Value: "\n",
Usage: "This character determines the start and end of each timed unit (not yet implemented)",
},
cli.StringFlag{
Name: "unit, u",
Value: "bpm",
Usage: "The unit for measuring frequency (not yet implemented)",
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Visually display the current frequency measurement and which actions are being triggered (not yet implemented)",
},
}
cli.HelpFlag = cli.BoolFlag{
Name: "help, h",
Usage: "Show this help message",
}
cli.VersionFlag = cli.BoolFlag{
Name: "version, V",
Usage: "Print the version",
}
cli.AppHelpTemplate = `{{.Name}} - {{.Usage}}
Version {{.Version}}
Usage:
{{.HelpName}} [options] [triggers]...
{{if .VisibleFlags}}
Options:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}
`
if err := app.Run(os.Args); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
func run(c *cli.Context) error {
var err error
lastTime := time.Now()
scanner := bufio.NewScanner(os.Stdin)
var bpmFormat string
if terminal.IsTerminal(int(os.Stdout.Fd())) {
bpmFormat = "\r\033[K%.0f bpm"
} else {
bpmFormat = "%.0f bpm\n"
}
for scanner.Scan() {
fmt.Printf(bpmFormat, 1/time.Now().Sub(lastTime).Minutes())
lastTime = time.Now()
}
if err = scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Error reading input: ", err.Error())
}
return err
}