sidebar.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package chat
  2. import (
  3. "fmt"
  4. tea "github.com/charmbracelet/bubbletea"
  5. "github.com/charmbracelet/lipgloss"
  6. "github.com/kujtimiihoxha/termai/internal/config"
  7. "github.com/kujtimiihoxha/termai/internal/tui/styles"
  8. "github.com/kujtimiihoxha/termai/internal/version"
  9. )
  10. type sidebarCmp struct {
  11. width, height int
  12. }
  13. func (m *sidebarCmp) Init() tea.Cmd {
  14. return nil
  15. }
  16. func (m *sidebarCmp) Update(tea.Msg) (tea.Model, tea.Cmd) {
  17. return m, nil
  18. }
  19. func (m *sidebarCmp) View() string {
  20. return styles.BaseStyle.Width(m.width).Render(
  21. lipgloss.JoinVertical(
  22. lipgloss.Top,
  23. m.header(),
  24. " ",
  25. m.session(),
  26. " ",
  27. m.modifiedFiles(),
  28. " ",
  29. m.lspsConfigured(),
  30. ),
  31. )
  32. }
  33. func (m *sidebarCmp) session() string {
  34. sessionKey := styles.BaseStyle.Foreground(styles.PrimaryColor).Render("Session")
  35. sessionValue := styles.BaseStyle.
  36. Foreground(styles.Forground).
  37. Width(m.width - lipgloss.Width(sessionKey)).
  38. Render(": New Session")
  39. return lipgloss.JoinHorizontal(
  40. lipgloss.Left,
  41. sessionKey,
  42. sessionValue,
  43. )
  44. }
  45. func (m *sidebarCmp) modifiedFile(filePath string, additions, removals int) string {
  46. stats := ""
  47. if additions > 0 && removals > 0 {
  48. stats = styles.BaseStyle.Foreground(styles.ForgroundDim).Render(fmt.Sprintf("%d additions and %d removals", additions, removals))
  49. } else if additions > 0 {
  50. stats = styles.BaseStyle.Foreground(styles.ForgroundDim).Render(fmt.Sprintf("%d additions", additions))
  51. } else if removals > 0 {
  52. stats = styles.BaseStyle.Foreground(styles.ForgroundDim).Render(fmt.Sprintf("%d removals", removals))
  53. }
  54. filePathStr := styles.BaseStyle.Foreground(styles.Forground).Render(filePath)
  55. return styles.BaseStyle.
  56. Width(m.width).
  57. Render(
  58. lipgloss.JoinHorizontal(
  59. lipgloss.Left,
  60. filePathStr,
  61. " ",
  62. stats,
  63. ),
  64. )
  65. }
  66. func (m *sidebarCmp) lspsConfigured() string {
  67. lsps := styles.BaseStyle.Width(m.width).Foreground(styles.PrimaryColor).Render("LSP Configuration:")
  68. lspsConfigured := []struct {
  69. name string
  70. path string
  71. }{
  72. {"golsp", "path/to/lsp1"},
  73. {"vtsls", "path/to/lsp2"},
  74. }
  75. var lspViews []string
  76. for _, lsp := range lspsConfigured {
  77. lspName := styles.BaseStyle.Foreground(styles.Forground).Render(
  78. fmt.Sprintf("• %s", lsp.name),
  79. )
  80. lspPath := styles.BaseStyle.Foreground(styles.ForgroundDim).Render(
  81. fmt.Sprintf("(%s)", lsp.path),
  82. )
  83. lspViews = append(lspViews,
  84. styles.BaseStyle.
  85. Width(m.width).
  86. Render(
  87. lipgloss.JoinHorizontal(
  88. lipgloss.Left,
  89. lspName,
  90. " ",
  91. lspPath,
  92. ),
  93. ),
  94. )
  95. }
  96. return styles.BaseStyle.
  97. Width(m.width).
  98. Render(
  99. lipgloss.JoinVertical(
  100. lipgloss.Left,
  101. lsps,
  102. lipgloss.JoinVertical(
  103. lipgloss.Left,
  104. lspViews...,
  105. ),
  106. ),
  107. )
  108. }
  109. func (m *sidebarCmp) modifiedFiles() string {
  110. modifiedFiles := styles.BaseStyle.Width(m.width).Foreground(styles.PrimaryColor).Render("Modified Files:")
  111. files := []struct {
  112. path string
  113. additions int
  114. removals int
  115. }{
  116. {"file1.txt", 10, 5},
  117. {"file2.txt", 20, 0},
  118. {"file3.txt", 0, 15},
  119. }
  120. var fileViews []string
  121. for _, file := range files {
  122. fileViews = append(fileViews, m.modifiedFile(file.path, file.additions, file.removals))
  123. }
  124. return styles.BaseStyle.
  125. Width(m.width).
  126. Render(
  127. lipgloss.JoinVertical(
  128. lipgloss.Top,
  129. modifiedFiles,
  130. lipgloss.JoinVertical(
  131. lipgloss.Left,
  132. fileViews...,
  133. ),
  134. ),
  135. )
  136. }
  137. func (m *sidebarCmp) logo() string {
  138. logo := fmt.Sprintf("%s %s", styles.OpenCodeIcon, "OpenCode")
  139. version := styles.BaseStyle.Foreground(styles.ForgroundDim).Render(version.Version)
  140. return styles.BaseStyle.
  141. Bold(true).
  142. Width(m.width).
  143. Render(
  144. lipgloss.JoinHorizontal(
  145. lipgloss.Left,
  146. logo,
  147. " ",
  148. version,
  149. ),
  150. )
  151. }
  152. func (m *sidebarCmp) header() string {
  153. header := lipgloss.JoinVertical(
  154. lipgloss.Top,
  155. m.logo(),
  156. m.cwd(),
  157. )
  158. return header
  159. }
  160. func (m *sidebarCmp) cwd() string {
  161. cwd := fmt.Sprintf("cwd: %s", config.WorkingDirectory())
  162. return styles.BaseStyle.
  163. Foreground(styles.ForgroundDim).
  164. Width(m.width).
  165. Render(cwd)
  166. }
  167. func (m *sidebarCmp) SetSize(width, height int) {
  168. m.width = width
  169. m.height = height
  170. }
  171. func (m *sidebarCmp) GetSize() (int, int) {
  172. return m.width, m.height
  173. }
  174. func NewSidebarCmp() tea.Model {
  175. return &sidebarCmp{}
  176. }