From 11c599a37144078a054177a82bd14c9b204042fa Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Tue, 22 Oct 2024 13:13:41 +0200 Subject: [PATCH] Genesis --- .gitignore | 1 + ChronoTomato.go | 13 ++++++++ cmd/client/main.go | 31 ++++++++++++++++++ config/PomodoroSettings.go | 12 +++++++ go.mod | 28 +++++++++++++++++ go.sum | 50 ++++++++++++++++++++++++++++++ internal/notifications/desktop.go | 35 +++++++++++++++++++++ internal/notifications/terminal.go | 37 ++++++++++++++++++++++ internal/websocket/connect.go | 38 +++++++++++++++++++++++ internal/websocket/receive.go | 37 ++++++++++++++++++++++ internal/websocket/send.go | 43 +++++++++++++++++++++++++ 11 files changed, 325 insertions(+) create mode 100644 .gitignore create mode 100644 ChronoTomato.go create mode 100644 cmd/client/main.go create mode 100644 config/PomodoroSettings.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/notifications/desktop.go create mode 100644 internal/notifications/terminal.go create mode 100644 internal/websocket/connect.go create mode 100644 internal/websocket/receive.go create mode 100644 internal/websocket/send.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dd9e974 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +ChronoTomato diff --git a/ChronoTomato.go b/ChronoTomato.go new file mode 100644 index 0000000..b6f74b5 --- /dev/null +++ b/ChronoTomato.go @@ -0,0 +1,13 @@ +package main + +//// TODO +// - add progress bar (https://github.com/charmbracelet/bubbles?tab=readme-ov-file#progress) +// - add controls + +import ( + "ChronoTomato/cmd/client" +) + +func main() { + client.Start() +} diff --git a/cmd/client/main.go b/cmd/client/main.go new file mode 100644 index 0000000..94958b1 --- /dev/null +++ b/cmd/client/main.go @@ -0,0 +1,31 @@ +package client + +import ( + "flag" + "os" + "os/signal" + + "ChronoTomato/internal/websocket" +) + +var interrupt = make(chan os.Signal, 1) + +func Start() { + signal.Notify(interrupt, os.Interrupt) + + GoTomatoUrl := flag.String("url", "ws://localhost:8080/ws", "GoTomato Server URL") + password := flag.String("password", "", "Control password for pomodoro session (optional)") + autoStart := flag.Bool("start", false, "Immediately start a Pomodoro") + flag.Parse() + + conn := websocket.Connect(*GoTomatoUrl) + + websocket.Send_updateSettings(conn, *password) + if *autoStart { + websocket.SendCmd(conn, "start", *password) + } + + go websocket.ProcessServerMessages(conn) + + websocket.WaitForDisconnect(conn, interrupt) +} diff --git a/config/PomodoroSettings.go b/config/PomodoroSettings.go new file mode 100644 index 0000000..36c64c5 --- /dev/null +++ b/config/PomodoroSettings.go @@ -0,0 +1,12 @@ +package config + +import ( + "git.smsvc.net/pomodoro/GoTomato/pkg/models" +) + +var PomodoroSettings = models.GoTomatoPomodoroConfig{ + Work: 25 * 60, + ShortBreak: 5 * 60, + LongBreak: 10 * 60, + Sessions: 4, +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2146b76 --- /dev/null +++ b/go.mod @@ -0,0 +1,28 @@ +module ChronoTomato + +go 1.23.1 + +require ( + git.smsvc.net/pomodoro/GoTomato v0.0.0-20241022065122-cb6616f400db + github.com/charmbracelet/log v0.4.0 + github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 + github.com/gorilla/websocket v1.5.3 +) + +require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/lipgloss v0.10.0 // indirect + github.com/go-logfmt/logfmt v0.6.0 // indirect + github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect + github.com/godbus/dbus/v5 v5.1.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.18 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect + golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect + golang.org/x/sys v0.13.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ab717cd --- /dev/null +++ b/go.sum @@ -0,0 +1,50 @@ +git.smsvc.net/pomodoro/GoTomato v0.0.0-20241022065122-cb6616f400db h1:KLq83XCEgM7f2BFRBX4IPlg73ASqlI85Ib5qLb0GDw0= +git.smsvc.net/pomodoro/GoTomato v0.0.0-20241022065122-cb6616f400db/go.mod h1:TV6+3bYjwc+kJ7I60H9YSd7BdBBzPnisx/MRnBbEeDk= +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= +github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= +github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM= +github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4 h1:ygs9POGDQpQGLJPlq4+0LBUmMBNox1N4JSpw+OETcvI= +github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4/go.mod h1:0W7dI87PvXJ1Sjs0QPvWXKcQmNERY77e8l7GFhZB/s4= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/OqtnntR4DfOY2+BgwR60cAcu/i3SE= +github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10= +github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= +github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= +github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ= +github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk= +github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/notifications/desktop.go b/internal/notifications/desktop.go new file mode 100644 index 0000000..f65d785 --- /dev/null +++ b/internal/notifications/desktop.go @@ -0,0 +1,35 @@ +package notifications + +import ( + "fmt" + "git.smsvc.net/pomodoro/GoTomato/pkg/models" + "github.com/gen2brain/beeep" +) + +func DesktopNotifications(msg models.ServerMessage) { + var duration int + var notification string + + mode := msg.Mode + session := msg.Session + sessions := msg.PomodoroSettings.Sessions + + switch msg.Mode { + case "Work": + duration = msg.PomodoroSettings.Work + notification = fmt.Sprintf("Session %d/%d: %s %0.f minutes", session, sessions, mode, float32(duration)/60) + case "ShortBreak": + duration = msg.PomodoroSettings.ShortBreak + notification = fmt.Sprintf("Session %d/%d: Take a %0.f minute break", session, sessions, float32(duration)/60) + case "LongBreak": + duration = msg.PomodoroSettings.LongBreak + notification = fmt.Sprintf("Long Break: Take a %0.f minute break", float32(duration)/60) + case "End": + duration = 0 + notification = fmt.Sprintf("Pomodoro sessions complete! Great job! šŸŽ‰") + } + + if msg.TimeLeft == duration { // start of segment + beeep.Alert("šŸ… Pomodoro Timer", notification, "") + } +} diff --git a/internal/notifications/terminal.go b/internal/notifications/terminal.go new file mode 100644 index 0000000..22b9c77 --- /dev/null +++ b/internal/notifications/terminal.go @@ -0,0 +1,37 @@ +package notifications + +import ( + "fmt" + "git.smsvc.net/pomodoro/GoTomato/pkg/models" +) + +func TerminalOutput(msg models.ServerMessage) { + var timerOutput string + + fmt.Print("\033[H\033[2J") // Clears the screen + + fmt.Printf("Work: %d | Break: %d | Longbreak: %d\n", + msg.PomodoroSettings.Work/60, + msg.PomodoroSettings.ShortBreak/60, + msg.PomodoroSettings.LongBreak/60, + ) + fmt.Printf("Session: %d/%d\n", + msg.Session, + msg.PomodoroSettings.Sessions, + ) + fmt.Printf("\nā–¶ %s\n", + msg.Mode, + ) + + switch msg.Mode { + case "Idle": + timerOutput = "" + default: + minutes := msg.TimeLeft / 60 + seconds := msg.TimeLeft % 60 + + timerOutput = fmt.Sprintf("ā³ %02d:%02d", minutes, seconds) + } + + fmt.Printf(timerOutput) +} diff --git a/internal/websocket/connect.go b/internal/websocket/connect.go new file mode 100644 index 0000000..de7f5ef --- /dev/null +++ b/internal/websocket/connect.go @@ -0,0 +1,38 @@ +package websocket + +import ( + "github.com/charmbracelet/log" + "github.com/gorilla/websocket" + "os" + "time" +) + +func Connect(url string) *websocket.Conn { + log.Info("Connected 󰖟 ", "host", url) + + conn, _, err := websocket.DefaultDialer.Dial(url, nil) + if err != nil { + log.Fatal("Dial error!", "reason", err) + } + + return conn +} + +func WaitForDisconnect(conn *websocket.Conn, interrupt chan os.Signal) { + for { + select { + case <-Done: + // session closed by remote + return + case <-interrupt: + // Cleanly close the connection by sending a close message and then + // waiting (with timeout) for the server to close the connection. + conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + select { + case <-Done: + case <-time.After(time.Second): + } + return + } + } +} diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go new file mode 100644 index 0000000..e73c88c --- /dev/null +++ b/internal/websocket/receive.go @@ -0,0 +1,37 @@ +package websocket + +import ( + "ChronoTomato/internal/notifications" + "encoding/json" + "fmt" + "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) { + var serverMessage models.ServerMessage + + defer close(Done) + + for { + _, message, err := conn.ReadMessage() + if err != nil { + fmt.Println() + log.Error("Read error!", "reason", err) + return + } + + err = json.Unmarshal(message, &serverMessage) + if err != nil { + log.Error("Error unmarshalling!", "reason", err) + continue + } + + notifications.DesktopNotifications(serverMessage) + notifications.TerminalOutput(serverMessage) + } + +} diff --git a/internal/websocket/send.go b/internal/websocket/send.go new file mode 100644 index 0000000..c2971d4 --- /dev/null +++ b/internal/websocket/send.go @@ -0,0 +1,43 @@ +package websocket + +import ( + "ChronoTomato/config" + "encoding/json" + "git.smsvc.net/pomodoro/GoTomato/pkg/models" + "github.com/charmbracelet/log" + "github.com/gorilla/websocket" +) + +func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) { + messageBytes, err := json.Marshal(msg) + + if err != nil { + log.Error("Error marshalling!", "reason", err) + return + } + + err = conn.WriteMessage(websocket.TextMessage, messageBytes) + if err != nil { + log.Error("Write error!", "reason", err) + return + } +} + +func SendCmd(conn *websocket.Conn, cmd string, pwd string) { + message := models.ClientCommand{ + Command: cmd, + Password: pwd, + } + + sendClientCommand(conn, message) +} + +func Send_updateSettings(conn *websocket.Conn, pwd string) { + message := models.ClientCommand{ + Command: "updateSettings", + Password: pwd, + PomodoroSettings: config.PomodoroSettings, + } + + sendClientCommand(conn, message) +}