-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathstatusbar.go
129 lines (113 loc) · 3.28 KB
/
statusbar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Package statusbar provides an statusbar bubble which can render
// four different status sections
package statusbar
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/exp/term/ansi"
)
// Height represents the height of the statusbar.
const Height = 1
// ColorConfig represents the the foreground and background
// color configuration.
type ColorConfig struct {
Foreground lipgloss.AdaptiveColor
Background lipgloss.AdaptiveColor
}
// Model represents the properties of the statusbar.
type Model struct {
Width int
Height int
FirstColumn string
SecondColumn string
ThirdColumn string
FourthColumn string
FirstColumnColors ColorConfig
SecondColumnColors ColorConfig
ThirdColumnColors ColorConfig
FourthColumnColors ColorConfig
}
// New creates a new instance of the statusbar.
func New(
firstColumnColors,
secondColumnColors,
thirdColumnColors,
fourthColumnColors ColorConfig,
) Model {
return Model{
FirstColumnColors: firstColumnColors,
SecondColumnColors: secondColumnColors,
ThirdColumnColors: thirdColumnColors,
FourthColumnColors: fourthColumnColors,
}
}
// SetSize sets the width of the statusbar.
func (m *Model) SetSize(width int) {
m.Width = width
}
// Update updates the UI of the statusbar.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if windowSizeMsg, ok := msg.(tea.WindowSizeMsg); ok {
m.SetSize(windowSizeMsg.Width)
}
return m, nil
}
// SetContent sets the content of the statusbar.
func (m *Model) SetContent(firstColumn, secondColumn, thirdColumn, fourthColumn string) {
m.FirstColumn = firstColumn
m.SecondColumn = secondColumn
m.ThirdColumn = thirdColumn
m.FourthColumn = fourthColumn
}
// SetColors sets the colors of the 4 columns.
func (m *Model) SetColors(
firstColumnColors,
secondColumnColors,
thirdColumnColors,
fourthColumnColors ColorConfig,
) {
m.FirstColumnColors = firstColumnColors
m.SecondColumnColors = secondColumnColors
m.ThirdColumnColors = thirdColumnColors
m.FourthColumnColors = fourthColumnColors
}
// View returns a string representation of the statusbar.
func (m Model) View() string {
width := lipgloss.Width
firstColumn := lipgloss.NewStyle().
Foreground(m.FirstColumnColors.Foreground).
Background(m.FirstColumnColors.Background).
Padding(0, 1).
Height(Height).
Render(ansi.Truncate(m.FirstColumn, 30, "..."))
thirdColumn := lipgloss.NewStyle().
Foreground(m.ThirdColumnColors.Foreground).
Background(m.ThirdColumnColors.Background).
Align(lipgloss.Right).
Padding(0, 1).
Height(Height).
Render(m.ThirdColumn)
fourthColumn := lipgloss.NewStyle().
Foreground(m.FourthColumnColors.Foreground).
Background(m.FourthColumnColors.Background).
Padding(0, 1).
Height(Height).
Render(m.FourthColumn)
secondColumn := lipgloss.NewStyle().
Foreground(m.SecondColumnColors.Foreground).
Background(m.SecondColumnColors.Background).
Padding(0, 1).
Height(Height).
Width(m.Width - width(firstColumn) - width(thirdColumn) - width(fourthColumn)).
Render(ansi.Truncate(
m.SecondColumn,
m.Width-width(firstColumn)-width(thirdColumn)-width(fourthColumn)-3,
"..."),
)
return lipgloss.JoinHorizontal(lipgloss.Top,
firstColumn,
secondColumn,
thirdColumn,
fourthColumn,
)
}