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	key := st.StatusBarKey.Render(s.msg.Key)
55	info := ""
56	if s.msg.Info != "" {
57		info = st.StatusBarInfo.Render(s.msg.Info)
58	}
59	branch := st.StatusBarBranch.Render(s.msg.Branch)
60	maxWidth := s.common.Width - w(key) - w(info) - w(branch)
61	v := truncate.StringWithTail(s.msg.Value, uint(maxWidth-st.StatusBarValue.GetHorizontalFrameSize()), "…")
62	value := st.StatusBarValue.
63		Width(maxWidth).
64		Render(v)
65
66	return lipgloss.JoinHorizontal(lipgloss.Top,
67		key,
68		value,
69		info,
70		branch,
71	)
72}