@@ -5,7 +5,6 @@ import (
11 tea "github.com/charmbracelet/bubbletea/v2"
10 tea "github.com/charmbracelet/bubbletea/v2"
@@ -13,21 +12,21 @@ import (
13 "github.com/charmbracelet/crush/internal/config"
12 "github.com/charmbracelet/crush/internal/config"
14 "github.com/charmbracelet/crush/internal/csync"
13 "github.com/charmbracelet/crush/internal/csync"
15 "github.com/charmbracelet/crush/internal/diff"
14 "github.com/charmbracelet/crush/internal/diff"
16− "github.com/charmbracelet/crush/internal/fsext"
17 "github.com/charmbracelet/crush/internal/history"
15 "github.com/charmbracelet/crush/internal/history"
18 "github.com/charmbracelet/crush/internal/lsp"
16 "github.com/charmbracelet/crush/internal/lsp"
19− "github.com/charmbracelet/crush/internal/lsp/protocol"
20 "github.com/charmbracelet/crush/internal/pubsub"
17 "github.com/charmbracelet/crush/internal/pubsub"
21 "github.com/charmbracelet/crush/internal/session"
18 "github.com/charmbracelet/crush/internal/session"
22 "github.com/charmbracelet/crush/internal/tui/components/chat"
19 "github.com/charmbracelet/crush/internal/tui/components/chat"
23 "github.com/charmbracelet/crush/internal/tui/components/core"
20 "github.com/charmbracelet/crush/internal/tui/components/core"
24 "github.com/charmbracelet/crush/internal/tui/components/core/layout"
21 "github.com/charmbracelet/crush/internal/tui/components/core/layout"
22+ "github.com/charmbracelet/crush/internal/tui/components/files"
25 "github.com/charmbracelet/crush/internal/tui/components/logo"
23 "github.com/charmbracelet/crush/internal/tui/components/logo"
24+ lspcomponent "github.com/charmbracelet/crush/internal/tui/components/lsp"
25+ "github.com/charmbracelet/crush/internal/tui/components/mcp"
26 "github.com/charmbracelet/crush/internal/tui/styles"
26 "github.com/charmbracelet/crush/internal/tui/styles"
27 "github.com/charmbracelet/crush/internal/tui/util"
27 "github.com/charmbracelet/crush/internal/tui/util"
28 "github.com/charmbracelet/crush/internal/version"
28 "github.com/charmbracelet/crush/internal/version"
29 "github.com/charmbracelet/lipgloss/v2"
29 "github.com/charmbracelet/lipgloss/v2"
30− "github.com/charmbracelet/x/ansi"
31 "golang.org/x/text/cases"
30 "golang.org/x/text/cases"
32 "golang.org/x/text/language"
31 "golang.org/x/text/language"
@@ -382,459 +381,125 @@ func (m *sidebarCmp) renderSectionsHorizontal() string {
383// filesBlockCompact renders the files block with limited width and height for horizontal layout
382// filesBlockCompact renders the files block with limited width and height for horizontal layout
384func (m *sidebarCmp) filesBlockCompact(maxWidth int) string {
383func (m *sidebarCmp) filesBlockCompact(maxWidth int) string {
385− t := styles.CurrentTheme()
384+ // Convert map to slice and handle type conversion
387− section := t.S().Subtle.Render("Modified Files")
389− files := slices.Collect(m.files.Seq())
385+ sessionFiles := slices.Collect(m.files.Seq())
390−
386+ fileSlice := make([]files.SessionFile, len(sessionFiles))
391− if len(files) == 0 {
387+ for i, sf := range sessionFiles {
392− content := lipgloss.JoinVertical(
388+ fileSlice[i] = files.SessionFile{
393− lipgloss.Left,
389+ History: files.FileHistory{
394− section,
390+ InitialVersion: sf.History.initialVersion,
395− "",
391+ LatestVersion: sf.History.latestVersion,
396− t.S().Base.Foreground(t.Border).Render("None"),
392+ },
397− )
393+ FilePath: sf.FilePath,
398− return lipgloss.NewStyle().Width(maxWidth).Render(content)
394+ Additions: sf.Additions,
395+ Deletions: sf.Deletions,
401− fileList := []string{section, ""}
402− sort.Slice(files, func(i, j int) bool {
403− return files[i].History.latestVersion.CreatedAt > files[j].History.latestVersion.CreatedAt
406− // Limit items for horizontal layout - use less space
399+ // Limit items for horizontal layout
407− maxItems := min(5, len(files))
400+ maxItems := min(5, len(fileSlice))
408 availableHeight := m.height - 8 // Reserve space for header and other content
401 availableHeight := m.height - 8 // Reserve space for header and other content
409 if availableHeight > 0 {
402 if availableHeight > 0 {
410 maxItems = min(maxItems, availableHeight)
403 maxItems = min(maxItems, availableHeight)
413− filesShown := 0
406+ return files.RenderFileBlock(fileSlice, files.RenderOptions{
414− for _, file := range files {
407+ MaxWidth: maxWidth,
415− if file.Additions == 0 && file.Deletions == 0 {
408+ MaxItems: maxItems,
416− continue
409+ ShowSection: true,
417− }
410+ SectionName: "Modified Files",
418− if filesShown >= maxItems {
411+ }, true)
422− var statusParts []string
423− if file.Additions > 0 {
424− statusParts = append(statusParts, t.S().Base.Foreground(t.Success).Render(fmt.Sprintf("+%d", file.Additions)))
426− if file.Deletions > 0 {
427− statusParts = append(statusParts, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("-%d", file.Deletions)))
430− extraContent := strings.Join(statusParts, " ")
431− cwd := config.Get().WorkingDir() + string(os.PathSeparator)
432− filePath := file.FilePath
433− filePath = strings.TrimPrefix(filePath, cwd)
434− filePath = fsext.DirTrim(fsext.PrettyPath(filePath), 2)
435− filePath = ansi.Truncate(filePath, maxWidth-lipgloss.Width(extraContent)-2, "…")
437− fileList = append(fileList,
440− IconColor: t.FgMuted,
443− ExtraContent: extraContent,
451− // Add "..." indicator if there are more files
452− totalFilesWithChanges := 0
453− for _, file := range files {
454− if file.Additions > 0 || file.Deletions > 0 {
455− totalFilesWithChanges++
458− if totalFilesWithChanges > maxItems {
459− fileList = append(fileList, t.S().Base.Foreground(t.FgMuted).Render("…"))
462− content := lipgloss.JoinVertical(lipgloss.Left, fileList...)
463− return lipgloss.NewStyle().Width(maxWidth).Render(content)
466// lspBlockCompact renders the LSP block with limited width and height for horizontal layout
414// lspBlockCompact renders the LSP block with limited width and height for horizontal layout
467func (m *sidebarCmp) lspBlockCompact(maxWidth int) string {
415func (m *sidebarCmp) lspBlockCompact(maxWidth int) string {
468− t := styles.CurrentTheme()
470− section := t.S().Subtle.Render("LSPs")
472− lspList := []string{section, ""}
474− lsp := config.Get().LSP.Sorted()
476− content := lipgloss.JoinVertical(
480− t.S().Base.Foreground(t.Border).Render("None"),
482− return lipgloss.NewStyle().Width(maxWidth).Render(content)
485 // Limit items for horizontal layout
416 // Limit items for horizontal layout
417+ lspConfigs := config.Get().LSP.Sorted()
486− maxItems := min(5, len(lsp))
418+ maxItems := min(5, len(lspConfigs))
487 availableHeight := m.height - 8
419 availableHeight := m.height - 8
488 if availableHeight > 0 {
420 if availableHeight > 0 {
489 maxItems = min(maxItems, availableHeight)
421 maxItems = min(maxItems, availableHeight)
492− for i, l := range lsp {
424+ return lspcomponent.RenderLSPBlock(m.lspClients, lspcomponent.RenderOptions{
493− if i >= maxItems {
425+ MaxWidth: maxWidth,
494− break
426+ MaxItems: maxItems,
495− }
427+ ShowSection: true,
496−
428+ SectionName: "LSPs",
497− iconColor := t.Success
429+ }, true)
499− iconColor = t.FgMuted
502− lspErrs := map[protocol.DiagnosticSeverity]int{
503− protocol.SeverityError: 0,
504− protocol.SeverityWarning: 0,
505− protocol.SeverityHint: 0,
506− protocol.SeverityInformation: 0,
508− if client, ok := m.lspClients[l.Name]; ok {
509− for _, diagnostics := range client.GetDiagnostics() {
510− for _, diagnostic := range diagnostics {
511− if severity, ok := lspErrs[diagnostic.Severity]; ok {
512− lspErrs[diagnostic.Severity] = severity + 1
519− if lspErrs[protocol.SeverityError] > 0 {
520− errs = append(errs, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, lspErrs[protocol.SeverityError])))
522− if lspErrs[protocol.SeverityWarning] > 0 {
523− errs = append(errs, t.S().Base.Foreground(t.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, lspErrs[protocol.SeverityWarning])))
525− if lspErrs[protocol.SeverityHint] > 0 {
526− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.HintIcon, lspErrs[protocol.SeverityHint])))
528− if lspErrs[protocol.SeverityInformation] > 0 {
529− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.InfoIcon, lspErrs[protocol.SeverityInformation])))
532− lspList = append(lspList,
535− IconColor: iconColor,
537− Description: l.LSP.Command,
538− ExtraContent: strings.Join(errs, " "),
545− // Add "..." indicator if there are more LSPs
546− if len(lsp) > maxItems {
547− lspList = append(lspList, t.S().Base.Foreground(t.FgMuted).Render("…"))
550− content := lipgloss.JoinVertical(lipgloss.Left, lspList...)
551− return lipgloss.NewStyle().Width(maxWidth).Render(content)
554// mcpBlockCompact renders the MCP block with limited width and height for horizontal layout
432// mcpBlockCompact renders the MCP block with limited width and height for horizontal layout
555func (m *sidebarCmp) mcpBlockCompact(maxWidth int) string {
433func (m *sidebarCmp) mcpBlockCompact(maxWidth int) string {
556− t := styles.CurrentTheme()
558− section := t.S().Subtle.Render("MCPs")
560− mcpList := []string{section, ""}
562− mcps := config.Get().MCP.Sorted()
564− content := lipgloss.JoinVertical(
568− t.S().Base.Foreground(t.Border).Render("None"),
570− return lipgloss.NewStyle().Width(maxWidth).Render(content)
573 // Limit items for horizontal layout
434 // Limit items for horizontal layout
574− maxItems := min(5, len(mcps))
435+ maxItems := min(5, len(config.Get().MCP.Sorted()))
575 availableHeight := m.height - 8
436 availableHeight := m.height - 8
576 if availableHeight > 0 {
437 if availableHeight > 0 {
577 maxItems = min(maxItems, availableHeight)
438 maxItems = min(maxItems, availableHeight)
580− for i, l := range mcps {
441+ return mcp.RenderMCPBlock(mcp.RenderOptions{
581− if i >= maxItems {
442+ MaxWidth: maxWidth,
582− break
443+ MaxItems: maxItems,
583− }
444+ ShowSection: true,
584−
445+ SectionName: "MCPs",
585− iconColor := t.Success
446+ }, true)
587− iconColor = t.FgMuted
590− mcpList = append(mcpList,
593− IconColor: iconColor,
595− Description: l.MCP.Command,
602− // Add "..." indicator if there are more MCPs
603− if len(mcps) > maxItems {
604− mcpList = append(mcpList, t.S().Base.Foreground(t.FgMuted).Render("…"))
607− content := lipgloss.JoinVertical(lipgloss.Left, mcpList...)
608− return lipgloss.NewStyle().Width(maxWidth).Render(content)
611func (m *sidebarCmp) filesBlock() string {
449func (m *sidebarCmp) filesBlock() string {
612− t := styles.CurrentTheme()
450+ // Convert map to slice and handle type conversion
614− section := t.S().Subtle.Render(
615− core.Section("Modified Files", m.getMaxWidth()),
618− files := slices.Collect(m.files.Seq())
451+ sessionFiles := slices.Collect(m.files.Seq())
619− if len(files) == 0 {
452+ fileSlice := make([]files.SessionFile, len(sessionFiles))
620− return lipgloss.JoinVertical(
453+ for i, sf := range sessionFiles {
621− lipgloss.Left,
454+ fileSlice[i] = files.SessionFile{
622− section,
455+ History: files.FileHistory{
456+ InitialVersion: sf.History.initialVersion,
457+ LatestVersion: sf.History.latestVersion,
624− t.S().Base.Foreground(t.Border).Render("None"),
459+ FilePath: sf.FilePath,
625− )
460+ Additions: sf.Additions,
461+ Deletions: sf.Deletions,
628− fileList := []string{section, ""}
629− // order files by the latest version's created time
630− sort.Slice(files, func(i, j int) bool {
631− return files[i].History.latestVersion.CreatedAt > files[j].History.latestVersion.CreatedAt
634 // Limit the number of files shown
465 // Limit the number of files shown
635 maxFiles, _, _ := m.getDynamicLimits()
466 maxFiles, _, _ := m.getDynamicLimits()
636− maxFiles = min(len(files), maxFiles)
467+ maxFiles = min(len(fileSlice), maxFiles)
638−
469+ return files.RenderFileBlock(fileSlice, files.RenderOptions{
639− for _, file := range files {
470+ MaxWidth: m.getMaxWidth(),
640− if file.Additions == 0 && file.Deletions == 0 {
471+ MaxItems: maxFiles,
641− continue // skip files with no changes
472+ ShowSection: true,
642− }
473+ SectionName: core.Section("Modified Files", m.getMaxWidth()),
643− if filesShown >= maxFiles {
474+ }, true)
647− var statusParts []string
648− if file.Additions > 0 {
649− statusParts = append(statusParts, t.S().Base.Foreground(t.Success).Render(fmt.Sprintf("+%d", file.Additions)))
651− if file.Deletions > 0 {
652− statusParts = append(statusParts, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("-%d", file.Deletions)))
655− extraContent := strings.Join(statusParts, " ")
656− cwd := config.Get().WorkingDir() + string(os.PathSeparator)
657− filePath := file.FilePath
658− filePath = strings.TrimPrefix(filePath, cwd)
659− filePath = fsext.DirTrim(fsext.PrettyPath(filePath), 2)
660− filePath = ansi.Truncate(filePath, m.getMaxWidth()-lipgloss.Width(extraContent)-2, "…")
661− fileList = append(fileList,
664− IconColor: t.FgMuted,
667− ExtraContent: extraContent,
675− // Add indicator if there are more files
676− totalFilesWithChanges := 0
677− for _, file := range files {
678− if file.Additions > 0 || file.Deletions > 0 {
679− totalFilesWithChanges++
682− if totalFilesWithChanges > maxFiles {
683− remaining := totalFilesWithChanges - maxFiles
684− fileList = append(fileList,
685− t.S().Base.Foreground(t.FgSubtle).Render(fmt.Sprintf("…and %d more", remaining)),
689− return lipgloss.JoinVertical(
695func (m *sidebarCmp) lspBlock() string {
477func (m *sidebarCmp) lspBlock() string {
696− t := styles.CurrentTheme()
698− section := t.S().Subtle.Render(
699− core.Section("LSPs", m.getMaxWidth()),
702− lspList := []string{section, ""}
704− lsp := config.Get().LSP.Sorted()
706− return lipgloss.JoinVertical(
710− t.S().Base.Foreground(t.Border).Render("None"),
714 // Limit the number of LSPs shown
478 // Limit the number of LSPs shown
715 _, maxLSPs, _ := m.getDynamicLimits()
479 _, maxLSPs, _ := m.getDynamicLimits()
480+ lspConfigs := config.Get().LSP.Sorted()
716− maxLSPs = min(len(lsp), maxLSPs)
481+ maxLSPs = min(len(lspConfigs), maxLSPs)
717− for i, l := range lsp {
482+
718− if i >= maxLSPs {
483+ return lspcomponent.RenderLSPBlock(m.lspClients, lspcomponent.RenderOptions{
719− break
484+ MaxWidth: m.getMaxWidth(),
720− }
485+ MaxItems: maxLSPs,
721−
486+ ShowSection: true,
722− iconColor := t.Success
487+ SectionName: core.Section("LSPs", m.getMaxWidth()),
723− if l.LSP.Disabled {
488+ }, true)
724− iconColor = t.FgMuted
726− lspErrs := map[protocol.DiagnosticSeverity]int{
727− protocol.SeverityError: 0,
728− protocol.SeverityWarning: 0,
729− protocol.SeverityHint: 0,
730− protocol.SeverityInformation: 0,
732− if client, ok := m.lspClients[l.Name]; ok {
733− for _, diagnostics := range client.GetDiagnostics() {
734− for _, diagnostic := range diagnostics {
735− if severity, ok := lspErrs[diagnostic.Severity]; ok {
736− lspErrs[diagnostic.Severity] = severity + 1
743− if lspErrs[protocol.SeverityError] > 0 {
744− errs = append(errs, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, lspErrs[protocol.SeverityError])))
746− if lspErrs[protocol.SeverityWarning] > 0 {
747− errs = append(errs, t.S().Base.Foreground(t.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, lspErrs[protocol.SeverityWarning])))
749− if lspErrs[protocol.SeverityHint] > 0 {
750− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.HintIcon, lspErrs[protocol.SeverityHint])))
752− if lspErrs[protocol.SeverityInformation] > 0 {
753− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.InfoIcon, lspErrs[protocol.SeverityInformation])))
756− lspList = append(lspList,
759− IconColor: iconColor,
761− Description: l.LSP.Command,
762− ExtraContent: strings.Join(errs, " "),
769− // Add indicator if there are more LSPs
770− if len(lsp) > maxLSPs {
771− remaining := len(lsp) - maxLSPs
772− lspList = append(lspList,
773− t.S().Base.Foreground(t.FgSubtle).Render(fmt.Sprintf("…and %d more", remaining)),
777− return lipgloss.JoinVertical(
783func (m *sidebarCmp) mcpBlock() string {
491func (m *sidebarCmp) mcpBlock() string {
784− t := styles.CurrentTheme()
786− section := t.S().Subtle.Render(
787− core.Section("MCPs", m.getMaxWidth()),
790− mcpList := []string{section, ""}
792− mcps := config.Get().MCP.Sorted()
794− return lipgloss.JoinVertical(
798− t.S().Base.Foreground(t.Border).Render("None"),
802 // Limit the number of MCPs shown
492 // Limit the number of MCPs shown
803 _, _, maxMCPs := m.getDynamicLimits()
493 _, _, maxMCPs := m.getDynamicLimits()
494+ mcps := config.Get().MCP.Sorted()
804 maxMCPs = min(len(mcps), maxMCPs)
495 maxMCPs = min(len(mcps), maxMCPs)
805− for i, l := range mcps {
810− iconColor := t.Success
812− iconColor = t.FgMuted
814− mcpList = append(mcpList,
817− IconColor: iconColor,
819− Description: l.MCP.Command,
826− // Add indicator if there are more MCPs
827− if len(mcps) > maxMCPs {
828− remaining := len(mcps) - maxMCPs
829− mcpList = append(mcpList,
830− t.S().Base.Foreground(t.FgSubtle).Render(fmt.Sprintf("…and %d more", remaining)),
834− return lipgloss.JoinVertical(
497+ return mcp.RenderMCPBlock(mcp.RenderOptions{
835− lipgloss.Left,
498+ MaxWidth: m.getMaxWidth(),
836− mcpList...,
499+ MaxItems: maxMCPs,
837− )
500+ ShowSection: true,
501+ SectionName: core.Section("MCPs", m.getMaxWidth()),
840func formatTokensAndCost(tokens, contextWindow int64, cost float64) string {
505func formatTokensAndCost(tokens, contextWindow int64, cost float64) string {