status.go

 1package core
 2
 3import (
 4	tea "github.com/charmbracelet/bubbletea"
 5	"github.com/charmbracelet/lipgloss"
 6	"github.com/kujtimiihoxha/termai/internal/config"
 7	"github.com/kujtimiihoxha/termai/internal/llm/models"
 8	"github.com/kujtimiihoxha/termai/internal/tui/styles"
 9	"github.com/kujtimiihoxha/termai/internal/tui/util"
10	"github.com/kujtimiihoxha/termai/internal/version"
11)
12
13type statusCmp struct {
14	err   error
15	info  string
16	width int
17}
18
19func (m statusCmp) Init() tea.Cmd {
20	return nil
21}
22
23func (m statusCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
24	switch msg := msg.(type) {
25	case tea.WindowSizeMsg:
26		m.width = msg.Width
27	case util.ErrorMsg:
28		m.err = msg
29	case util.InfoMsg:
30		m.info = string(msg)
31	}
32	return m, nil
33}
34
35var (
36	versionWidget = styles.Padded.Background(styles.DarkGrey).Foreground(styles.Text).Render(version.Version)
37	helpWidget    = styles.Padded.Background(styles.Grey).Foreground(styles.Text).Render("? help")
38)
39
40func (m statusCmp) View() string {
41	status := styles.Padded.Background(styles.Grey).Foreground(styles.Text).Render("? help")
42
43	if m.err != nil {
44		status += styles.Regular.Padding(0, 1).
45			Background(styles.Red).
46			Foreground(styles.Text).
47			Width(m.availableFooterMsgWidth()).
48			Render(m.err.Error())
49	} else if m.info != "" {
50		status += styles.Padded.
51			Foreground(styles.Base).
52			Background(styles.Green).
53			Width(m.availableFooterMsgWidth()).
54			Render(m.info)
55	} else {
56		status += styles.Padded.
57			Foreground(styles.Base).
58			Background(styles.LightGrey).
59			Width(m.availableFooterMsgWidth()).
60			Render(m.info)
61	}
62	status += m.model()
63	status += versionWidget
64	return status
65}
66
67func (m statusCmp) availableFooterMsgWidth() int {
68	// -2 to accommodate padding
69	return max(0, m.width-lipgloss.Width(helpWidget)-lipgloss.Width(versionWidget)-lipgloss.Width(m.model()))
70}
71
72func (m statusCmp) model() string {
73	model := models.SupportedModels[config.Get().Model.Coder]
74	return styles.Padded.Background(styles.Grey).Foreground(styles.Text).Render(model.Name)
75}
76
77func NewStatusCmp() tea.Model {
78	return &statusCmp{}
79}