This repository has been archived on 2024-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
Files
cairde/internal/models/main.go

123 lines
2.3 KiB
Go

package models
import (
"path"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
const (
statusPane = iota
)
var mainStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder())
type mainModel struct {
username string
userDir string
width int
height int
titleModel titleModel
profileBarModel profileBarModel
inputModel inputModel
paneState int
statusPaneModel statusPaneModel
}
func NewMainModel(username, homeDir string) mainModel {
// TODO(d1): integrate from build system later
titleModel := newTitleModel("cairde", "v0.1.0")
inputModel := newInputModel()
// TODO(d1): programmatically fill in profile names
profileBarModel := newProfileBarModel([]string{
"status", "profile1", "profile2", "profile3",
})
statusPaneModel := newStatusPaneModel()
return mainModel{
username: username,
userDir: path.Join(homeDir, "/.cairde/"),
titleModel: titleModel,
profileBarModel: profileBarModel,
inputModel: inputModel,
statusPaneModel: statusPaneModel,
paneState: statusPane,
}
}
func (m mainModel) Init() tea.Cmd {
return nil
}
func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmds []tea.Cmd
)
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width - 2
m.height = msg.Height - 2
msg.Height -= 1
msg.Width -= 4
m.statusPaneModel.heightOffset = m.titleModel.height + m.profileBarModel.height + m.inputModel.height
return m.propagate(msg), nil
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
}
}
return m.propagate(msg), tea.Batch(cmds...)
}
func (m mainModel) propagate(msg tea.Msg) tea.Model {
m.titleModel, _ = m.titleModel.Update(msg)
m.statusPaneModel, _ = m.statusPaneModel.Update(msg)
m.inputModel, _ = m.inputModel.Update(msg)
return m
}
func (m mainModel) View() string {
body := strings.Builder{}
var activePaneRender string
switch m.paneState {
case 0:
activePaneRender = m.statusPaneModel.View()
}
body.WriteString(
mainStyle.
Width(m.width).
Height(m.height).
Render(
lipgloss.JoinVertical(
lipgloss.Top,
m.titleModel.View(),
activePaneRender,
m.profileBarModel.View(),
m.inputModel.View(),
),
),
)
return body.String()
}