sidebar.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package chat
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. tea "github.com/charmbracelet/bubbletea"
  7. "github.com/charmbracelet/lipgloss"
  8. "github.com/sst/opencode/internal/config"
  9. "github.com/sst/opencode/internal/tui/app"
  10. "github.com/sst/opencode/internal/tui/state"
  11. "github.com/sst/opencode/internal/tui/styles"
  12. "github.com/sst/opencode/internal/tui/theme"
  13. )
  14. type sidebarCmp struct {
  15. app *app.App
  16. width, height int
  17. modFiles map[string]struct {
  18. additions int
  19. removals int
  20. }
  21. }
  22. func (m *sidebarCmp) Init() tea.Cmd {
  23. // TODO: History service not implemented in API yet
  24. // Initialize the modified files map
  25. m.modFiles = make(map[string]struct {
  26. additions int
  27. removals int
  28. })
  29. return nil
  30. }
  31. func (m *sidebarCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  32. switch msg.(type) {
  33. case state.SessionSelectedMsg:
  34. // TODO: History service not implemented in API yet
  35. // ctx := context.Background()
  36. // m.loadModifiedFiles(ctx)
  37. // case pubsub.Event[history.File]:
  38. // TODO: History service not implemented in API yet
  39. // if msg.Payload.SessionID == m.app.CurrentSession.ID {
  40. // // Process the individual file change instead of reloading all files
  41. // ctx := context.Background()
  42. // m.processFileChanges(ctx, msg.Payload)
  43. // }
  44. }
  45. return m, nil
  46. }
  47. func (m *sidebarCmp) View() string {
  48. t := theme.CurrentTheme()
  49. baseStyle := styles.BaseStyle()
  50. shareUrl := baseStyle.Foreground(t.TextMuted()).Render("https://dev.opencode.ai/share?id=" + m.app.Session.Id)
  51. // qrcode := ""
  52. // if m.app.Session.ShareID != nil {
  53. // url := "https://dev.opencode.ai/share?id="
  54. // qrcode, _, _ = qr.Generate(url + m.app.Session.Id)
  55. // }
  56. return baseStyle.
  57. Width(m.width).
  58. PaddingLeft(4).
  59. PaddingRight(1).
  60. Render(
  61. lipgloss.JoinVertical(
  62. lipgloss.Top,
  63. header(m.width),
  64. " ",
  65. m.sessionSection(),
  66. shareUrl,
  67. ),
  68. )
  69. }
  70. func (m *sidebarCmp) sessionSection() string {
  71. t := theme.CurrentTheme()
  72. baseStyle := styles.BaseStyle()
  73. sessionKey := baseStyle.
  74. Foreground(t.Primary()).
  75. Bold(true).
  76. Render("Session")
  77. sessionValue := baseStyle.
  78. Foreground(t.Text()).
  79. Render(fmt.Sprintf(": %s", m.app.Session.Title))
  80. return sessionKey + sessionValue
  81. }
  82. func (m *sidebarCmp) modifiedFile(filePath string, additions, removals int) string {
  83. t := theme.CurrentTheme()
  84. baseStyle := styles.BaseStyle()
  85. stats := ""
  86. if additions > 0 && removals > 0 {
  87. additionsStr := baseStyle.
  88. Foreground(t.Success()).
  89. PaddingLeft(1).
  90. Render(fmt.Sprintf("+%d", additions))
  91. removalsStr := baseStyle.
  92. Foreground(t.Error()).
  93. PaddingLeft(1).
  94. Render(fmt.Sprintf("-%d", removals))
  95. content := lipgloss.JoinHorizontal(lipgloss.Left, additionsStr, removalsStr)
  96. stats = baseStyle.Width(lipgloss.Width(content)).Render(content)
  97. } else if additions > 0 {
  98. additionsStr := fmt.Sprintf(" %s", baseStyle.
  99. PaddingLeft(1).
  100. Foreground(t.Success()).
  101. Render(fmt.Sprintf("+%d", additions)))
  102. stats = baseStyle.Width(lipgloss.Width(additionsStr)).Render(additionsStr)
  103. } else if removals > 0 {
  104. removalsStr := fmt.Sprintf(" %s", baseStyle.
  105. PaddingLeft(1).
  106. Foreground(t.Error()).
  107. Render(fmt.Sprintf("-%d", removals)))
  108. stats = baseStyle.Width(lipgloss.Width(removalsStr)).Render(removalsStr)
  109. }
  110. filePathStr := baseStyle.Render(filePath)
  111. return baseStyle.
  112. Width(m.width).
  113. Render(
  114. lipgloss.JoinHorizontal(
  115. lipgloss.Left,
  116. filePathStr,
  117. stats,
  118. ),
  119. )
  120. }
  121. func (m *sidebarCmp) modifiedFiles() string {
  122. t := theme.CurrentTheme()
  123. baseStyle := styles.BaseStyle()
  124. modifiedFiles := baseStyle.
  125. Width(m.width).
  126. Foreground(t.Primary()).
  127. Bold(true).
  128. Render("Modified Files:")
  129. // If no modified files, show a placeholder message
  130. if m.modFiles == nil || len(m.modFiles) == 0 {
  131. message := "No modified files"
  132. remainingWidth := m.width - lipgloss.Width(message)
  133. if remainingWidth > 0 {
  134. message += strings.Repeat(" ", remainingWidth)
  135. }
  136. return baseStyle.
  137. Width(m.width).
  138. Render(
  139. lipgloss.JoinVertical(
  140. lipgloss.Top,
  141. modifiedFiles,
  142. baseStyle.Foreground(t.TextMuted()).Render(message),
  143. ),
  144. )
  145. }
  146. // Sort file paths alphabetically for consistent ordering
  147. var paths []string
  148. for path := range m.modFiles {
  149. paths = append(paths, path)
  150. }
  151. sort.Strings(paths)
  152. // Create views for each file in sorted order
  153. var fileViews []string
  154. for _, path := range paths {
  155. stats := m.modFiles[path]
  156. fileViews = append(fileViews, m.modifiedFile(path, stats.additions, stats.removals))
  157. }
  158. return baseStyle.
  159. Width(m.width).
  160. Render(
  161. lipgloss.JoinVertical(
  162. lipgloss.Top,
  163. modifiedFiles,
  164. lipgloss.JoinVertical(
  165. lipgloss.Left,
  166. fileViews...,
  167. ),
  168. ),
  169. )
  170. }
  171. func (m *sidebarCmp) SetSize(width, height int) tea.Cmd {
  172. m.width = width
  173. m.height = height
  174. return nil
  175. }
  176. func (m *sidebarCmp) GetSize() (int, int) {
  177. return m.width, m.height
  178. }
  179. func NewSidebarCmp(app *app.App) tea.Model {
  180. return &sidebarCmp{
  181. app: app,
  182. }
  183. }
  184. // Helper function to get the display path for a file
  185. func getDisplayPath(path string) string {
  186. workingDir := config.WorkingDirectory()
  187. displayPath := strings.TrimPrefix(path, workingDir)
  188. return strings.TrimPrefix(displayPath, "/")
  189. }