Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
1ef19ca9c7 | |||
004ca7ab5b | |||
4f8927d0d3 | |||
44a9cccbbb |
3
Makefile
3
Makefile
@ -14,5 +14,8 @@ LDFLAGS=-ldflags "-w -s -X cmd.Version=${VERSION} -X cmd.Build=${BUILD}"
|
|||||||
breaktime:
|
breaktime:
|
||||||
go build ${LDFLAGS} -o build/${BINARY}
|
go build ${LDFLAGS} -o build/${BINARY}
|
||||||
|
|
||||||
|
install:
|
||||||
|
mv build/${BINARY} ${GOPATH}/bin
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm build/*
|
rm build/*
|
||||||
|
10
cmd/break.go
10
cmd/break.go
@ -5,7 +5,10 @@ Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
// breakCmd represents the break command
|
// breakCmd represents the break command
|
||||||
@ -13,10 +16,9 @@ var breakCmd = &cobra.Command{
|
|||||||
Use: "break",
|
Use: "break",
|
||||||
Short: "Start a break",
|
Short: "Start a break",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
viper.Set("lastbreak", time.Now())
|
||||||
|
viper.WriteConfig()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() { rootCmd.AddCommand(breakCmd) }
|
||||||
rootCmd.AddCommand(breakCmd)
|
|
||||||
}
|
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -22,9 +23,7 @@ var configCmd = &cobra.Command{
|
|||||||
RunE: opConfig,
|
RunE: opConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() { rootCmd.AddCommand(configCmd) }
|
||||||
rootCmd.AddCommand(configCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func opConfig(cmd *cobra.Command, args []string) error {
|
func opConfig(cmd *cobra.Command, args []string) error {
|
||||||
updConfig := make(map[string]string)
|
updConfig := make(map[string]string)
|
||||||
@ -61,6 +60,17 @@ func opConfig(cmd *cobra.Command, args []string) error {
|
|||||||
viper.WriteConfig()
|
viper.WriteConfig()
|
||||||
}
|
}
|
||||||
settings = append(settings, fmt.Sprintf("%s: %t", k, v))
|
settings = append(settings, fmt.Sprintf("%s: %t", k, v))
|
||||||
|
case float64:
|
||||||
|
if nv, ok := updConfig[k]; ok {
|
||||||
|
var err error
|
||||||
|
v, err = strconv.ParseFloat(nv, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Unable to parse float value")
|
||||||
|
}
|
||||||
|
viper.Set(k, v)
|
||||||
|
viper.WriteConfig()
|
||||||
|
}
|
||||||
|
settings = append(settings, fmt.Sprintf("%s: %f", k, v))
|
||||||
default:
|
default:
|
||||||
if nv, ok := updConfig[k]; ok {
|
if nv, ok := updConfig[k]; ok {
|
||||||
v = nv
|
v = nv
|
||||||
|
41
cmd/i3.go
41
cmd/i3.go
@ -1,6 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
|
||||||
*/
|
*/
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
@ -17,17 +16,45 @@ var i3Cmd = &cobra.Command{
|
|||||||
Use: "i3",
|
Use: "i3",
|
||||||
Short: "Print state to stdout in i3 bar format",
|
Short: "Print state to stdout in i3 bar format",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if len(args) == 1 {
|
||||||
|
switch args[0] {
|
||||||
|
case "toggle":
|
||||||
|
toggleCmd.Run(cmd, []string{"-s"})
|
||||||
|
case "click":
|
||||||
|
if viper.GetBool("active") {
|
||||||
|
breakCmd.Run(cmd, []string{"-s"})
|
||||||
|
} else {
|
||||||
|
startCmd.Run(cmd, []string{"-s"})
|
||||||
|
}
|
||||||
|
case "status":
|
||||||
|
if viper.GetBool("active") {
|
||||||
|
fmt.Print("{\"text\":\"\uf04c\",\"state\":\"Good\"}")
|
||||||
|
} else {
|
||||||
|
fmt.Print("{\"text\":\"\uf04b\"}")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if viper.GetBool("active") {
|
||||||
lastBreak := viper.GetTime("lastbreak")
|
lastBreak := viper.GetTime("lastbreak")
|
||||||
pomo := viper.GetDuration("pomodoro")
|
pomo := viper.GetDuration("pomodoro")
|
||||||
warnPct := viper.GetFloat64("warnpct") //0.75
|
warnPct := viper.GetFloat64("warnpct") //0.75
|
||||||
warn := pomo * time.Duration(warnPct)
|
warn := int(pomo.Seconds() * warnPct)
|
||||||
elapsed := time.Since(lastBreak)
|
elapsed := int(time.Since(lastBreak).Seconds())
|
||||||
if elapsed > pomo {
|
rem := time.Until(lastBreak.Add(pomo)).Round(time.Duration(time.Second))
|
||||||
fmt.Print("{\"icon\":\"tomato\",\"state\":\"Critical\", \"text\": \"BREAK!\"}")
|
if elapsed > int(pomo.Seconds()) {
|
||||||
|
state := "Critical"
|
||||||
|
if elapsed%2 == 1 {
|
||||||
|
state = "Warning"
|
||||||
|
}
|
||||||
|
fmt.Printf("{\"icon\":\"time\",\"state\":\"%s\", \"text\": \"BREAK!\"}", state)
|
||||||
} else if elapsed > warn {
|
} else if elapsed > warn {
|
||||||
fmt.Printf("{\"icon\":\"tomato\",\"state\":\"Warning\", \"text\": \"%s\"}", elapsed)
|
fmt.Printf("{\"icon\":\"time\",\"state\":\"Warning\", \"text\": \"%s\"}", rem)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("{\"icon\":\"tomato\",\"state\":\"Good\", \"text\": \"%s\"}", elapsed)
|
fmt.Printf("{\"icon\":\"time\",\"state\":\"Good\", \"text\": \"%s\"}", rem)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Print("{\"icon\":\"time\", \"text\": \"Stopped\"}")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
21
cmd/root.go
21
cmd/root.go
@ -33,15 +33,16 @@ func Execute() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
viper.SetDefault("active", false)
|
||||||
viper.SetDefault("lastbreak", time.Now())
|
viper.SetDefault("lastbreak", time.Now())
|
||||||
p, _ := time.ParseDuration("25m")
|
p, _ := time.ParseDuration("25m")
|
||||||
viper.SetDefault("pomodoro", p)
|
viper.SetDefault("pomodoro", p)
|
||||||
|
p, _ = time.ParseDuration("5m")
|
||||||
|
viper.SetDefault("breaklength", p)
|
||||||
viper.SetDefault("warnpct", 0.75)
|
viper.SetDefault("warnpct", 0.75)
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.breaktime.yaml)")
|
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.breaktime.yaml)")
|
||||||
|
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
@ -95,11 +96,19 @@ func initConfig() {
|
|||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
|
||||||
// If a config file is found, read it in.
|
// If a config file is found, read it in.
|
||||||
if err := viper.ReadInConfig(); err == nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
fmt.Println("Error reading config")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRootCmd(cmd *cobra.Command, args []string) error {
|
func runRootCmd(cmd *cobra.Command, args []string) error { return nil }
|
||||||
return nil
|
|
||||||
|
func hasArg(arg string, args []string) bool {
|
||||||
|
for i := range args {
|
||||||
|
if args[i] == arg {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
27
cmd/start.go
Normal file
27
cmd/start.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// startCmd represents the start command
|
||||||
|
var startCmd = &cobra.Command{
|
||||||
|
Use: "start",
|
||||||
|
Short: "Start the timer",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
viper.Set("active", true)
|
||||||
|
viper.WriteConfig()
|
||||||
|
if !hasArg("-s", args) {
|
||||||
|
fmt.Println("Timer Started.")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { rootCmd.AddCommand(startCmd) }
|
27
cmd/stop.go
Normal file
27
cmd/stop.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// stopCmd represents the stop command
|
||||||
|
var stopCmd = &cobra.Command{
|
||||||
|
Use: "stop",
|
||||||
|
Short: "Stop the timer",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
viper.Set("active", false)
|
||||||
|
viper.WriteConfig()
|
||||||
|
if !hasArg("-s", args) {
|
||||||
|
fmt.Println("Timer Stopped.")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { rootCmd.AddCommand(stopCmd) }
|
25
cmd/toggle.go
Normal file
25
cmd/toggle.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
|
||||||
|
|
||||||
|
*/
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/viper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// toggleCmd represents the toggle command
|
||||||
|
var toggleCmd = &cobra.Command{
|
||||||
|
Use: "toggle",
|
||||||
|
Short: "Toggle timer off/on",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if viper.GetBool("active") {
|
||||||
|
stopCmd.Run(cmd, args)
|
||||||
|
} else {
|
||||||
|
startCmd.Run(cmd, args)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { rootCmd.AddCommand(toggleCmd) }
|
Loading…
x
Reference in New Issue
Block a user