@@ -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,465 +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"),
398− return lipgloss.NewStyle().Width(maxWidth).Render(content)
400−
393+ FilePath: sf.FilePath,
401− fileList := []string{section, ""}
394+ Additions: sf.Additions,
402− sort.Slice(files, func(i, j int) bool {
395+ Deletions: sf.Deletions,
403− if files[i].History.latestVersion.CreatedAt == files[j].History.latestVersion.CreatedAt {
404− return files[i].FilePath < files[j].FilePath
406− return files[i].History.latestVersion.CreatedAt > files[j].History.latestVersion.CreatedAt
409− // Limit items for horizontal layout - use less space
399+ // Limit items for horizontal layout
410− maxItems := min(5, len(files))
400+ maxItems := min(5, len(fileSlice))
411 availableHeight := m.height - 8 // Reserve space for header and other content
401 availableHeight := m.height - 8 // Reserve space for header and other content
412 if availableHeight > 0 {
402 if availableHeight > 0 {
413 maxItems = min(maxItems, availableHeight)
403 maxItems = min(maxItems, availableHeight)
416− filesShown := 0
406+ return files.RenderFileBlock(fileSlice, files.RenderOptions{
417− for _, file := range files {
407+ MaxWidth: maxWidth,
418− if file.Additions == 0 && file.Deletions == 0 {
408+ MaxItems: maxItems,
419− continue
409+ ShowSection: true,
420− }
410+ SectionName: "Modified Files",
421− if filesShown >= maxItems {
411+ }, true)
425− var statusParts []string
426− if file.Additions > 0 {
427− statusParts = append(statusParts, t.S().Base.Foreground(t.Success).Render(fmt.Sprintf("+%d", file.Additions)))
429− if file.Deletions > 0 {
430− statusParts = append(statusParts, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("-%d", file.Deletions)))
433− extraContent := strings.Join(statusParts, " ")
434− cwd := config.Get().WorkingDir() + string(os.PathSeparator)
435− filePath := file.FilePath
436− filePath = strings.TrimPrefix(filePath, cwd)
437− filePath = fsext.DirTrim(fsext.PrettyPath(filePath), 2)
438− filePath = ansi.Truncate(filePath, maxWidth-lipgloss.Width(extraContent)-2, "…")
440− fileList = append(fileList,
443− IconColor: t.FgMuted,
446− ExtraContent: extraContent,
454− // Add "..." indicator if there are more files
455− totalFilesWithChanges := 0
456− for _, file := range files {
457− if file.Additions > 0 || file.Deletions > 0 {
458− totalFilesWithChanges++
461− if totalFilesWithChanges > maxItems {
462− fileList = append(fileList, t.S().Base.Foreground(t.FgMuted).Render("…"))
465− content := lipgloss.JoinVertical(lipgloss.Left, fileList...)
466− return lipgloss.NewStyle().Width(maxWidth).Render(content)
469// 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
470func (m *sidebarCmp) lspBlockCompact(maxWidth int) string {
415func (m *sidebarCmp) lspBlockCompact(maxWidth int) string {
471− t := styles.CurrentTheme()
473− section := t.S().Subtle.Render("LSPs")
475− lspList := []string{section, ""}
477− lsp := config.Get().LSP.Sorted()
479− content := lipgloss.JoinVertical(
483− t.S().Base.Foreground(t.Border).Render("None"),
485− return lipgloss.NewStyle().Width(maxWidth).Render(content)
488 // Limit items for horizontal layout
416 // Limit items for horizontal layout
417+ lspConfigs := config.Get().LSP.Sorted()
489− maxItems := min(5, len(lsp))
418+ maxItems := min(5, len(lspConfigs))
490 availableHeight := m.height - 8
419 availableHeight := m.height - 8
491 if availableHeight > 0 {
420 if availableHeight > 0 {
492 maxItems = min(maxItems, availableHeight)
421 maxItems = min(maxItems, availableHeight)
495− for i, l := range lsp {
424+ return lspcomponent.RenderLSPBlock(m.lspClients, lspcomponent.RenderOptions{
496− if i >= maxItems {
425+ MaxWidth: maxWidth,
497− break
426+ MaxItems: maxItems,
498− }
427+ ShowSection: true,
499−
428+ SectionName: "LSPs",
500− iconColor := t.Success
429+ }, true)
502− iconColor = t.FgMuted
505− lspErrs := map[protocol.DiagnosticSeverity]int{
506− protocol.SeverityError: 0,
507− protocol.SeverityWarning: 0,
508− protocol.SeverityHint: 0,
509− protocol.SeverityInformation: 0,
511− if client, ok := m.lspClients[l.Name]; ok {
512− for _, diagnostics := range client.GetDiagnostics() {
513− for _, diagnostic := range diagnostics {
514− if severity, ok := lspErrs[diagnostic.Severity]; ok {
515− lspErrs[diagnostic.Severity] = severity + 1
522− if lspErrs[protocol.SeverityError] > 0 {
523− errs = append(errs, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, lspErrs[protocol.SeverityError])))
525− if lspErrs[protocol.SeverityWarning] > 0 {
526− errs = append(errs, t.S().Base.Foreground(t.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, lspErrs[protocol.SeverityWarning])))
528− if lspErrs[protocol.SeverityHint] > 0 {
529− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.HintIcon, lspErrs[protocol.SeverityHint])))
531− if lspErrs[protocol.SeverityInformation] > 0 {
532− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.InfoIcon, lspErrs[protocol.SeverityInformation])))
535− lspList = append(lspList,
538− IconColor: iconColor,
540− Description: l.LSP.Command,
541− ExtraContent: strings.Join(errs, " "),
548− // Add "..." indicator if there are more LSPs
549− if len(lsp) > maxItems {
550− lspList = append(lspList, t.S().Base.Foreground(t.FgMuted).Render("…"))
553− content := lipgloss.JoinVertical(lipgloss.Left, lspList...)
554− return lipgloss.NewStyle().Width(maxWidth).Render(content)
557// 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
558func (m *sidebarCmp) mcpBlockCompact(maxWidth int) string {
433func (m *sidebarCmp) mcpBlockCompact(maxWidth int) string {
559− t := styles.CurrentTheme()
561− section := t.S().Subtle.Render("MCPs")
563− mcpList := []string{section, ""}
565− mcps := config.Get().MCP.Sorted()
567− content := lipgloss.JoinVertical(
571− t.S().Base.Foreground(t.Border).Render("None"),
573− return lipgloss.NewStyle().Width(maxWidth).Render(content)
576 // Limit items for horizontal layout
434 // Limit items for horizontal layout
577− maxItems := min(5, len(mcps))
435+ maxItems := min(5, len(config.Get().MCP.Sorted()))
578 availableHeight := m.height - 8
436 availableHeight := m.height - 8
579 if availableHeight > 0 {
437 if availableHeight > 0 {
580 maxItems = min(maxItems, availableHeight)
438 maxItems = min(maxItems, availableHeight)
583− for i, l := range mcps {
441+ return mcp.RenderMCPBlock(mcp.RenderOptions{
584− if i >= maxItems {
442+ MaxWidth: maxWidth,
585− break
443+ MaxItems: maxItems,
586− }
444+ ShowSection: true,
587−
445+ SectionName: "MCPs",
588− iconColor := t.Success
446+ }, true)
590− iconColor = t.FgMuted
593− mcpList = append(mcpList,
596− IconColor: iconColor,
598− Description: l.MCP.Command,
605− // Add "..." indicator if there are more MCPs
606− if len(mcps) > maxItems {
607− mcpList = append(mcpList, t.S().Base.Foreground(t.FgMuted).Render("…"))
610− content := lipgloss.JoinVertical(lipgloss.Left, mcpList...)
611− return lipgloss.NewStyle().Width(maxWidth).Render(content)
614func (m *sidebarCmp) filesBlock() string {
449func (m *sidebarCmp) filesBlock() string {
615− t := styles.CurrentTheme()
450+ // Convert map to slice and handle type conversion
617− section := t.S().Subtle.Render(
618− core.Section("Modified Files", m.getMaxWidth()),
621− files := slices.Collect(m.files.Seq())
451+ sessionFiles := slices.Collect(m.files.Seq())
622− if len(files) == 0 {
452+ fileSlice := make([]files.SessionFile, len(sessionFiles))
623− return lipgloss.JoinVertical(
453+ for i, sf := range sessionFiles {
624− lipgloss.Left,
454+ fileSlice[i] = files.SessionFile{
625− section,
455+ History: files.FileHistory{
626− "",
456+ InitialVersion: sf.History.initialVersion,
627− t.S().Base.Foreground(t.Border).Render("None"),
457+ LatestVersion: sf.History.latestVersion,
629− }
459+ FilePath: sf.FilePath,
630−
460+ Additions: sf.Additions,
631− fileList := []string{section, ""}
461+ Deletions: sf.Deletions,
632− // order files by the latest version's created time, then by path for stability
633− sort.Slice(files, func(i, j int) bool {
634− if files[i].History.latestVersion.CreatedAt == files[j].History.latestVersion.CreatedAt {
635− return files[i].FilePath < files[j].FilePath
637− return files[i].History.latestVersion.CreatedAt > files[j].History.latestVersion.CreatedAt
640 // Limit the number of files shown
465 // Limit the number of files shown
641 maxFiles, _, _ := m.getDynamicLimits()
466 maxFiles, _, _ := m.getDynamicLimits()
642− maxFiles = min(len(files), maxFiles)
467+ maxFiles = min(len(fileSlice), maxFiles)
644−
469+ return files.RenderFileBlock(fileSlice, files.RenderOptions{
645− for _, file := range files {
470+ MaxWidth: m.getMaxWidth(),
646− if file.Additions == 0 && file.Deletions == 0 {
471+ MaxItems: maxFiles,
647− continue // skip files with no changes
472+ ShowSection: true,
648− }
473+ SectionName: core.Section("Modified Files", m.getMaxWidth()),
649− if filesShown >= maxFiles {
474+ }, true)
653− var statusParts []string
654− if file.Additions > 0 {
655− statusParts = append(statusParts, t.S().Base.Foreground(t.Success).Render(fmt.Sprintf("+%d", file.Additions)))
657− if file.Deletions > 0 {
658− statusParts = append(statusParts, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("-%d", file.Deletions)))
661− extraContent := strings.Join(statusParts, " ")
662− cwd := config.Get().WorkingDir() + string(os.PathSeparator)
663− filePath := file.FilePath
664− filePath = strings.TrimPrefix(filePath, cwd)
665− filePath = fsext.DirTrim(fsext.PrettyPath(filePath), 2)
666− filePath = ansi.Truncate(filePath, m.getMaxWidth()-lipgloss.Width(extraContent)-2, "…")
667− fileList = append(fileList,
670− IconColor: t.FgMuted,
673− ExtraContent: extraContent,
681− // Add indicator if there are more files
682− totalFilesWithChanges := 0
683− for _, file := range files {
684− if file.Additions > 0 || file.Deletions > 0 {
685− totalFilesWithChanges++
688− if totalFilesWithChanges > maxFiles {
689− remaining := totalFilesWithChanges - maxFiles
690− fileList = append(fileList,
691− t.S().Base.Foreground(t.FgSubtle).Render(fmt.Sprintf("…and %d more", remaining)),
695− return lipgloss.JoinVertical(
701func (m *sidebarCmp) lspBlock() string {
477func (m *sidebarCmp) lspBlock() string {
702− t := styles.CurrentTheme()
704− section := t.S().Subtle.Render(
705− core.Section("LSPs", m.getMaxWidth()),
708− lspList := []string{section, ""}
710− lsp := config.Get().LSP.Sorted()
712− return lipgloss.JoinVertical(
716− t.S().Base.Foreground(t.Border).Render("None"),
720 // Limit the number of LSPs shown
478 // Limit the number of LSPs shown
721 _, maxLSPs, _ := m.getDynamicLimits()
479 _, maxLSPs, _ := m.getDynamicLimits()
480+ lspConfigs := config.Get().LSP.Sorted()
722− maxLSPs = min(len(lsp), maxLSPs)
481+ maxLSPs = min(len(lspConfigs), maxLSPs)
723− for i, l := range lsp {
482+
724− if i >= maxLSPs {
483+ return lspcomponent.RenderLSPBlock(m.lspClients, lspcomponent.RenderOptions{
725− break
484+ MaxWidth: m.getMaxWidth(),
726− }
485+ MaxItems: maxLSPs,
727−
486+ ShowSection: true,
728− iconColor := t.Success
487+ SectionName: core.Section("LSPs", m.getMaxWidth()),
729− if l.LSP.Disabled {
488+ }, true)
730− iconColor = t.FgMuted
732− lspErrs := map[protocol.DiagnosticSeverity]int{
733− protocol.SeverityError: 0,
734− protocol.SeverityWarning: 0,
735− protocol.SeverityHint: 0,
736− protocol.SeverityInformation: 0,
738− if client, ok := m.lspClients[l.Name]; ok {
739− for _, diagnostics := range client.GetDiagnostics() {
740− for _, diagnostic := range diagnostics {
741− if severity, ok := lspErrs[diagnostic.Severity]; ok {
742− lspErrs[diagnostic.Severity] = severity + 1
749− if lspErrs[protocol.SeverityError] > 0 {
750− errs = append(errs, t.S().Base.Foreground(t.Error).Render(fmt.Sprintf("%s %d", styles.ErrorIcon, lspErrs[protocol.SeverityError])))
752− if lspErrs[protocol.SeverityWarning] > 0 {
753− errs = append(errs, t.S().Base.Foreground(t.Warning).Render(fmt.Sprintf("%s %d", styles.WarningIcon, lspErrs[protocol.SeverityWarning])))
755− if lspErrs[protocol.SeverityHint] > 0 {
756− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.HintIcon, lspErrs[protocol.SeverityHint])))
758− if lspErrs[protocol.SeverityInformation] > 0 {
759− errs = append(errs, t.S().Base.Foreground(t.FgHalfMuted).Render(fmt.Sprintf("%s %d", styles.InfoIcon, lspErrs[protocol.SeverityInformation])))
762− lspList = append(lspList,
765− IconColor: iconColor,
767− Description: l.LSP.Command,
768− ExtraContent: strings.Join(errs, " "),
775− // Add indicator if there are more LSPs
776− if len(lsp) > maxLSPs {
777− remaining := len(lsp) - maxLSPs
778− lspList = append(lspList,
779− t.S().Base.Foreground(t.FgSubtle).Render(fmt.Sprintf("…and %d more", remaining)),
783− return lipgloss.JoinVertical(
789func (m *sidebarCmp) mcpBlock() string {
491func (m *sidebarCmp) mcpBlock() string {
790− t := styles.CurrentTheme()
792− section := t.S().Subtle.Render(
793− core.Section("MCPs", m.getMaxWidth()),
796− mcpList := []string{section, ""}
798− mcps := config.Get().MCP.Sorted()
800− return lipgloss.JoinVertical(
804− t.S().Base.Foreground(t.Border).Render("None"),
808 // Limit the number of MCPs shown
492 // Limit the number of MCPs shown
809 _, _, maxMCPs := m.getDynamicLimits()
493 _, _, maxMCPs := m.getDynamicLimits()
494+ mcps := config.Get().MCP.Sorted()
810 maxMCPs = min(len(mcps), maxMCPs)
495 maxMCPs = min(len(mcps), maxMCPs)
811− for i, l := range mcps {
816− iconColor := t.Success
818− iconColor = t.FgMuted
820− mcpList = append(mcpList,
823− IconColor: iconColor,
825− Description: l.MCP.Command,
832− // Add indicator if there are more MCPs
833− if len(mcps) > maxMCPs {
834− remaining := len(mcps) - maxMCPs
835− mcpList = append(mcpList,
836− t.S().Base.Foreground(t.FgSubtle).Render(fmt.Sprintf("…and %d more", remaining)),
840− return lipgloss.JoinVertical(
497+ return mcp.RenderMCPBlock(mcp.RenderOptions{
841− lipgloss.Left,
498+ MaxWidth: m.getMaxWidth(),
842− mcpList...,
499+ MaxItems: maxMCPs,
843− )
500+ ShowSection: true,
501+ SectionName: core.Section("MCPs", m.getMaxWidth()),
846func formatTokensAndCost(tokens, contextWindow int64, cost float64) string {
505func formatTokensAndCost(tokens, contextWindow int64, cost float64) string {