This commit is contained in:
Sebastian Mark 2024-10-22 13:13:41 +02:00
commit 11c599a371
11 changed files with 325 additions and 0 deletions

View file

@ -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, "")
}
}

View file

@ -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)
}

View file

@ -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
}
}
}

View file

@ -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)
}
}

View file

@ -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)
}