1package statusbar
2
3import (
4 tea "github.com/charmbracelet/bubbletea/v2"
5 lipgloss "github.com/charmbracelet/lipgloss/v2"
6 "github.com/charmbracelet/soft-serve/pkg/ui/common"
7 "github.com/charmbracelet/x/ansi"
8)
9
10// Model is a status bar model.
11type Model struct {
12 common common.Common
13 key string
14 value string
15 info string
16 extra string
17}
18
19// New creates a new status bar component.
20func New(c common.Common) *Model {
21 s := &Model{
22 common: c,
23 }
24 return s
25}
26
27// SetSize implements common.Component.
28func (s *Model) SetSize(width, height int) {
29 s.common.Width = width
30 s.common.Height = height
31}
32
33// SetStatus sets the status bar status.
34func (s *Model) SetStatus(key, value, info, extra string) {
35 if key != "" {
36 s.key = key
37 }
38 if value != "" {
39 s.value = value
40 }
41 if info != "" {
42 s.info = info
43 }
44 if extra != "" {
45 s.extra = extra
46 }
47}
48
49// Init implements tea.Model.
50func (s *Model) Init() tea.Cmd {
51 return nil
52}
53
54// Update implements tea.Model.
55func (s *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
56 switch msg := msg.(type) {
57 case tea.WindowSizeMsg:
58 s.SetSize(msg.Width, msg.Height)
59 }
60 return s, nil
61}
62
63// View implements tea.Model.
64func (s *Model) View() string {
65 st := s.common.Styles
66 w := lipgloss.Width
67 help := s.common.Zone.Mark(
68 "repo-help",
69 st.StatusBarHelp.Render("? Help"),
70 )
71 key := st.StatusBarKey.Render(s.key)
72 info := ""
73 if s.info != "" {
74 info = st.StatusBarInfo.Render(s.info)
75 }
76 branch := st.StatusBarBranch.Render(s.extra)
77 maxWidth := s.common.Width - w(key) - w(info) - w(branch) - w(help)
78 v := ansi.Truncate(s.value, maxWidth-st.StatusBarValue.GetHorizontalFrameSize(), "…")
79 value := st.StatusBarValue.
80 Width(maxWidth).
81 Render(v)
82
83 return lipgloss.NewStyle().MaxWidth(s.common.Width).
84 Render(
85 lipgloss.JoinHorizontal(lipgloss.Top,
86 key,
87 value,
88 info,
89 branch,
90 help,
91 ),
92 )
93}