Compare commits

...

11 commits

Author SHA1 Message Date
a649a3d9fd refactor: rename desktop.go to notification.go
🤖
2024-10-27 22:49:28 +01:00
c764deeeb7 feat: simplify command handling for start, pause, and resume
- extract command logic into a separate function
- reduce code duplication in keyhandler function

🤖
2024-10-27 22:33:36 +01:00
e0000382e9 fix: move package variable to frontend.Handler 2024-10-27 22:33:36 +01:00
e8b463c59c format: update import statements
- clean up and streamline import statements across multiple files

🤖
2024-10-27 22:33:36 +01:00
f26f12ef98 format: improve variable declaration style
- change variable declarations to use a grouped format

🤖
2024-10-27 22:33:36 +01:00
912dfa62f2 format: improve code structure for better readability
🤖
2024-10-27 22:33:36 +01:00
c2310f7735 refactor: improve cli parameter handling
- streamline `main` functions
- refactor cli parameter handling
2024-10-27 22:33:36 +01:00
34d4206f65 fix: use websocket constant to check IsCloseError 2024-10-27 22:33:36 +01:00
f54e8486f1 break: configure via file OR cli parameters
no more defaults when no parameter passed!
2024-10-27 22:33:36 +01:00
4f9bd664a3 fix: remove ununsed return statements in keyhandler() 2024-10-27 22:33:36 +01:00
bbc9977f1c 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
2024-10-27 22:33:30 +01:00
10 changed files with 142 additions and 108 deletions

View file

@ -4,75 +4,42 @@ import (
"atomicgo.dev/cursor" "atomicgo.dev/cursor"
"flag" "flag"
"git.smsvc.net/pomodoro/ChronoTomato/internal/frontend"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper" "git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket" "git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
"atomicgo.dev/keyboard" ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
"atomicgo.dev/keyboard/keys" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
"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() { func Start() {
flag.Parse()
cursor.Hide() cursor.Hide()
defer cursor.Show() defer cursor.Show()
parameter_url := flag.String("url", "", "GoTomato Server URL (eg ws://localhost:8080/ws)") // read cli parameters if no config file passed
parameter_password := flag.String("password", "", "Control password for pomodoro session (optional)") if *configfile == "" {
configfile := flag.String("config", "~/.config/ChronoTomato.yml", "path to config file (optional)") config.URL = *parameter_url
flag.Parse() config.Password = *parameter_password
config := helper.ParseConfig(*configfile)
url := *parameter_url
if url == "" {
url = config.URL
}
password := *parameter_password
if password == "" {
password = config.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 { } else {
websocket.SendCmd(conn, password, "pause") // otherwise read config
return false, nil config = helper.ParseConfig(*configfile)
}
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 conn := websocket.Connect(config.URL)
})
channel := make(chan GoTomato.ServerMessage, 2)
go websocket.ProcessServerMessages(conn, channel)
frontend.Handler(conn, config, channel)
websocket.Disconnect(conn) websocket.Disconnect(conn)
} }

View file

@ -0,0 +1,50 @@
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"
)
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 {
case <-websocket.Done:
quit <- true
return true, nil
default:
switch key.String() {
case "space":
cmd := start_pause_resume(message)
websocket.SendCmd(conn, config.Password, cmd)
case "s":
websocket.SendCmd(conn, config.Password, "stop")
case "r":
if config.PomodoroConfig != (GoTomato.GoTomatoPomodoroConfig{}) {
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
}
case "q":
quit <- true
return true, nil
}
}
return false, nil
})
}

25
internal/frontend/main.go Normal file
View file

@ -0,0 +1,25 @@
package frontend
import (
"github.com/gorilla/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
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)
for {
select {
case message = <-channel:
desktopNotifications(message)
terminalOutput(message)
case <-keyhandler_quit:
return
}
}
}

View file

