doc: add and improve comments

This commit is contained in:
Sebastian Mark 2024-10-30 11:03:18 +01:00
parent 6187122b81
commit f19ab81dc8
11 changed files with 31 additions and 8 deletions

View file

@ -25,6 +25,7 @@ var (
func Start() { func Start() {
flag.Parse() flag.Parse()
// show/hide cursor for nicer interface
cursor.Hide() cursor.Hide()
defer cursor.Show() defer cursor.Show()
@ -45,12 +46,15 @@ func Start() {
config.Password = *parameter_password config.Password = *parameter_password
} }
// Create a client
client := websocket.Connect(config.URL) client := websocket.Connect(config.URL)
client.Password = config.Password client.Password = config.Password
// Receive server messages und update the terminal
channel := make(chan GoTomato.ServerMessage) channel := make(chan GoTomato.ServerMessage)
go client.ProcessServerMessages(channel) go client.ProcessServerMessages(channel)
frontend.UpdateLoop(client, config, channel) frontend.UpdateLoop(client, config, channel)
// disconnect from server
client.Disconnect() client.Disconnect()
} }

View file

@ -9,6 +9,7 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" 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 { func start_pause_resume(message GoTomato.ServerMessage) string {
if !message.Ongoing { if !message.Ongoing {
return "start" 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 { switch key.Rune {
case 0: // space case 0: // space
cmd := start_pause_resume(message) cmd := start_pause_resume(message)

View file

@ -9,22 +9,27 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" 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) { func UpdateLoop(client websocket.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
var message GoTomato.ServerMessage var message GoTomato.ServerMessage
// listen for key events
keysEvents, _ := keyboard.GetKeys(1) keysEvents, _ := keyboard.GetKeys(1)
defer keyboard.Close() defer keyboard.Close()
for { for {
select { select {
case message = <-channel: case message = <-channel:
// for every received message
desktopNotifications(message) desktopNotifications(message)
terminalOutput(message) terminalOutput(message)
case keypress := <-keysEvents: case keypress := <-keysEvents:
if !keyhandler(keypress, client, config, message) { // react to key pressed
if !menuHandler(keypress, client, config, message) {
return return
} }
case <-websocket.Done: case <-websocket.Done:
// connection closed
return return
} }
} }

View file

@ -7,8 +7,8 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
// Send desktop notifications on the start of each segment
func desktopNotifications(pomodoro GoTomato.ServerMessage) { func desktopNotifications(pomodoro GoTomato.ServerMessage) {
var ( var (
duration int duration int
notification string notification string

View file

@ -8,13 +8,15 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
// Update the terminal output based on the passed ServerMessage
func terminalOutput(pomodoro GoTomato.ServerMessage) { func terminalOutput(pomodoro GoTomato.ServerMessage) {
var ( var (
modePrefix string modePrefix string
timerOutput string timerOutput string
) )
fmt.Print("\033[H\033[2J") // Clears the screen // Clear the screen
fmt.Print("\033[H\033[2J")
// header // header
color.Blue("Work: %d ◊ Break: %d ◊ Longbreak: %d\n\n", color.Blue("Work: %d ◊ Break: %d ◊ Longbreak: %d\n\n",

View file

@ -10,6 +10,7 @@ import (
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
) )
// Expands the `~` in a passed filename
func expandUnixPath(filename string) string { func expandUnixPath(filename string) string {
if strings.HasPrefix(filename, "~/") { if strings.HasPrefix(filename, "~/") {
dirname, _ := os.UserHomeDir() dirname, _ := os.UserHomeDir()
@ -19,12 +20,14 @@ func expandUnixPath(filename string) string {
return filename return filename
} }
// Checks if the passed file exists
func FileExists(filename string) bool { func FileExists(filename string) bool {
_, err := os.Stat(expandUnixPath(filename)) _, err := os.Stat(expandUnixPath(filename))
return !os.IsNotExist(err) return !os.IsNotExist(err)
} }
// Parses the ChronoTomato config in the passed config file
func ParseConfig(filename string) ChronoTomato.Config { func ParseConfig(filename string) ChronoTomato.Config {
var config ChronoTomato.Config var config ChronoTomato.Config

View file

@ -10,6 +10,7 @@ import (
type Client ChronoTomato.GoTomatoClient // New websocket client type Client ChronoTomato.GoTomatoClient // New websocket client
// Connects to websocket
func Connect(url string) Client { func Connect(url string) Client {
log.Info("Connected 󰖟 ", "host", url) log.Info("Connected 󰖟 ", "host", url)
@ -21,6 +22,7 @@ func Connect(url string) Client {
return Client{Conn: conn} return Client{Conn: conn}
} }
// Disconnects from websocket
func (c Client) Disconnect() { func (c Client) Disconnect() {
select { select {
case <-Done: case <-Done:

View file

@ -11,6 +11,7 @@ import (
var Done = make(chan struct{}) var Done = make(chan struct{})
// Receives websocket messages and write them to `channel`
func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) { func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
var serverMessage GoTomato.ServerMessage var serverMessage GoTomato.ServerMessage

View file

@ -8,6 +8,7 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
// Sends to ClientCommand to the clients websocket
func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { func (c Client) sendClientCommand(msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg) messageBytes, err := json.Marshal(msg)
if err != nil { 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) { func (c Client) SendCmd(cmd string) {
message := GoTomato.ClientCommand{ message := GoTomato.ClientCommand{
Command: cmd, Command: cmd,
@ -31,6 +33,7 @@ func (c Client) SendCmd(cmd string) {
c.sendClientCommand(message) c.sendClientCommand(message)
} }
// Sends the new PomodoroConfig to the server
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) { func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
message := GoTomato.ClientCommand{ message := GoTomato.ClientCommand{
Command: "updateSettings", Command: "updateSettings",

View file

@ -2,7 +2,7 @@ package models
import "github.com/gorilla/websocket" import "github.com/gorilla/websocket"
// Represents a websocket client // Represents a GoTomato client
type GoTomatoClient struct { type GoTomatoClient struct {
Conn *websocket.Conn // The websocket connection of the client Conn *websocket.Conn // The websocket connection of the client
Password string // Pomodoro password Password string // Pomodoro password

View file

@ -2,8 +2,9 @@ package models
import GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" import GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
// Represents a ChronoTomato Config
type Config struct { type Config struct {
URL string `yaml:"url"` URL string `yaml:"url"` // URL to GoTomato server
Password string `yaml:"password"` Password string `yaml:"password"` // Pomodoro Password
PomodoroConfig GoTomato.PomodoroConfig `yaml:"config"` PomodoroConfig GoTomato.PomodoroConfig `yaml:"config"` // Pomodoro Config
} }