This commit is contained in:
Brian Buller 2023-03-31 11:07:03 -05:00
parent 96c2338300
commit 44a9cccbbb
6 changed files with 101 additions and 13 deletions

View File

@ -5,7 +5,10 @@ Copyright © 2023 Brian Buller <brian@bullercodeworks.com>
package cmd
import (
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// breakCmd represents the break command
@ -13,7 +16,9 @@ var breakCmd = &cobra.Command{
Use: "break",
Short: "Start a break",
Run: func(cmd *cobra.Command, args []string) {
length := viper.GetDuration("breaklength")
viper.Set("lastbreak", time.Now().Add(length))
viper.WriteConfig()
},
}

View File

@ -9,6 +9,7 @@ import (
"os"
"os/exec"
"sort"
"strconv"
"strings"
"github.com/spf13/cobra"
@ -61,6 +62,17 @@ func opConfig(cmd *cobra.Command, args []string) error {
viper.WriteConfig()
}
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:
if nv, ok := updConfig[k]; ok {
v = nv

View File

@ -17,17 +17,30 @@ var i3Cmd = &cobra.Command{
Use: "i3",
Short: "Print state to stdout in i3 bar format",
Run: func(cmd *cobra.Command, args []string) {
lastBreak := viper.GetTime("lastbreak")
pomo := viper.GetDuration("pomodoro")
warnPct := viper.GetFloat64("warnpct") //0.75
warn := pomo * time.Duration(warnPct)
elapsed := time.Since(lastBreak)
if elapsed > pomo {
fmt.Print("{\"icon\":\"tomato\",\"state\":\"Critical\", \"text\": \"BREAK!\"}")
} else if elapsed > warn {
fmt.Printf("{\"icon\":\"tomato\",\"state\":\"Warning\", \"text\": \"%s\"}", elapsed)
if len(args) == 1 && args[0] == "click" {
if viper.GetBool("active") {
breakCmd.Run(cmd, []string{})
} else {
startCmd.Run(cmd, []string{})
}
return
}
if viper.GetBool("active") {
lastBreak := viper.GetTime("lastbreak")
pomo := viper.GetDuration("pomodoro")
warnPct := viper.GetFloat64("warnpct") //0.75
warn := float64(pomo.Seconds() * warnPct)
elapsed := time.Since(lastBreak).Seconds()
rem := time.Until(lastBreak.Add(pomo)).Round(time.Duration(time.Second))
if elapsed > pomo.Seconds() {
fmt.Print("{\"icon\":\"time\",\"state\":\"Critical\", \"text\": \"BREAK!\"}")
} else if elapsed > warn {
fmt.Printf("{\"icon\":\"time\",\"state\":\"Warning\", \"text\": \"%s\"}", rem)
} else {
fmt.Printf("{\"icon\":\"time\",\"state\":\"Good\", \"text\": \"%s\"}", rem)
}
} else {
fmt.Printf("{\"icon\":\"tomato\",\"state\":\"Good\", \"text\": \"%s\"}", elapsed)
fmt.Print("{\"icon\":\"time\", \"text\": \"Stopped\"}")
}
},
}

View File

@ -33,9 +33,12 @@ func Execute() {
}
func init() {
viper.SetDefault("active", false)
viper.SetDefault("lastbreak", time.Now())
p, _ := time.ParseDuration("25m")
viper.SetDefault("pomodoro", p)
p, _ = time.ParseDuration("5m")
viper.SetDefault("breaklength", p)
viper.SetDefault("warnpct", 0.75)
cobra.OnInitialize(initConfig)
@ -95,8 +98,9 @@ func initConfig() {
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
if err := viper.ReadInConfig(); err != nil {
fmt.Println("Error reading config")
os.Exit(1)
}
}

27
cmd/start.go Normal file
View 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()
fmt.Println("Timer Started.")
},
}
func init() {
rootCmd.AddCommand(startCmd)
}

27
cmd/stop.go Normal file
View 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()
fmt.Println("Timer Stopped.")
},
}
func init() {
rootCmd.AddCommand(stopCmd)
}