1package statusbar
2
3import (
4 tea "github.com/charmbracelet/bubbletea"
5 "github.com/charmbracelet/lipgloss"
6 "github.com/charmbracelet/soft-serve/ui/common"
7 "github.com/muesli/reflow/truncate"
8)
9
10type StatusBarMsg struct {
11 Key string
12 Value string
13 Info string
14 Branch string
15}
16
17type StatusBar struct {
18 common common.Common
19 msg StatusBarMsg
20}
21
22type Model interface {
23 StatusBarValue() string
24 StatusBarInfo() string
25}
26
27func New(c common.Common) *StatusBar {
28 s := &StatusBar{
29 common: c,
30 }
31 return s
32}
33
34func (s *StatusBar) SetSize(width, height int) {
35 s.common.Width = width
36 s.common.Height = height
37}
38
39func (s *StatusBar) Init() tea.Cmd {
40 return nil
41}
42
43func (s *StatusBar) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
44 switch msg := msg.(type) {
45 case StatusBarMsg:
46 s.msg = msg
47 }
48 return s, nil
49}
50
51func (s *StatusBar) View() string {
52 st := s.common.Styles
53 w := lipgloss.Width
54 help := st.StatusBarHelp.Render("? Help")
55 key := st.StatusBarKey.Render(s.msg.Key)
56 info := ""
57 if s.msg.Info != "" {
58 info = st.StatusBarInfo.Render(s.msg.Info)
59 }
60 branch := st.StatusBarBranch.Render(s.msg.Branch)
61 maxWidth := s.common.Width - w(key) - w(info) - w(branch) - w(help)
62 v := truncate.StringWithTail(s.msg.Value, uint(maxWidth-st.StatusBarValue.GetHorizontalFrameSize()), "…")
63 value := st.StatusBarValue.
64 Width(maxWidth).
65 Render(v)
66
67 return lipgloss.JoinHorizontal(lipgloss.Top,
68 key,
69 value,
70 info,
71 branch,
72 help,
73 )
74}