feat: use bubbletea framework
- implement TUI in bubbletea - split components into separate files - remove now unused functions - restructure files
This commit is contained in:
parent
9656db5335
commit
8a0ef32c91
14 changed files with 269 additions and 187 deletions
92
cmd/client/app.go
Normal file
92
cmd/client/app.go
Normal file
|
@ -0,0 +1,92 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"github.com/charmbracelet/log"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
|
||||
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
|
||||
"github.com/charmbracelet/bubbles/help"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
var (
|
||||
config ChronoTomato.Config
|
||||
client websocket.Client
|
||||
)
|
||||
|
||||
type app struct {
|
||||
keys keyMap
|
||||
help help.Model
|
||||
channel chan GoTomato.ServerMessage
|
||||
pomodoro GoTomato.ServerMessage
|
||||
}
|
||||
|
||||
func myApp() app {
|
||||
return app{
|
||||
keys: keys,
|
||||
help: help.New(),
|
||||
channel: make(chan GoTomato.ServerMessage),
|
||||
pomodoro: GoTomato.ServerMessage{},
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for channel signal and return the ServerMessage as a tea.Msg.
|
||||
// Return tea.Quit() if the channel has been closed.
|
||||
// Excapsulate all this in a tea.Cmd() because bubbletea demands it.
|
||||
func (a app) waitForChannelSignal() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
content, open := <-a.channel
|
||||
if !open {
|
||||
return tea.Quit()
|
||||
}
|
||||
return content
|
||||
}
|
||||
}
|
||||
|
||||
func (a app) Init() tea.Cmd {
|
||||
client = websocket.Connect(config.URL)
|
||||
client.Password = config.Password
|
||||
go client.ProcessServerMessages(a.channel)
|
||||
|
||||
return tea.Batch(
|
||||
tea.ClearScreen,
|
||||
a.waitForChannelSignal(),
|
||||
)
|
||||
}
|
||||
|
||||
func Start() {
|
||||
var (
|
||||
defaultConfigFile = "~/.config/ChronoTomato.yml"
|
||||
|
||||
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")
|
||||
)
|
||||
flag.Parse()
|
||||
|
||||
// read passed config file or try to use default config
|
||||
if *configfile != "" {
|
||||
config = helper.ParseConfig(*configfile)
|
||||
} else {
|
||||
if helper.FileExists(defaultConfigFile) {
|
||||
config = helper.ParseConfig(defaultConfigFile)
|
||||
}
|
||||
}
|
||||
|
||||
// cli parameters always supersede config file
|
||||
if *parameter_url != "" {
|
||||
config.URL = *parameter_url
|
||||
}
|
||||
if *parameter_password != "" {
|
||||
config.Password = *parameter_password
|
||||
}
|
||||
|
||||
_, err := tea.NewProgram(myApp()).Run()
|
||||
if err != nil {
|
||||
log.Fatal("Could not start program:", "msg", err)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue