1package ui
2
3import (
4 "strings"
5
6 "github.com/charmbracelet/lipgloss"
7)
8
9func TitledBox(title, content string, styles Styles, width int, activeSession bool, height ...int) string {
10
11 style := styles.SectionBorder
12 if activeSession {
13 style = styles.SectionBorderActive
14 title = title + " ●"
15 }
16
17 borderColor := style.GetBorderTopForeground()
18
19 boxWidth := 0
20 if width != -1 {
21 boxWidth = width
22 }
23
24 style = style.Padding(1, 2)
25
26 if boxWidth > 0 {
27 style = style.Width(boxWidth)
28 }
29 if len(height) > 0 {
30 style = style.Height(height[0])
31 }
32
33 box := style.Render(content)
34
35 lines := strings.Split(box, "\n")
36 if len(lines) == 0 {
37 return box
38 }
39
40 styledTitle := lipgloss.NewStyle().
41 Foreground(styles.SectionTitle.GetForeground()).
42 Bold(true).
43 Render(" " + title + " ")
44
45 topBorderWidth := lipgloss.Width(lines[0])
46 titleWidth := lipgloss.Width(styledTitle)
47
48 dashesAfterTitle := topBorderWidth - titleWidth - 3
49
50 if dashesAfterTitle > 0 {
51 cornerAndDash := lipgloss.NewStyle().Foreground(borderColor).Render("╭─")
52 dashes := lipgloss.NewStyle().Foreground(borderColor).Render(strings.Repeat("─", dashesAfterTitle))
53 corner := lipgloss.NewStyle().Foreground(borderColor).Render("╮")
54 lines[0] = cornerAndDash + styledTitle + dashes + corner
55 }
56
57 return strings.Join(lines, "\n")
58}