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