@ -1,16 +1,18 @@
package notifications package frontend
import ( import (
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"fmt" "fmt"
"github.com/gen2brain/beeep" "github.com/gen2brain/beeep"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
func DesktopNotifications() { func desktopNotifications(pomodoro GoTomato.ServerMessage) {
var duration int
var notification string
pomodoro := &shared.ServerMessage var (
duration int
notification string
)
mode := pomodoro.Mode mode := pomodoro.Mode
session := pomodoro.Session session := pomodoro.Session

View file

@ -1,17 +1,18 @@
package notifications package frontend
import ( import (
"fmt" "fmt"
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"github.com/fatih/color" "github.com/fatih/color"
"strings" "strings"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
func TerminalOutput() { func terminalOutput(pomodoro GoTomato.ServerMessage) {
var modePrefix string var (
var timerOutput string modePrefix string
timerOutput string
pomodoro := &shared.ServerMessage )
fmt.Print("\033[H\033[2J") // Clears the screen fmt.Print("\033[H\033[2J") // Clears the screen
@ -31,7 +32,6 @@ func TerminalOutput() {
modePrefix = " " modePrefix = " "
if pomodoro.Paused { if pomodoro.Paused {
modePrefix = " " modePrefix = " "
} else {
} }
minutes := pomodoro.TimeLeft / 60 minutes := pomodoro.TimeLeft / 60
seconds := pomodoro.TimeLeft % 60 seconds := pomodoro.TimeLeft % 60

View file

@ -1,32 +1,32 @@
package helper package helper
import ( import (
"git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
) )
func ParseConfig(filename string) models.ConfigFile { func ParseConfig(filename string) ChronoTomato.Config {
var config models.ConfigFile var config ChronoTomato.Config
if strings.HasPrefix(filename, "~/") { if strings.HasPrefix(filename, "~/") {
dirname, _ := os.UserHomeDir() dirname, _ := os.UserHomeDir()
filename = filepath.Join(dirname, filename[2:]) filename = filepath.Join(dirname, filename[2:])
} }
yamlFile, err := os.ReadFile(filename) yamlFile, err := os.ReadFile(filename)
if err != nil { if err != nil {
log.Warn("Error opening config file!", "reason", err) log.Fatal("Error opening config file!", "reason", err)
log.Warn("Using defaults")
return models.ConfigFile{
URL: "ws://localhost:8080/ws",
}
} }
err = yaml.Unmarshal(yamlFile, &config) err = yaml.Unmarshal(yamlFile, &config)
if err != nil { if err != nil {
log.Fatalf("Unmarshal: %v", err) log.Fatalf("Unmarshal: %v", err)
} }
return config
return config
} }

View file

@ -1,7 +0,0 @@
package shared
import (
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
var ServerMessage models.ServerMessage

View file

@ -3,22 +3,23 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.smsvc.net/pomodoro/ChronoTomato/internal/notifications"
"git.smsvc.net/pomodoro/ChronoTomato/internal/shared"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
var Done = make(chan struct{}) 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) defer close(Done)
for { for {
_, message, err := conn.ReadMessage() _, message, err := conn.ReadMessage()
if err != nil { if err != nil {
if websocket.IsCloseError(err, 1000) { if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// Ignore normal closure and exit gracefully // Ignore normal closure and exit gracefully
return return
} }
@ -28,14 +29,12 @@ func ProcessServerMessages(conn *websocket.Conn) {
return return
} }
err = json.Unmarshal(message, &shared.ServerMessage) err = json.Unmarshal(message, &serverMessage)
if err != nil { if err != nil {
log.Error("Error unmarshalling!", "reason", err) log.Error("Error unmarshalling!", "reason", err)
continue continue
} }
notifications.DesktopNotifications() channel <- serverMessage
notifications.TerminalOutput()
} }
} }

View file

@ -2,14 +2,14 @@ package websocket
import ( import (
"encoding/json" "encoding/json"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) { func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg) messageBytes, err := json.Marshal(msg)
if err != nil { if err != nil {
log.Error("Error marshalling!", "reason", err) log.Error("Error marshalling!", "reason", err)
return return
@ -23,7 +23,7 @@ func sendClientCommand(conn *websocket.Conn, msg models.ClientCommand) {
} }
func SendCmd(conn *websocket.Conn, pwd string, cmd string) { func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
message := models.ClientCommand{ message := GoTomato.ClientCommand{
Command: cmd, Command: cmd,
Password: pwd, Password: pwd,
} }
@ -31,8 +31,8 @@ func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
sendClientCommand(conn, message) sendClientCommand(conn, message)
} }
func Send_updateSettings(conn *websocket.Conn, pwd string, settings models.GoTomatoPomodoroConfig) { func Send_updateSettings(conn *websocket.Conn, pwd string, settings GoTomato.GoTomatoPomodoroConfig) {
message := models.ClientCommand{ message := GoTomato.ClientCommand{
Command: "updateSettings", Command: "updateSettings",
Password: pwd, Password: pwd,
PomodoroSettings: settings, PomodoroSettings: settings,

View file

@ -1,11 +1,9 @@
package models package models
import ( import GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
type ConfigFile struct { type Config struct {
URL string `yaml:"url"` URL string `yaml:"url"`
Password string `yaml:"password"` Password string `yaml:"password"`
PomodoroConfig models.GoTomatoPomodoroConfig `yaml:"config"` PomodoroConfig GoTomato.GoTomatoPomodoroConfig `yaml:"config"`
} }