-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathui.go
More file actions
2468 lines (2215 loc) · 60.5 KB
/
Copy pathui.go
File metadata and controls
2468 lines (2215 loc) · 60.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
)
// ── States ──
type viewState int
const (
viewLoading viewState = iota
viewProjectList
viewProjectDetail
)
type pane int
const (
paneSidebar pane = iota
paneContent
)
// sidebarItem is a single navigable row in the project detail sidebar.
type sidebarItem struct {
kind string // "back", "file", "conversation", "subagent", "separator", "header"
label string
path string
badge string
}
type searchScope int
const (
searchScopeProject searchScope = iota
searchScopeGlobal
)
type sessionSearchState struct {
active bool
scope searchScope
input []rune
results []SearchResult
cursor int
offset int
gen int // debounce generation — only the latest tick fires search
}
// ── Model ──
type model struct {
state viewState
width, height int
// Providers
providers []Provider
providerTrees []*TreeData // one tree per provider
providerTab int // active tab index in project list
// Project list screen (current tab's tree)
tree *TreeData
projCursor int
projOffset int
// Project detail screen
activePane pane
projIndex int // index into tree.Projects
currentProj *TreeProject // pointer to selected project
currentProvider Provider // provider for current project
sidebar []sidebarItem
sidebarCursor int
sidebarOffset int
expandedConvPath string // which conversation's subagents are visible
// Content pane
contentLines []string
contentOffset int
contentTitle string
contentPath string
contentKind string
directFile string
err error
statusMsg string
// Export overlay
export exportState
// Mouse selection
mouseSelecting bool
mouseSelStart [2]int // [row, col] in screen coordinates
mouseSelEnd [2]int
mouseHasSelection bool
// Content search
contentSearchActive bool
contentSearchInput []rune
contentSearchPos int
contentMatches []int // line indices with matches
contentMatchIdx int // current match index
contentSearchQuery string // for highlighting
contentSearchGen int // debounce generation
// Session search overlay
sessionSearch sessionSearchState
}
// ── Export overlay types ──
type exportStep int
const (
exportStepWhat exportStep = iota
exportStepFormat
exportStepPath
exportStepFilename
exportStepConfirm
)
type exportWhat int
const (
exportFullConversation exportWhat = iota
exportMainThread
exportSelectedSubagent
)
type exportFormat int
const (
exportFormatHTML exportFormat = iota
exportFormatMarkdown
exportFormatJSONL
)
type exportState struct {
active bool
step exportStep
what exportWhat
whatCursor int
format exportFormat
formatCursor int
pathBuf []rune
pathCurPos int
filenameBuf []rune
filenameCurPos int
sourcePath string
sourceLabel string
convHasSubagents bool
}
// ── Messages ──
type contentLoadedMsg struct {
lines []string
title string
path string
kind string
err error
}
type statusClearMsg struct{}
type exportDoneMsg struct {
outPath string
err error
}
type editorFinishedMsg struct {
err error
}
type searchDebounceMsg struct {
gen int
kind string // "content" or "session"
}
// ── Styles ──
var (
titleStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FAFAFA")).
Background(lipgloss.Color("#D97706")).
Padding(0, 1)
projectNameStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#cfc8c4"))
projectMetaStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#78716C"))
projectBadgeStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#D97706"))
selectedStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FFFFFF")).
Background(lipgloss.Color("#D97706"))
paneTitleActive = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#D97706"))
paneTitleInactive = lipgloss.NewStyle().
Foreground(lipgloss.Color("#78716C"))
loadedStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#2D8B4E"))
dimStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#78716C"))
faintStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#A8A29E"))
sepStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#D6D3CD"))
backStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#6366F1"))
userHeaderStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FFFFFF")).
Background(lipgloss.Color("#5B5FC7")).
Padding(0, 1)
assistantHeaderStyle = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#FFFFFF")).
Background(lipgloss.Color("#2D8B4E")).
Padding(0, 1)
systemStyle = lipgloss.NewStyle().
Italic(true).
Foreground(lipgloss.Color("#A8A29E"))
thinkingStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#A8A29E")).
Italic(true)
toolStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#D97706"))
toolResultStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#A8A29E"))
statusStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#78716C"))
statusHighlight = lipgloss.NewStyle().
Foreground(lipgloss.Color("#D97706"))
)
// ── Init ──
func newModel(directFile string, providers []Provider) model {
return model{
state: viewLoading,
directFile: directFile,
providers: providers,
}
}
func (m model) Init() tea.Cmd {
if m.directFile != "" {
return loadConvCmd(m.directFile, m.directFile, 120, nil)
}
return m.loadAllTreesCmd()
}
// treeLoadedMsg is now per-provider.
type providerTreeLoadedMsg struct {
index int
tree *TreeData
err error
}
func (m model) loadAllTreesCmd() tea.Cmd {
cmds := make([]tea.Cmd, len(m.providers))
for i, prov := range m.providers {
i, prov := i, prov
cmds[i] = func() tea.Msg {
tree, err := prov.LoadTree()
return providerTreeLoadedMsg{i, tree, err}
}
}
return tea.Batch(cmds...)
}
func loadConvCmd(path, title string, width int, provider Provider) tea.Cmd {
return func() tea.Msg {
var entries []Entry
var err error
if provider != nil {
entries, err = provider.LoadConversation(path)
} else {
entries, err = parseConversation(path)
}
if err != nil {
return contentLoadedMsg{nil, title, path, "conversation", err}
}
lines := renderConversation(entries, width)
return contentLoadedMsg{lines, title, path, "conversation", nil}
}
}
func loadFileCmd(path, title string, width int) tea.Cmd {
return func() tea.Msg {
content, err := readFileContent(path)
if err != nil {
return contentLoadedMsg{nil, title, path, "file", err}
}
if strings.HasSuffix(path, ".md") {
rendered := renderMarkdownTerm(content, width)
return contentLoadedMsg{strings.Split(rendered, "\n"), title, path, "file", nil}
}
return contentLoadedMsg{strings.Split(content, "\n"), title, path, "file", nil}
}
}
// ── Sidebar builder ──
func buildSidebar(proj *TreeProject, plans []TreeFileRef, expandedConvPath string) []sidebarItem {
items := []sidebarItem{
{kind: "back", label: "< Back to Projects"},
{kind: "separator"},
}
if proj.ClaudeMD != "" {
items = append(items, sidebarItem{kind: "file", label: "CLAUDE.md", path: proj.ClaudeMD})
}
for _, mem := range proj.MemoryFiles {
items = append(items, sidebarItem{kind: "file", label: mem.Name, path: mem.Path})
}
if proj.ClaudeMD != "" || len(proj.MemoryFiles) > 0 {
items = append(items, sidebarItem{kind: "separator"})
}
// Plans (global)
if len(plans) > 0 {
items = append(items, sidebarItem{kind: "header", label: "PLANS"})
for _, plan := range plans {
items = append(items, sidebarItem{kind: "file", label: plan.Name, path: plan.Path})
}
items = append(items, sidebarItem{kind: "separator"})
}
for _, conv := range proj.Conversations {
title := conv.Title
if title == "" {
title = conv.Slug
}
if title == "" && len(conv.SessionID) >= 8 {
title = conv.SessionID[:8]
}
badge := fmt.Sprintf("%s %d msgs", formatDateSmart(conv.ModTime), conv.MsgCount)
if len(conv.SubAgents) > 0 {
badge = fmt.Sprintf("%s %d msgs · %d agents", formatDateSmart(conv.ModTime), conv.MsgCount, len(conv.SubAgents))
}
items = append(items, sidebarItem{
kind: "conversation",
label: title,
path: conv.Path,
badge: badge,
})
// Only show subagents for the expanded conversation
if conv.Path == expandedConvPath {
for _, sa := range conv.SubAgents {
desc := sa.Description
if desc == "" {
desc = sa.Name
}
at := sa.AgentType
if at == "" {
at = "agent"
}
lbl := at + ": " + desc
if len(lbl) > 55 {
lbl = lbl[:52] + "..."
}
items = append(items, sidebarItem{kind: "subagent", label: lbl, path: sa.Path})
}
}
}
return items
}
// navigable returns true if the sidebar item can be selected with cursor.
func (si sidebarItem) navigable() bool {
return si.kind != "separator" && si.kind != "header"
}
// nextNavigable returns the next navigable index from pos in direction dir (+1/-1).
func nextNavigable(items []sidebarItem, pos, dir int) int {
for i := pos + dir; i >= 0 && i < len(items); i += dir {
if items[i].navigable() {
return i
}
}
return pos
}
// ── Update ──
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
oldW := m.width
m.width = msg.Width
m.height = msg.Height
if m.state == viewProjectDetail && m.contentPath != "" && m.contentKind == "conversation" && oldW != msg.Width {
_, rw := m.paneWidths()
return m, loadConvCmd(m.contentPath, m.contentTitle, rw, m.currentProvider)
}
return m, nil
case providerTreeLoadedMsg:
// Initialize providerTrees slice if needed
if m.providerTrees == nil {
m.providerTrees = make([]*TreeData, len(m.providers))
}
if msg.index >= 0 && msg.index < len(m.providerTrees) {
if msg.err == nil && msg.tree != nil {
m.providerTrees[msg.index] = msg.tree
} else {
// Mark errored providers with empty tree so we know they've responded
m.providerTrees[msg.index] = &TreeData{}
}
}
// Count how many have responded
loaded := 0
for _, t := range m.providerTrees {
if t != nil {
loaded++
}
}
if loaded < len(m.providers) {
return m, nil // still waiting
}
if m.directFile != "" {
return m, nil
}
// All loaded — set active tab to first provider with data
for i, t := range m.providerTrees {
if t != nil && len(t.Projects) > 0 {
m.providerTab = i
m.tree = t
break
}
}
if m.tree == nil {
// Use first non-nil tree even if empty
for i, t := range m.providerTrees {
if t != nil {
m.providerTab = i
m.tree = t
break
}
}
}
m.state = viewProjectList
return m, nil
case contentLoadedMsg:
if msg.err != nil {
m.statusMsg = fmt.Sprintf("Error: %v", msg.err)
return m, tea.Tick(3*time.Second, func(time.Time) tea.Msg { return statusClearMsg{} })
}
m.contentLines = msg.lines
m.contentTitle = msg.title
m.contentPath = msg.path
m.contentKind = msg.kind
m.contentOffset = 0
if m.directFile != "" {
m.state = viewProjectDetail
}
return m, nil
case searchDebounceMsg:
if msg.kind == "content" && msg.gen == m.contentSearchGen {
m.contentSearchQuery = string(m.contentSearchInput)
m.computeContentMatches()
if len(m.contentMatches) > 0 {
m.contentMatchIdx = 0
m.scrollToMatch()
}
}
if msg.kind == "session" && msg.gen == m.sessionSearch.gen {
m.computeSessionSearchResults()
}
return m, nil
case statusClearMsg:
m.statusMsg = ""
return m, nil
case exportDoneMsg:
if msg.err != nil {
m.statusMsg = fmt.Sprintf("Export error: %v", msg.err)
} else {
m.statusMsg = fmt.Sprintf("Exported to %s", msg.outPath)
}
return m, tea.Tick(3*time.Second, func(time.Time) tea.Msg { return statusClearMsg{} })
case editorFinishedMsg:
if msg.err != nil {
m.statusMsg = fmt.Sprintf("Editor error: %v", msg.err)
return m, tea.Tick(3*time.Second, func(time.Time) tea.Msg { return statusClearMsg{} })
}
return m, nil
case searchNavigateMsg:
// Switch to the correct provider tab
if msg.providerIdx >= 0 && msg.providerIdx < len(m.providerTrees) {
m.switchProviderTab(msg.providerIdx)
}
// Open the project
m.openProject(msg.projIdx)
// Load the conversation
_, rw := m.paneWidths()
return m, loadConvCmd(msg.convPath, msg.convTitle, rw, m.currentProvider)
case tea.MouseClickMsg:
return m.handleMouseClick(msg)
case tea.MouseMotionMsg:
return m.handleMouseMotion(msg)
case tea.MouseReleaseMsg:
return m.handleMouseRelease(msg)
case tea.MouseWheelMsg:
return m.handleMouseWheel(msg)
case tea.KeyPressMsg:
if msg.String() == "ctrl+c" {
return m, tea.Quit
}
// Clear selection on any keypress
if m.mouseHasSelection {
m.mouseHasSelection = false
m.mouseSelecting = false
}
// Overlays intercept all keys when active
if m.sessionSearch.active {
return m.updateSessionSearch(msg)
}
if m.export.active {
return m.updateExportOverlay(msg)
}
switch m.state {
case viewProjectList:
return m.updateProjectList(msg)
case viewProjectDetail:
if m.directFile != "" {
return m.updateContent(msg)
}
if m.activePane == paneContent {
return m.updateContent(msg)
}
return m.updateSidebar(msg)
}
}
return m, nil
}
func (m *model) switchProviderTab(idx int) {
if idx < 0 || idx >= len(m.providerTrees) || m.providerTrees[idx] == nil {
return
}
m.providerTab = idx
m.tree = m.providerTrees[idx]
m.projCursor = 0
m.projOffset = 0
}
func (m model) hasMultipleTabs() bool {
count := 0
for _, t := range m.providerTrees {
if t != nil && len(t.Projects) > 0 {
count++
}
}
return count > 1
}
func (m model) updateProjectList(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "q":
return m, tea.Quit
case "1":
if m.hasMultipleTabs() {
m.switchProviderTab(0)
}
case "2":
if len(m.providerTrees) > 1 && m.hasMultipleTabs() {
m.switchProviderTab(1)
}
case "tab":
if m.hasMultipleTabs() {
next := (m.providerTab + 1) % len(m.providerTrees)
// Skip nil trees
for m.providerTrees[next] == nil || len(m.providerTrees[next].Projects) == 0 {
next = (next + 1) % len(m.providerTrees)
if next == m.providerTab {
break
}
}
m.switchProviderTab(next)
}
}
if m.tree == nil || len(m.tree.Projects) == 0 {
return m, nil
}
switch msg.String() {
case "up", "k":
if m.projCursor > 0 {
m.projCursor--
}
case "down", "j":
if m.projCursor < len(m.tree.Projects)-1 {
m.projCursor++
}
case "home", "g":
m.projCursor = 0
case "end", "G":
m.projCursor = len(m.tree.Projects) - 1
case "enter", "l", "right":
m.openProject(m.projCursor)
return m, nil
case "/":
m.openSessionSearch(searchScopeGlobal)
return m, nil
}
// Keep cursor visible — account for tab bar height
tabBarH := 0
if m.hasMultipleTabs() {
tabBarH = 2
}
viewH := m.height - 4 - tabBarH
itemH := 3 // lines per project item
maxVisible := viewH / itemH
if maxVisible < 1 {
maxVisible = 1
}
if m.projCursor < m.projOffset {
m.projOffset = m.projCursor
}
if m.projCursor >= m.projOffset+maxVisible {
m.projOffset = m.projCursor - maxVisible + 1
}
return m, nil
}
func (m *model) openProject(idx int) {
if idx < 0 || idx >= len(m.tree.Projects) {
return
}
m.projIndex = idx
m.currentProj = &m.tree.Projects[idx]
// Set the provider for this project based on its source
m.currentProvider = m.providers[m.providerTab]
m.expandedConvPath = ""
m.sidebar = buildSidebar(m.currentProj, m.tree.Plans, m.expandedConvPath)
m.sidebarCursor = 0
// Move cursor to first navigable item
if len(m.sidebar) > 0 && !m.sidebar[0].navigable() {
m.sidebarCursor = nextNavigable(m.sidebar, -1, 1)
}
m.sidebarOffset = 0
m.activePane = paneSidebar
m.contentLines = nil
m.contentTitle = ""
m.contentPath = ""
m.state = viewProjectDetail
}
func (m model) updateSidebar(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
_, rightW := m.paneWidths()
switch msg.String() {
case "q":
return m, tea.Quit
case "esc", "backspace", "h", "left":
m.state = viewProjectList
m.currentProj = nil
m.sidebar = nil
m.contentLines = nil
m.contentTitle = ""
m.contentPath = ""
m.expandedConvPath = ""
return m, nil
case "up", "k":
m.sidebarCursor = nextNavigable(m.sidebar, m.sidebarCursor, -1)
case "down", "j":
m.sidebarCursor = nextNavigable(m.sidebar, m.sidebarCursor, 1)
case "home", "g":
m.sidebarCursor = nextNavigable(m.sidebar, -1, 1)
case "end", "G":
m.sidebarCursor = nextNavigable(m.sidebar, len(m.sidebar), -1)
case "tab", "l", "right":
if len(m.contentLines) > 0 {
m.activePane = paneContent
}
case "enter":
if m.sidebarCursor < len(m.sidebar) {
item := m.sidebar[m.sidebarCursor]
switch item.kind {
case "back":
m.state = viewProjectList
m.currentProj = nil
m.sidebar = nil
m.contentLines = nil
m.expandedConvPath = ""
return m, nil
case "conversation":
m.expandedConvPath = item.path
m.sidebar = buildSidebar(m.currentProj, m.tree.Plans, m.expandedConvPath)
// Find cursor for the expanded conversation
for i, si := range m.sidebar {
if si.path == item.path && si.kind == "conversation" {
m.sidebarCursor = i
break
}
}
m.contentTitle = item.label
m.contentLines = nil
return m, loadConvCmd(item.path, item.label, rightW, m.currentProvider)
case "subagent":
m.contentTitle = item.label
m.contentLines = nil
return m, loadConvCmd(item.path, item.label, rightW, m.currentProvider)
case "file":
m.contentTitle = item.label
m.contentLines = nil
return m, loadFileCmd(item.path, item.label, rightW)
}
}
case "e":
if m.sidebarCursor < len(m.sidebar) {
item := m.sidebar[m.sidebarCursor]
if item.kind == "conversation" || item.kind == "subagent" {
m.initExportOverlay(item.path, item.label)
return m, nil
}
}
case "o":
if m.sidebarCursor < len(m.sidebar) {
item := m.sidebar[m.sidebarCursor]
if item.path != "" {
return m, openInEditor(item.path)
}
}
case "/":
m.openSessionSearch(searchScopeProject)
return m, nil
}
// Keep cursor visible
sidebarH := m.height - 5
if sidebarH < 1 {
sidebarH = 1
}
if m.sidebarCursor < m.sidebarOffset {
m.sidebarOffset = m.sidebarCursor
}
if m.sidebarCursor >= m.sidebarOffset+sidebarH {
m.sidebarOffset = m.sidebarCursor - sidebarH + 1
}
return m, nil
}
func (m model) updateContent(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
// Content search mode intercepts keys
if m.contentSearchActive {
return m.updateContentSearch(msg)
}
_, paneH := m.contentPaneDims()
contentH := paneH - 2
maxOff := len(m.contentLines) - contentH
if maxOff < 0 {
maxOff = 0
}
switch msg.String() {
case "q":
return m, tea.Quit
case "up", "k":
if m.contentOffset > 0 {
m.contentOffset--
}
case "down", "j":
if m.contentOffset < maxOff {
m.contentOffset++
}
case "pgup", "b":
m.contentOffset -= contentH
if m.contentOffset < 0 {
m.contentOffset = 0
}
case "pgdown", "f", "space":
m.contentOffset += contentH
if m.contentOffset > maxOff {
m.contentOffset = maxOff
}
case "home", "g":
m.contentOffset = 0
case "end", "G":
m.contentOffset = maxOff
case "esc":
if m.contentSearchQuery != "" {
// Clear search highlights
m.contentSearchQuery = ""
m.contentMatches = nil
return m, nil
}
if m.directFile != "" {
return m, tea.Quit
}
m.activePane = paneSidebar
case "h", "left", "tab":
if m.directFile != "" {
return m, tea.Quit
}
m.activePane = paneSidebar
case "/":
if len(m.contentLines) > 0 {
m.contentSearchActive = true
m.contentSearchInput = nil
m.contentSearchPos = 0
return m, nil
}
case "n":
// Next match
if len(m.contentMatches) > 0 {
m.contentMatchIdx = (m.contentMatchIdx + 1) % len(m.contentMatches)
m.scrollToMatch()
}
case "N":
// Previous match
if len(m.contentMatches) > 0 {
m.contentMatchIdx--
if m.contentMatchIdx < 0 {
m.contentMatchIdx = len(m.contentMatches) - 1
}
m.scrollToMatch()
}
case "e":
if m.contentPath != "" && m.contentKind == "conversation" {
m.initExportOverlay(m.contentPath, m.contentTitle)
return m, nil
}
case "o":
if m.contentPath != "" {
return m, openInEditor(m.contentPath)
}
}
return m, nil
}
func (m model) contentSearchDebounceCmd() tea.Cmd {
gen := m.contentSearchGen
return tea.Tick(250*time.Millisecond, func(time.Time) tea.Msg {
return searchDebounceMsg{gen: gen, kind: "content"}
})
}
func (m model) updateContentSearch(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
key := msg.String()
switch key {
case "esc":
m.contentSearchActive = false
return m, nil
case "enter":
m.contentSearchActive = false
// Immediate compute on enter (bypass debounce)
m.contentSearchQuery = string(m.contentSearchInput)
m.computeContentMatches()
if len(m.contentMatches) > 0 {
m.contentMatchIdx = 0
m.scrollToMatch()
}
return m, nil
case "backspace":
if len(m.contentSearchInput) > 0 {
m.contentSearchInput = m.contentSearchInput[:len(m.contentSearchInput)-1]
m.contentSearchGen++
return m, m.contentSearchDebounceCmd()
}
return m, nil
default:
r := []rune(key)
if len(r) == 1 && r[0] >= 32 {
m.contentSearchInput = append(m.contentSearchInput, r[0])
m.contentSearchGen++
return m, m.contentSearchDebounceCmd()
}
return m, nil
}
}
func (m *model) computeContentMatches() {
m.contentMatches = nil
if m.contentSearchQuery == "" {
return
}
q := strings.ToLower(m.contentSearchQuery)
for i, line := range m.contentLines {
plain := strings.ToLower(ansi.Strip(line))
if strings.Contains(plain, q) {
m.contentMatches = append(m.contentMatches, i)
}
}
}
func (m *model) scrollToMatch() {
if m.contentMatchIdx < 0 || m.contentMatchIdx >= len(m.contentMatches) {
return
}
targetLine := m.contentMatches[m.contentMatchIdx]
_, paneH := m.contentPaneDims()
contentH := paneH - 2
// Center the match in the viewport
m.contentOffset = targetLine - contentH/2
maxOff := len(m.contentLines) - contentH
if maxOff < 0 {
maxOff = 0
}
if m.contentOffset < 0 {
m.contentOffset = 0
}
if m.contentOffset > maxOff {
m.contentOffset = maxOff
}
}
// ── Session search ──
func (m *model) openSessionSearch(scope searchScope) {
m.sessionSearch = sessionSearchState{
active: true,
scope: scope,
}
}
func (m model) sessionSearchDebounceCmd() tea.Cmd {
gen := m.sessionSearch.gen
return tea.Tick(300*time.Millisecond, func(time.Time) tea.Msg {
return searchDebounceMsg{gen: gen, kind: "session"}
})
}
func (m model) updateSessionSearch(msg tea.KeyPressMsg) (tea.Model, tea.Cmd) {
key := msg.String()
switch key {
case "esc":
m.sessionSearch.active = false
return m, nil
case "enter":
if len(m.sessionSearch.results) > 0 && m.sessionSearch.cursor < len(m.sessionSearch.results) {
result := m.sessionSearch.results[m.sessionSearch.cursor]
m.sessionSearch.active = false
return m, m.navigateToSearchResult(result)
}
return m, nil
case "tab":
// Toggle scope — immediate recompute since it's just re-filtering
if m.sessionSearch.scope == searchScopeProject {
m.sessionSearch.scope = searchScopeGlobal
} else {
m.sessionSearch.scope = searchScopeProject
}
m.sessionSearch.gen++
m.computeSessionSearchResults()
return m, nil
case "up", "k":
if m.sessionSearch.cursor > 0 {
m.sessionSearch.cursor--
}
case "down", "j":
if m.sessionSearch.cursor < len(m.sessionSearch.results)-1 {
m.sessionSearch.cursor++
}