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