-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection.go
61 lines (50 loc) · 1.52 KB
/
section.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
package main
import (
"fmt"
"strings"
"github.com/charmbracelet/bubbles/viewport"
"github.com/charmbracelet/lipgloss"
)
type Section struct {
viewport viewport.Model
title string
subtitle string
}
var borderColor = lipgloss.Color("#452297")
var titleStyle lipgloss.Style = lipgloss.NewStyle().Background(borderColor)
var topBarStyle lipgloss.Style = lipgloss.NewStyle().Foreground(borderColor)
func (s Section) View() string {
var buf strings.Builder
buf.WriteString(titleStyle.Render(fmt.Sprintf(" %s ", s.title)))
var subtitle string
if s.subtitle != "" {
subtitle = fmt.Sprintf(" %s ", s.subtitle)
buf.WriteString(subtitle)
}
topBarLength := s.viewport.Width - lipgloss.Width(s.title) - lipgloss.Width(subtitle) - 3
buf.WriteString(topBarStyle.Render(strings.Repeat("─", max(0, topBarLength)) + "╗"))
buf.WriteString("\n")
buf.WriteString(s.viewport.View())
return buf.String()
}
func NewSection(title string, width, height int) Section {
vp := viewport.New(width-1, height-1)
vp.Style = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder(), false, true, true, true).
BorderForeground(borderColor).
PaddingRight(2).PaddingLeft(2)
return Section{title: title, viewport: vp}
}
func (s *Section) SetContent(content string) {
s.viewport.SetContent("\n" + content)
}
func (s *Section) SetDimensions(width, height int) {
s.viewport.Width = width
s.viewport.Height = height
}
func (s *Section) LineUp(lines int) {
s.viewport.LineUp(lines)
}
func (s *Section) LineDown(lines int) {
s.viewport.LineDown(lines)
}