1package chat
2
3import (
4 "fmt"
5
6 tea "github.com/charmbracelet/bubbletea"
7 "github.com/charmbracelet/lipgloss"
8 "github.com/kujtimiihoxha/termai/internal/session"
9 "github.com/kujtimiihoxha/termai/internal/tui/styles"
10)
11
12type sidebarCmp struct {
13 width, height int
14 session session.Session
15}
16
17func (m *sidebarCmp) Init() tea.Cmd {
18 return nil
19}
20
21func (m *sidebarCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
22 return m, nil
23}
24
25func (m *sidebarCmp) View() string {
26 return styles.BaseStyle.
27 Width(m.width).
28 Height(m.height - 1).
29 Render(
30 lipgloss.JoinVertical(
31 lipgloss.Top,
32 header(m.width),
33 " ",
34 m.sessionSection(),
35 " ",
36 m.modifiedFiles(),
37 " ",
38 lspsConfigured(m.width),
39 ),
40 )
41}
42
43func (m *sidebarCmp) sessionSection() string {
44 sessionKey := styles.BaseStyle.Foreground(styles.PrimaryColor).Bold(true).Render("Session")
45 sessionValue := styles.BaseStyle.
46 Foreground(styles.Forground).
47 Width(m.width - lipgloss.Width(sessionKey)).
48 Render(": New Session")
49 return lipgloss.JoinHorizontal(
50 lipgloss.Left,
51 sessionKey,
52 sessionValue,
53 )
54}
55
56func (m *sidebarCmp) modifiedFile(filePath string, additions, removals int) string {
57 stats := ""
58 if additions > 0 && removals > 0 {
59 stats = styles.BaseStyle.Foreground(styles.ForgroundDim).Render(fmt.Sprintf(" %d additions and %d removals", additions, removals))
60 } else if additions > 0 {
61 stats = styles.BaseStyle.Foreground(styles.ForgroundDim).Render(fmt.Sprintf(" %d additions", additions))
62 } else if removals > 0 {
63 stats = styles.BaseStyle.Foreground(styles.ForgroundDim).Render(fmt.Sprintf(" %d removals", removals))
64 }
65 filePathStr := styles.BaseStyle.Foreground(styles.Forground).Render(filePath)
66
67 return styles.BaseStyle.
68 Width(m.width).
69 Render(
70 lipgloss.JoinHorizontal(
71 lipgloss.Left,
72 filePathStr,
73 stats,
74 ),
75 )
76}
77
78func (m *sidebarCmp) modifiedFiles() string {
79 modifiedFiles := styles.BaseStyle.Width(m.width).Foreground(styles.PrimaryColor).Bold(true).Render("Modified Files:")
80 files := []struct {
81 path string
82 additions int
83 removals int
84 }{
85 {"file1.txt", 10, 5},
86 {"file2.txt", 20, 0},
87 {"file3.txt", 0, 15},
88 }
89 var fileViews []string
90 for _, file := range files {
91 fileViews = append(fileViews, m.modifiedFile(file.path, file.additions, file.removals))
92 }
93
94 return styles.BaseStyle.
95 Width(m.width).
96 Render(
97 lipgloss.JoinVertical(
98 lipgloss.Top,
99 modifiedFiles,
100 lipgloss.JoinVertical(
101 lipgloss.Left,
102 fileViews...,
103 ),
104 ),
105 )
106}
107
108func (m *sidebarCmp) SetSize(width, height int) {
109 m.width = width
110 m.height = height
111}
112
113func (m *sidebarCmp) GetSize() (int, int) {
114 return m.width, m.height
115}
116
117func NewSidebarCmp(session session.Session) tea.Model {
118 return &sidebarCmp{
119 session: session,
120 }
121}