From f19ab81dc8f13ff339fcb8c7408b999838a1bf59 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 30 Oct 2024 11:03:18 +0100 Subject: [PATCH] doc: add and improve comments --- cmd/client/main.go | 4 ++++ internal/frontend/keyhandler.go | 4 +++- internal/frontend/main.go | 7 ++++++- internal/frontend/notification.go | 2 +- internal/frontend/terminal.go | 4 +++- internal/helper/files.go | 3 +++ internal/websocket/connect.go | 2 ++ internal/websocket/receive.go | 1 + internal/websocket/send.go | 3 +++ pkg/models/client.go | 2 +- pkg/models/configfile.go | 7 ++++--- 11 files changed, 31 insertions(+), 8 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index de0126c..82791b2 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -25,6 +25,7 @@ var ( func Start() { flag.Parse() + // show/hide cursor for nicer interface cursor.Hide() defer cursor.Show() @@ -45,12 +46,15 @@ func Start() { config.Password = *parameter_password } + // Create a client client := websocket.Connect(config.URL) client.Password = config.Password + // Receive server messages und update the terminal channel := make(chan GoTomato.ServerMessage) go client.ProcessServerMessages(channel) frontend.UpdateLoop(client, config, channel) + // disconnect from server client.Disconnect() } diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index 85a2e4c..b2d8d10 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -9,6 +9,7 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) +// sends start/pause/resume based on the state of the pomodoro func start_pause_resume(message GoTomato.ServerMessage) string { if !message.Ongoing { return "start" @@ -20,7 +21,8 @@ func start_pause_resume(message GoTomato.ServerMessage) string { } } -func keyhandler(key keyboard.KeyEvent, client websocket.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool { +// reads a KeyEvent and sends the matching command +func menuHandler(key keyboard.KeyEvent, client websocket.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool { switch key.Rune { case 0: // space cmd := start_pause_resume(message) diff --git a/internal/frontend/main.go b/internal/frontend/main.go index 6a5888e..c9c941b 100644 --- a/internal/frontend/main.go +++ b/internal/frontend/main.go @@ -9,22 +9,27 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) +// Update the terminal and send desktop notifications until the websocket if closed or "quit" func UpdateLoop(client websocket.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { var message GoTomato.ServerMessage + // listen for key events keysEvents, _ := keyboard.GetKeys(1) defer keyboard.Close() for { select { case message = <-channel: + // for every received message desktopNotifications(message) terminalOutput(message) case keypress := <-keysEvents: - if !keyhandler(keypress, client, config, message) { + // react to key pressed + if !menuHandler(keypress, client, config, message) { return } case <-websocket.Done: + // connection closed return } } diff --git a/internal/frontend/notification.go b/internal/frontend/notification.go index bbf53bf..7895f30 100644 --- a/internal/frontend/notification.go +++ b/internal/frontend/notification.go @@ -7,8 +7,8 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) +// Send desktop notifications on the start of each segment func desktopNotifications(pomodoro GoTomato.ServerMessage) { - var ( duration int notification string diff --git a/internal/frontend/terminal.go b/internal/frontend/terminal.go index bac0907..1935721 100644 --- a/internal/frontend/terminal.go +++ b/internal/frontend/terminal.go @@ -8,13 +8,15 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) +// Update the terminal output based on the passed ServerMessage func terminalOutput(pomodoro GoTomato.ServerMessage) { var ( modePrefix string timerOutput string ) - fmt.Print("\033[H\033[2J") // Clears the screen + // Clear the screen + fmt.Print("\033[H\033[2J") // header color.Blue("Work: %d ◊ Break: %d ◊ Longbreak: %d\n\n", diff --git a/internal/helper/files.go b/internal/helper/files.go index 0f6b7e8..7adad37 100644 --- a/internal/helper/files.go +++ b/internal/helper/files.go @@ -10,6 +10,7 @@ import ( ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ) +// Expands the `~` in a passed filename func expandUnixPath(filename string) string { if strings.HasPrefix(filename, "~/") { dirname, _ := os.UserHomeDir() @@ -19,12 +20,14 @@ func expandUnixPath(filename string) string { return filename } +// Checks if the passed file exists func FileExists(filename string) bool { _, err := os.Stat(expandUnixPath(filename)) return !os.IsNotExist(err) } +// Parses the ChronoTomato config in the passed config file func ParseConfig(filename string) ChronoTomato.Config { var config ChronoTomato.Config diff --git a/internal/websocket/connect.go b/internal/websocket/connect.go index 4a2f2b8..0110215 100644 --- a/internal/websocket/connect.go +++ b/internal/websocket/connect.go @@ -10,6 +10,7 @@ import ( type Client ChronoTomato.GoTomatoClient // New websocket client +// Connects to websocket func Connect(url string) Client { log.Info("Connected 󰖟 ", "host", url) @@ -21,6 +22,7 @@ func Connect(url string) Client { return Client{Conn: conn} } +// Disconnects from websocket func (c Client) Disconnect() { select { case <-Done: diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index c6ec7e8..767f6cb 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -11,6 +11,7 @@ import ( var Done = make(chan struct{}) +// Receives websocket messages and write them to `channel` func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) { var serverMessage GoTomato.ServerMessage diff --git a/internal/websocket/send.go b/internal/websocket/send.go index bc958d6..8bc905a 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -8,6 +8,7 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) +// Sends to ClientCommand to the clients websocket func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) if err != nil { @@ -22,6 +23,7 @@ func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { } } +// Sends the passed command to the server func (c Client) SendCmd(cmd string) { message := GoTomato.ClientCommand{ Command: cmd, @@ -31,6 +33,7 @@ func (c Client) SendCmd(cmd string) { c.sendClientCommand(message) } +// Sends the new PomodoroConfig to the server func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) { message := GoTomato.ClientCommand{ Command: "updateSettings", diff --git a/pkg/models/client.go b/pkg/models/client.go index 9cf8436..f2bb4c7 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -2,7 +2,7 @@ package models import "github.com/gorilla/websocket" -// Represents a websocket client +// Represents a GoTomato client type GoTomatoClient struct { Conn *websocket.Conn // The websocket connection of the client Password string // Pomodoro password diff --git a/pkg/models/configfile.go b/pkg/models/configfile.go index 4e824db..fc8d94d 100644 --- a/pkg/models/configfile.go +++ b/pkg/models/configfile.go @@ -2,8 +2,9 @@ package models import GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" +// Represents a ChronoTomato Config type Config struct { - URL string `yaml:"url"` - Password string `yaml:"password"` - PomodoroConfig GoTomato.PomodoroConfig `yaml:"config"` + URL string `yaml:"url"` // URL to GoTomato server + Password string `yaml:"password"` // Pomodoro Password + PomodoroConfig GoTomato.PomodoroConfig `yaml:"config"` // Pomodoro Config }