From bbc9977f1cb9bbe985b467865d0a7cbbe99ca77a Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 10:41:31 +0100 Subject: [PATCH 01/11] feat: refactor shared.Message to a channel - remove `shared` package and shared.Message - rename `notifications` package to `frontend` - introduce a channel send the received ServerMessages to the frontend handler(s) - move keyhandler to goroutine - simplify password handling in Start function - update import names when importing from GoTomoto --- cmd/client/main.go | 52 +++---------------- .../{notifications => frontend}/desktop.go | 9 ++-- internal/frontend/keyhandler.go | 48 +++++++++++++++++ internal/frontend/main.go | 24 +++++++++ .../{notifications => frontend}/terminal.go | 9 ++-- internal/helper/config.go | 8 +-- internal/shared/state.go | 7 --- internal/websocket/receive.go | 13 ++--- internal/websocket/send.go | 10 ++-- pkg/models/configfile.go | 10 ++-- 10 files changed, 108 insertions(+), 82 deletions(-) rename internal/{notifications => frontend}/desktop.go (86%) create mode 100644 internal/frontend/keyhandler.go create mode 100644 internal/frontend/main.go rename internal/{notifications => frontend}/terminal.go (86%) delete mode 100644 internal/shared/state.go diff --git a/cmd/client/main.go b/cmd/client/main.go index 11243f1..191345f 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -4,19 +4,19 @@ import ( "atomicgo.dev/cursor" "flag" + "git.smsvc.net/pomodoro/ChronoTomato/internal/frontend" "git.smsvc.net/pomodoro/ChronoTomato/internal/helper" - "git.smsvc.net/pomodoro/ChronoTomato/internal/shared" "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" - "atomicgo.dev/keyboard" - "atomicgo.dev/keyboard/keys" - "git.smsvc.net/pomodoro/GoTomato/pkg/models" + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) func Start() { cursor.Hide() defer cursor.Show() + channel := make(chan GoTomato.ServerMessage, 2) + parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)") parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)") configfile := flag.String("config", "~/.config/ChronoTomato.yml", "path to config file (optional)") @@ -29,50 +29,14 @@ func Start() { url = config.URL } - password := *parameter_password - if password == "" { - password = config.Password + if *parameter_password != "" { + config.Password = *parameter_password } conn := websocket.Connect(url) - go websocket.ProcessServerMessages(conn) - - pomodoro := &shared.ServerMessage - keyboard.Listen(func(key keys.Key) (stop bool, err error) { - select { - case <-websocket.Done: - return true, nil - default: - switch key.String() { - case "space": - if !pomodoro.Ongoing { - websocket.SendCmd(conn, password, "start") - return false, nil - } - if pomodoro.Paused { - websocket.SendCmd(conn, password, "resume") - return false, nil - } else { - websocket.SendCmd(conn, password, "pause") - return false, nil - } - case "s": - websocket.SendCmd(conn, password, "stop") - return false, nil - case "r": - - if config.PomodoroConfig != (models.GoTomatoPomodoroConfig{}) { - websocket.Send_updateSettings(conn, password, config.PomodoroConfig) - } - return false, nil - case "q": - return true, nil - } - } - - return false, nil - }) + go websocket.ProcessServerMessages(conn, channel) + frontend.Handler(conn, config, channel) websocket.Disconnect(conn) } diff --git a/internal/notifications/desktop.go b/internal/frontend/desktop.go similarity index 86% rename from internal/notifications/desktop.go rename to internal/frontend/desktop.go index 5f35efd..7a5e6f4 100644 --- a/internal/notifications/desktop.go +++ b/internal/frontend/desktop.go @@ -1,17 +1,16 @@ -package notifications +package frontend import ( - "git.smsvc.net/pomodoro/ChronoTomato/internal/shared" "fmt" + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/gen2brain/beeep" ) -func DesktopNotifications() { +func desktopNotifications(pomodoro GoTomato.ServerMessage) { + var duration int var notification string - pomodoro := &shared.ServerMessage - mode := pomodoro.Mode session := pomodoro.Session sessions := pomodoro.PomodoroSettings.Sessions diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go new file mode 100644 index 0000000..0c0c322 --- /dev/null +++ b/internal/frontend/keyhandler.go @@ -0,0 +1,48 @@ +package frontend + +import ( + "atomicgo.dev/keyboard" + "atomicgo.dev/keyboard/keys" + "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" + ws "github.com/gorilla/websocket" +) + +func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.ServerMessage, quit chan bool) { + keyboard.Listen(func(key keys.Key) (stop bool, err error) { + select { + case <-websocket.Done: + quit <- true + return true, nil + default: + switch key.String() { + case "space": + if !message.Ongoing { + websocket.SendCmd(conn, config.Password, "start") + return false, nil + } + if message.Paused { + websocket.SendCmd(conn, config.Password, "resume") + return false, nil + } else { + websocket.SendCmd(conn, config.Password, "pause") + return false, nil + } + case "s": + websocket.SendCmd(conn, config.Password, "stop") + return false, nil + case "r": + if config.PomodoroConfig != (GoTomato.GoTomatoPomodoroConfig{}) { + websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig) + } + return false, nil + case "q": + quit <- true + return true, nil + } + } + + return false, nil + }) +} diff --git a/internal/frontend/main.go b/internal/frontend/main.go new file mode 100644 index 0000000..524b6a7 --- /dev/null +++ b/internal/frontend/main.go @@ -0,0 +1,24 @@ +package frontend + +import ( + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" + "github.com/gorilla/websocket" +) + +var message GoTomato.ServerMessage + +func Handler(conn *websocket.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { + keyhandler_quit := make(chan bool, 1) + go keyhandler(conn, config, &message, keyhandler_quit) + + for { + select { + case message = <-channel: + desktopNotifications(message) + terminalOutput(message) + case <-keyhandler_quit: + return + } + } +} diff --git a/internal/notifications/terminal.go b/internal/frontend/terminal.go similarity index 86% rename from internal/notifications/terminal.go rename to internal/frontend/terminal.go index acf0bd5..eddc631 100644 --- a/internal/notifications/terminal.go +++ b/internal/frontend/terminal.go @@ -1,18 +1,16 @@ -package notifications +package frontend import ( "fmt" - "git.smsvc.net/pomodoro/ChronoTomato/internal/shared" + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/fatih/color" "strings" ) -func TerminalOutput() { +func terminalOutput(pomodoro GoTomato.ServerMessage) { var modePrefix string var timerOutput string - pomodoro := &shared.ServerMessage - fmt.Print("\033[H\033[2J") // Clears the screen // header @@ -31,7 +29,6 @@ func TerminalOutput() { modePrefix = " " if pomodoro.Paused { modePrefix = " " - } else { } minutes := pomodoro.TimeLeft / 60 seconds := pomodoro.TimeLeft % 60 diff --git a/internal/helper/config.go b/internal/helper/config.go index bdeed92..6d5ee66 100644 --- a/internal/helper/config.go +++ b/internal/helper/config.go @@ -1,7 +1,7 @@ package helper import ( - "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" "github.com/charmbracelet/log" "gopkg.in/yaml.v3" "os" @@ -9,8 +9,8 @@ import ( "strings" ) -func ParseConfig(filename string) models.ConfigFile { - var config models.ConfigFile +func ParseConfig(filename string) ChronoTomato.Config { + var config ChronoTomato.Config if strings.HasPrefix(filename, "~/") { dirname, _ := os.UserHomeDir() filename = filepath.Join(dirname, filename[2:]) @@ -19,7 +19,7 @@ func ParseConfig(filename string) models.ConfigFile { if err != nil { log.Warn("Error opening config file!", "reason", err) log.Warn("Using defaults") - return models.ConfigFile{ + return ChronoTomato.Config{ URL: "ws://localhost:8080/ws", } } diff --git a/internal/shared/state.go b/internal/shared/state.go deleted file mode 100644 index 2944910..0000000 --- a/internal/shared/state.go +++ /dev/null @@ -1,7 +0,0 @@ -package shared - -import ( - "git.smsvc.net/pomodoro/GoTomato/pkg/models" -) - -var ServerMessage models.ServerMessage diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index 889a8ce..dbce35f 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -3,15 +3,16 @@ package websocket import ( "encoding/json" "fmt" - "git.smsvc.net/pomodoro/ChronoTomato/internal/notifications" - "git.smsvc.net/pomodoro/ChronoTomato/internal/shared" + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/charmbracelet/log" "github.com/gorilla/websocket" ) var Done = make(chan struct{}) -func ProcessServerMessages(conn *websocket.Conn) { +func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerMessage) { + + var serverMessage GoTomato.ServerMessage defer close(Done) @@ -28,14 +29,14 @@ func ProcessServerMessages(conn *websocket.Conn) { return } - err = json.Unmarshal(message, &shared.ServerMessage) + err = json.Unmarshal(message, &serverMessage) if err != nil { log.Error("Error unmarshalling!", "reason", err) continue } - notifications.DesktopNotifications() - notifications.TerminalOutput() + channel <- serverMessage + channel <- serverMessage } } diff --git a/internal/websocket/send.go b/internal/websocket/send.go index dec081c..072ee37 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -2,12 +2,12 @@ package websocket import ( "encoding/json" - "git.smsvc.net/pomodoro/GoTomato/pkg/models" + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/charmbracelet/log" "github.com/gorilla/websocket" ) -func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) { +func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) if err != nil { @@ -23,7 +23,7 @@ func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) { } func SendCmd(conn *websocket.Conn, pwd string, cmd string) { - message := models.ClientCommand{ + message := GoTomato.ClientCommand{ Command: cmd, Password: pwd, } @@ -31,8 +31,8 @@ func SendCmd(conn *websocket.Conn, pwd string, cmd string) { sendClientCommand(conn, message) } -func Send_updateSettings(conn *websocket.Conn, pwd string, settings models.GoTomatoPomodoroConfig) { - message := models.ClientCommand{ +func Send_updateSettings(conn *websocket.Conn, pwd string, settings GoTomato.GoTomatoPomodoroConfig) { + message := GoTomato.ClientCommand{ Command: "updateSettings", Password: pwd, PomodoroSettings: settings, diff --git a/pkg/models/configfile.go b/pkg/models/configfile.go index 28c3fa9..fb4dfc2 100644 --- a/pkg/models/configfile.go +++ b/pkg/models/configfile.go @@ -1,11 +1,11 @@ package models import ( - "git.smsvc.net/pomodoro/GoTomato/pkg/models" + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -type ConfigFile struct { - URL string `yaml:"url"` - Password string `yaml:"password"` - PomodoroConfig models.GoTomatoPomodoroConfig `yaml:"config"` +type Config struct { + URL string `yaml:"url"` + Password string `yaml:"password"` + PomodoroConfig GoTomato.GoTomatoPomodoroConfig `yaml:"config"` } From 4f9bd664a3ff080e61e45479132f60c8f730bd2e Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 11:12:38 +0100 Subject: [PATCH 02/11] fix: remove ununsed return statements in keyhandler() --- internal/frontend/keyhandler.go | 6 +----- internal/websocket/send.go | 1 - 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index 0c0c322..f9c1747 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -20,23 +20,19 @@ func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.Ser case "space": if !message.Ongoing { websocket.SendCmd(conn, config.Password, "start") - return false, nil + break } if message.Paused { websocket.SendCmd(conn, config.Password, "resume") - return false, nil } else { websocket.SendCmd(conn, config.Password, "pause") - return false, nil } case "s": websocket.SendCmd(conn, config.Password, "stop") - return false, nil case "r": if config.PomodoroConfig != (GoTomato.GoTomatoPomodoroConfig{}) { websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig) } - return false, nil case "q": quit <- true return true, nil diff --git a/internal/websocket/send.go b/internal/websocket/send.go index 072ee37..dfb9769 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -9,7 +9,6 @@ import ( func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) - if err != nil { log.Error("Error marshalling!", "reason", err) return From f54e8486f1bd505a22d5119a9bcb1cdda7acb82c Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 16:36:14 +0100 Subject: [PATCH 03/11] break: configure via file OR cli parameters no more defaults when no parameter passed! --- cmd/client/main.go | 18 ++++++++---------- internal/helper/config.go | 7 +------ 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 191345f..89f8055 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -8,6 +8,7 @@ import ( "git.smsvc.net/pomodoro/ChronoTomato/internal/helper" "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) @@ -15,25 +16,22 @@ func Start() { cursor.Hide() defer cursor.Show() + var config ChronoTomato.Config channel := make(chan GoTomato.ServerMessage, 2) parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)") parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)") - configfile := flag.String("config", "~/.config/ChronoTomato.yml", "path to config file (optional)") + configfile := flag.String("config", "", "path to config file (optional)") flag.Parse() - config := helper.ParseConfig(*configfile) - - url := *parameter_url - if url == "" { - url = config.URL + if *configfile != "" { + config = helper.ParseConfig(*configfile) } - if *parameter_password != "" { - config.Password = *parameter_password - } + config.URL = *parameter_url + config.Password = *parameter_password - conn := websocket.Connect(url) + conn := websocket.Connect(config.URL) go websocket.ProcessServerMessages(conn, channel) frontend.Handler(conn, config, channel) diff --git a/internal/helper/config.go b/internal/helper/config.go index 6d5ee66..312e494 100644 --- a/internal/helper/config.go +++ b/internal/helper/config.go @@ -17,16 +17,11 @@ func ParseConfig(filename string) ChronoTomato.Config { } yamlFile, err := os.ReadFile(filename) if err != nil { - log.Warn("Error opening config file!", "reason", err) - log.Warn("Using defaults") - return ChronoTomato.Config{ - URL: "ws://localhost:8080/ws", - } + log.Fatal("Error opening config file!", "reason", err) } err = yaml.Unmarshal(yamlFile, &config) if err != nil { log.Fatalf("Unmarshal: %v", err) } return config - } From 34d4206f65cea637b3c8a04f23ea62c7b916a167 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 16:46:11 +0100 Subject: [PATCH 04/11] fix: use `websocket` constant to check `IsCloseError` --- internal/websocket/receive.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index dbce35f..3e96ca9 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -19,7 +19,7 @@ func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerM for { _, message, err := conn.ReadMessage() if err != nil { - if websocket.IsCloseError(err, 1000) { + if websocket.IsCloseError(err, websocket.CloseNormalClosure) { // Ignore normal closure and exit gracefully return } @@ -36,7 +36,5 @@ func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerM } channel <- serverMessage - channel <- serverMessage } - } From c2310f77350e6f85ebbc1ae4dfb141da462a79f7 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 17:01:23 +0100 Subject: [PATCH 05/11] refactor: improve cli parameter handling - streamline `main` functions - refactor cli parameter handling --- cmd/client/main.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 89f8055..7fdb573 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -12,27 +12,31 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) +var ( + config ChronoTomato.Config + parameter_url = flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)") + parameter_password = flag.String("password", "", "Control password for pomodoro session") + configfile = flag.String("config", "", "Path to config file") +) + func Start() { + flag.Parse() + cursor.Hide() defer cursor.Show() - var config ChronoTomato.Config - channel := make(chan GoTomato.ServerMessage, 2) - - parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)") - parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)") - configfile := flag.String("config", "", "path to config file (optional)") - flag.Parse() - - if *configfile != "" { + // read cli parameters if no config file passed + if *configfile == "" { + config.URL = *parameter_url + config.Password = *parameter_password + } else { + // otherwise read config config = helper.ParseConfig(*configfile) } - config.URL = *parameter_url - config.Password = *parameter_password - conn := websocket.Connect(config.URL) + channel := make(chan GoTomato.ServerMessage, 2) go websocket.ProcessServerMessages(conn, channel) frontend.Handler(conn, config, channel) From 912dfa62f2becbb5f2a6c56d7742af078d996be7 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 17:03:28 +0100 Subject: [PATCH 06/11] format: improve code structure for better readability MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 --- internal/helper/config.go | 4 ++++ internal/websocket/receive.go | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/helper/config.go b/internal/helper/config.go index 312e494..17e97ac 100644 --- a/internal/helper/config.go +++ b/internal/helper/config.go @@ -11,17 +11,21 @@ import ( func ParseConfig(filename string) ChronoTomato.Config { var config ChronoTomato.Config + if strings.HasPrefix(filename, "~/") { dirname, _ := os.UserHomeDir() filename = filepath.Join(dirname, filename[2:]) } + yamlFile, err := os.ReadFile(filename) if err != nil { log.Fatal("Error opening config file!", "reason", err) } + err = yaml.Unmarshal(yamlFile, &config) if err != nil { log.Fatalf("Unmarshal: %v", err) } + return config } diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index 3e96ca9..1474ccd 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -11,7 +11,6 @@ import ( var Done = make(chan struct{}) func ProcessServerMessages(conn *websocket.Conn, channel chan<- GoTomato.ServerMessage) { - var serverMessage GoTomato.ServerMessage defer close(Done) From f26f12ef98b985be172b07c5b6265556a598233f Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 21:32:57 +0100 Subject: [PATCH 07/11] format: improve variable declaration style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - change variable declarations to use a grouped format 🤖 --- cmd/client/main.go | 3 ++- internal/frontend/desktop.go | 6 ++++-- internal/frontend/terminal.go | 6 ++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index 7fdb573..80f3c5b 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -13,7 +13,8 @@ import ( ) var ( - config ChronoTomato.Config + config ChronoTomato.Config + parameter_url = flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)") parameter_password = flag.String("password", "", "Control password for pomodoro session") configfile = flag.String("config", "", "Path to config file") diff --git a/internal/frontend/desktop.go b/internal/frontend/desktop.go index 7a5e6f4..ee47394 100644 --- a/internal/frontend/desktop.go +++ b/internal/frontend/desktop.go @@ -8,8 +8,10 @@ import ( func desktopNotifications(pomodoro GoTomato.ServerMessage) { - var duration int - var notification string + var ( + duration int + notification string + ) mode := pomodoro.Mode session := pomodoro.Session diff --git a/internal/frontend/terminal.go b/internal/frontend/terminal.go index eddc631..f29447d 100644 --- a/internal/frontend/terminal.go +++ b/internal/frontend/terminal.go @@ -8,8 +8,10 @@ import ( ) func terminalOutput(pomodoro GoTomato.ServerMessage) { - var modePrefix string - var timerOutput string + var ( + modePrefix string + timerOutput string + ) fmt.Print("\033[H\033[2J") // Clears the screen From e8b463c59ce7c3c7a1f0fb7b4594685431c6c806 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 21:34:53 +0100 Subject: [PATCH 08/11] format: update import statements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - clean up and streamline import statements across multiple files 🤖 --- internal/frontend/desktop.go | 3 ++- internal/frontend/keyhandler.go | 4 +++- internal/frontend/main.go | 3 ++- internal/frontend/terminal.go | 3 ++- internal/helper/config.go | 3 ++- internal/websocket/receive.go | 3 ++- internal/websocket/send.go | 3 ++- pkg/models/configfile.go | 4 +--- 8 files changed, 16 insertions(+), 10 deletions(-) diff --git a/internal/frontend/desktop.go b/internal/frontend/desktop.go index ee47394..250c5db 100644 --- a/internal/frontend/desktop.go +++ b/internal/frontend/desktop.go @@ -2,8 +2,9 @@ package frontend import ( "fmt" - GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/gen2brain/beeep" + + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) func desktopNotifications(pomodoro GoTomato.ServerMessage) { diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index f9c1747..58aad16 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -3,10 +3,12 @@ package frontend import ( "atomicgo.dev/keyboard" "atomicgo.dev/keyboard/keys" + ws "github.com/gorilla/websocket" + "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" - ws "github.com/gorilla/websocket" ) func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.ServerMessage, quit chan bool) { diff --git a/internal/frontend/main.go b/internal/frontend/main.go index 524b6a7..e64b01d 100644 --- a/internal/frontend/main.go +++ b/internal/frontend/main.go @@ -1,9 +1,10 @@ package frontend import ( + "github.com/gorilla/websocket" + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" - "github.com/gorilla/websocket" ) var message GoTomato.ServerMessage diff --git a/internal/frontend/terminal.go b/internal/frontend/terminal.go index f29447d..70d9deb 100644 --- a/internal/frontend/terminal.go +++ b/internal/frontend/terminal.go @@ -2,9 +2,10 @@ package frontend import ( "fmt" - GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/fatih/color" "strings" + + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) func terminalOutput(pomodoro GoTomato.ServerMessage) { diff --git a/internal/helper/config.go b/internal/helper/config.go index 17e97ac..4cd2f7a 100644 --- a/internal/helper/config.go +++ b/internal/helper/config.go @@ -1,12 +1,13 @@ package helper import ( - ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" "github.com/charmbracelet/log" "gopkg.in/yaml.v3" "os" "path/filepath" "strings" + + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ) func ParseConfig(filename string) ChronoTomato.Config { diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index 1474ccd..d17cac6 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -3,9 +3,10 @@ package websocket import ( "encoding/json" "fmt" - GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/charmbracelet/log" "github.com/gorilla/websocket" + + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) var Done = make(chan struct{}) diff --git a/internal/websocket/send.go b/internal/websocket/send.go index dfb9769..90df844 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -2,9 +2,10 @@ package websocket import ( "encoding/json" - GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" "github.com/charmbracelet/log" "github.com/gorilla/websocket" + + GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) { diff --git a/pkg/models/configfile.go b/pkg/models/configfile.go index fb4dfc2..b8ea721 100644 --- a/pkg/models/configfile.go +++ b/pkg/models/configfile.go @@ -1,8 +1,6 @@ package models -import ( - GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" -) +import GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" type Config struct { URL string `yaml:"url"` From e0000382e9745dd7346c8b1db791b1c815116558 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 21:47:05 +0100 Subject: [PATCH 09/11] fix: move package variable to `frontend.Handler` --- internal/frontend/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/frontend/main.go b/internal/frontend/main.go index e64b01d..c876bd5 100644 --- a/internal/frontend/main.go +++ b/internal/frontend/main.go @@ -7,9 +7,9 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -var message GoTomato.ServerMessage - func Handler(conn *websocket.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { + var message GoTomato.ServerMessage + keyhandler_quit := make(chan bool, 1) go keyhandler(conn, config, &message, keyhandler_quit) From c764deeeb77b16a83d164bf8677b36dc2b989752 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 21:57:05 +0100 Subject: [PATCH 10/11] feat: simplify command handling for start, pause, and resume MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - extract command logic into a separate function - reduce code duplication in keyhandler function 🤖 --- internal/frontend/keyhandler.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index 58aad16..0c0ed45 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -11,6 +11,17 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) +func start_pause_resume(message *GoTomato.ServerMessage) string { + if !message.Ongoing { + return "start" + } + if message.Paused { + return "resume" + } else { + return "pause" + } +} + func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.ServerMessage, quit chan bool) { keyboard.Listen(func(key keys.Key) (stop bool, err error) { select { @@ -20,15 +31,8 @@ func keyhandler(conn *ws.Conn, config ChronoTomato.Config, message *GoTomato.Ser default: switch key.String() { case "space": - if !message.Ongoing { - websocket.SendCmd(conn, config.Password, "start") - break - } - if message.Paused { - websocket.SendCmd(conn, config.Password, "resume") - } else { - websocket.SendCmd(conn, config.Password, "pause") - } + cmd := start_pause_resume(message) + websocket.SendCmd(conn, config.Password, cmd) case "s": websocket.SendCmd(conn, config.Password, "stop") case "r": From a649a3d9fdc07ba680ab5ea08ae255b00ca4fcf8 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Sun, 27 Oct 2024 22:49:28 +0100 Subject: [PATCH 11/11] refactor: rename `desktop.go` to `notification.go` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🤖 --- internal/frontend/{desktop.go => notification.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename internal/frontend/{desktop.go => notification.go} (100%) diff --git a/internal/frontend/desktop.go b/internal/frontend/notification.go similarity index 100% rename from internal/frontend/desktop.go rename to internal/frontend/notification.go