Persist server configuration to config file

This commit is contained in:
2023-02-25 01:24:22 -07:00
parent a41c12d20c
commit 3fef157f6a
7 changed files with 110 additions and 53 deletions
+14 -10
View File
@@ -4,6 +4,9 @@ import (
"fmt"
"io"
"git.ohea.xyz/cursorius/tui/settings"
"git.ohea.xyz/golang/config"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
@@ -11,7 +14,7 @@ import (
type Login struct {
selected int
servers []CursoriusServer
config config.Config[settings.Config]
serverList list.Model
}
@@ -32,27 +35,28 @@ func (m Login) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch msg.String() {
case "e":
if m.serverList.Index() < len(m.servers) {
if m.serverList.Index() < len(m.config.Config.Servers) {
return m, func() tea.Msg {
return ScreenSwitchMsg{
NewScreen: createEditServer(
m.servers,
m.config,
m.serverList.Index(),
),
}
}
}
case "enter":
if m.serverList.Index() < len(m.servers) {
if m.serverList.Index() < len(m.config.Config.Servers) {
return m, func() tea.Msg {
return m.servers[m.serverList.Index()].Login()
return ServerLogin(m.config.Config.Servers[m.serverList.Index()])
}
} else {
return m, func() tea.Msg {
m.config.Config.Servers = append(m.config.Config.Servers, settings.CursoriusServer{})
return ScreenSwitchMsg{
NewScreen: createEditServer(
append(m.servers, CursoriusServer{}),
len(m.servers),
m.config,
len(m.config.Config.Servers)-1,
),
}
}
@@ -114,9 +118,9 @@ func (d loginItemDelegate) Render(w io.Writer, m list.Model, index int, listItem
fmt.Fprint(w, fn(str))
}
func CreateLogin(servers []CursoriusServer) Login {
func CreateLogin(conf config.Config[settings.Config]) Login {
items := []list.Item{}
for _, server := range servers {
for _, server := range conf.Config.Servers {
items = append(
items,
serverItem{
@@ -137,7 +141,7 @@ func CreateLogin(servers []CursoriusServer) Login {
return Login{
selected: 0,
servers: servers,
config: conf,
serverList: l,
}
}