TUI for The Lounge
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

93 lines
1.7 KiB

package main
import (
tea "github.com/charmbracelet/bubbletea"
)
type viewType int
const (
viewNone viewType = 0
viewLogin viewType = 1
viewChannels viewType = 2
viewChat viewType = 3
)
type mainModel struct {
conf *config
conn *loungeConnection
view viewType
width int
height int
loginModel loginModel
channelsModel channelsModel
chatModel chatModel
}
func mainModelInit() mainModel {
conf := configLoad()
conn := &loungeConnection{User: conf.User, Token: conf.Token, Action: actionNone}
login := loginModelInit(conf)
channels := channelsModelInit()
chat := chatModelInit()
return mainModel{conf, conn, viewLogin, 0, 0, login, channels, chat}
}
func (m mainModel) Init() tea.Cmd {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
cmd = m.loginInit(m.conf)
cmds = append(cmds, cmd)
cmd = m.channelsInit()
cmds = append(cmds, cmd)
cmd = m.chatInit()
cmds = append(cmds, cmd)
return tea.Batch(cmds...)
}
func (m mainModel) View() string {
s := ""
switch m.view {
case viewLogin:
s = m.loginView()
case viewChannels:
s = m.channelsView()
case viewChat:
s = m.chatView()
}
return s
}
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var model tea.Model = m
var cmd tea.Cmd = nil
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if m.conn.Action != actionNone {
m = connActionHandle(m)
} else {
m.width = msg.Width
m.height = msg.Height
}
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
return m, tea.Quit
case "ctrl+s":
if m.view != viewLogin {
m.view = viewChannels
}
}
}
switch m.view {
case viewLogin:
model, cmd = m.loginUpdate(msg)
case viewChannels:
model, cmd = m.channelsUpdate(msg)
case viewChat:
model, cmd = m.chatUpdate(msg)
}
return model, cmd
}