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"` }