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() {
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()
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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