statusbar.go

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