-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtui_view.go
More file actions
1050 lines (959 loc) · 30.3 KB
/
Copy pathtui_view.go
File metadata and controls
1050 lines (959 loc) · 30.3 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 (
"encoding/json"
"fmt"
"math"
"sort"
"strings"
"github.com/charmbracelet/lipgloss"
"github.com/foliagecp/easyjson"
)
// ── Body rendering ────────────────────────────────────────────────────────────
func (m tuiModel) bodyContent() string {
if m.fvi == nil {
if m.errMsg != "" {
return styleErr.Render("⚠ " + m.errMsg)
}
return styleDim.Render("(loading…)")
}
var content string
if m.rawBody {
if m.fvi.body == nil || !m.fvi.body.IsNonEmptyObject() {
content = styleDim.Render(" (empty body)")
} else {
content = JSONStrPrettyStringAnyway(m.fvi.body, 0, 2)
}
} else {
content = renderBodyKV(m.fvi.body, m.vpWidth())
}
return highlightMatches(content, m.searchQuery)
}
// renderBodyKV renders a JSON object as aligned key: value lines.
func renderBodyKV(body *easyjson.JSON, w int) string {
if body == nil || !body.IsNonEmptyObject() {
return styleDim.Render(" (empty)")
}
obj, ok := body.Value.(map[string]interface{})
if !ok {
return JSONStrPrettyStringAnyway(body, 0, 2)
}
keys := make([]string, 0, len(obj))
for k := range obj {
keys = append(keys, k)
}
sort.Strings(keys)
maxKeyLen := 0
for _, k := range keys {
if kw := lipgloss.Width(k); kw > maxKeyLen {
maxKeyLen = kw
}
}
if maxKeyLen > 20 {
maxKeyLen = 20
}
valW := w - maxKeyLen - 4
if valW < 6 {
valW = 6
}
var sb strings.Builder
for i, k := range keys {
if i > 0 {
sb.WriteByte('\n')
}
keyStr := truncateCells(k, maxKeyLen) + ":"
pad := maxKeyLen + 1 - lipgloss.Width(keyStr)
if pad < 0 {
pad = 0
}
sb.WriteString(styleMetaKey.Render(" " + keyStr + strings.Repeat(" ", pad)))
sb.WriteString(" ")
sb.WriteString(renderJSONValue(obj[k], valW))
}
return sb.String()
}
func renderJSONValue(v interface{}, maxW int) string {
if maxW < 4 {
maxW = 4
}
switch val := v.(type) {
case string:
return styleMetaVal.Render(truncateCells(val, maxW))
case float64:
if val == math.Trunc(val) && math.Abs(val) < 1e18 {
return styleMetaVal.Render(fmt.Sprintf("%d", int64(val)))
}
return styleMetaVal.Render(fmt.Sprintf("%g", val))
case bool:
if val {
return lipgloss.NewStyle().Foreground(lipgloss.Color("42")).Render("true")
}
return lipgloss.NewStyle().Foreground(lipgloss.Color("196")).Render("false")
case nil:
return styleDim.Render("null")
case []interface{}:
if len(val) == 0 {
return styleDim.Render("[]")
}
strs := make([]string, len(val))
allStr := true
for i, item := range val {
if s, ok := item.(string); ok {
strs[i] = s
} else {
allStr = false
break
}
}
if allStr {
joined := "[" + strings.Join(strs, ", ") + "]"
if lipgloss.Width(joined) <= maxW {
return styleMetaVal.Render(joined)
}
}
b, _ := json.Marshal(val)
return styleDim.Render(truncateCells(string(b), maxW))
default:
b, _ := json.Marshal(v)
return styleDim.Render(truncateCells(string(b), maxW))
}
}
// ── Three-column panels ───────────────────────────────────────────────────────
// renderSidePanel renders either the incoming (isOut=false) or outgoing (isOut=true) side panel.
func (m tuiModel) renderSidePanel(isOut bool) string {
cw := m.sideContentW()
ch := m.panelContentH()
active := (isOut && m.focus == panelOut) || (!isOut && m.focus == panelIn)
var groups []linkGroup
var flat []flatItem
var cursor, offset int
var dirIcon, dirLabel string
var headerStyle lipgloss.Style
if isOut {
groups = m.grouped.outGroups
flat = m.grouped.outFlat
cursor = m.rCursor
offset = m.rOffset
dirIcon, dirLabel = "→", "OUTGOING"
headerStyle = styleOut.Bold(true)
} else {
groups = m.grouped.inGroups
flat = m.grouped.inFlat
cursor = m.lCursor
offset = m.lOffset
dirIcon, dirLabel = "←", "INCOMING"
headerStyle = styleIn.Bold(true)
}
total := 0
for _, g := range groups {
total += len(g.links)
}
title := headerStyle.Render(dirIcon+" "+dirLabel) + " " + styleDim.Render(fmt.Sprintf("(%d)", total))
listH := ch - 1
if listH < 1 {
listH = 1
}
var content string
if m.loading {
content = styleLoading.Render("loading…")
} else if len(groups) == 0 {
if m.searchQuery != "" {
content = styleDim.Render("no matches")
} else {
content = styleDim.Render("(none)")
}
} else {
content = m.renderPanelLinks(groups, flat, cursor, offset, active, isOut, cw, listH)
}
inner := lipgloss.JoinVertical(lipgloss.Left, title, content)
ps := stylePanel.Width(cw).Height(ch)
if active {
ps = stylePanelActive.Width(cw).Height(ch)
}
return ps.Render(inner)
}
// renderPanelLinks renders the link list for one side panel.
func (m tuiModel) renderPanelLinks(groups []linkGroup, flat []flatItem, cursor, offset int, active, isOut bool, w, h int) string {
lines := make([]string, 0, h)
for i := offset; i < len(flat) && len(lines) < h; i++ {
item := flat[i]
if item.groupIdx >= len(groups) {
continue
}
g := groups[item.groupIdx]
selected := active && i == cursor
switch item.kind {
case flatTypeGroup:
collapseIcon := "▾"
if g.collapsed {
collapseIcon = "▸"
}
countStr := fmt.Sprintf("%d", len(g.links))
// prefix visual width: " " + icon + " " = 4; right-align count at edge
tpMax := w - 5 - len(countStr) // 4 prefix + 1 min-fill + count
if tpMax < 1 {
tpMax = 1
}
tp := truncateCells(g.tp, tpMax)
tpW := lipgloss.Width(tp)
fillW := w - 4 - tpW - len(countStr)
if fillW < 1 {
fillW = 1
}
fill := strings.Repeat(" ", fillW)
if selected {
plain := " " + collapseIcon + " " + tp + fill + countStr
lines = append(lines, styleSelected.Width(w).Render(plain))
} else {
lines = append(lines,
" "+styleDim.Render(collapseIcon)+" "+styleTypeHdr.Render(tp)+
fill+styleDim.Render(countStr),
)
}
case flatLink:
if item.linkIdx >= len(g.links) {
continue
}
dl := g.links[item.linkIdx]
// " name → tgt" — visual overhead = " "4 + " "1 + "→"1 + " "1 = 7
avail := w - 7
if avail < 2 {
avail = 2
}
nameMax := avail * 6 / 10 // ~60% for name
if nameMax < 1 {
nameMax = 1
}
targetMax := avail - nameMax
if targetMax < 1 {
targetMax = 1
}
name := dl.label()
tgt := stripDomain(dl.target())
rawName := truncateCells(name, nameMax)
// pad to nameMax so the arrow aligns at a fixed column across all rows
namePad := strings.Repeat(" ", nameMax-lipgloss.Width(rawName))
if selected {
// No marker of its own: the highlight already says "selected",
// and the ► that used to sit here is a near-twin of the ▸ a
// collapsed GROUP shows — so a selected link looked like
// something Tab would expand.
dispName := highlightMatches(rawName, m.searchQuery) + namePad
dispTarget := highlightMatches(truncateCells(tgt, targetMax), m.searchQuery)
plain := " " + dispName + " → " + dispTarget
lines = append(lines, styleSelected.Width(w).Render(plain))
} else {
var arrowStr string
if isOut {
arrowStr = styleOut.Render("→")
} else {
arrowStr = styleIn.Render("←")
}
dispName := highlightMatches(rawName, m.searchQuery) + namePad
dispTarget := highlightMatches(truncateCells(tgt, targetMax), m.searchQuery)
lines = append(lines, " "+dispName+" "+arrowStr+" "+dispTarget)
}
}
}
return strings.Join(lines, "\n")
}
// ── Center panel ──────────────────────────────────────────────────────────────
func (m tuiModel) renderCenterPanel() string {
cw := m.centerContentW()
ch := m.panelContentH()
rawMark := ""
if m.rawBody {
rawMark = styleDim.Render(" [raw]")
}
llMark := ""
if m.llMode {
llMark = styleWarn.Render(" [LL]")
}
// The title names the SUBJECT, so the panel and the header never disagree
// about what the next keystroke will act on.
subj := m.subject()
title := styleTitle.Render("◈ "+truncateCells(m.currentID, cw/2)) + m.vertexKindBadge() + rawMark + llMark
if subj.kind == subjLink {
title = styleTitle.Render("⎯ "+truncateCells(subj.link.info.id.name, cw/2)) +
" " + styleMetaKey.Render("["+m.tierOfSubjectLink(subj.link).noun()+"]")
}
// A form takes the WHOLE centre column — no divider, no type map. It needs
// the room, and the type map describes a vertex the user has stopped
// looking at.
if m.form != nil && m.form.chrome == chromeCenter {
// The form owns the column and writes its own header; a second title
// naming the subject would just repeat what the form already says.
return stylePanelActive.Width(cw).Height(ch).Render(m.renderFormCenter(cw, ch))
}
var body string
switch {
case len(m.queryResults) > 0:
body = m.renderQueryResults(cw, m.vpHeight())
case subj.kind == subjLink:
body = m.renderLinkSubject(subj.link, cw, m.vpHeight())
default:
body = m.bodyVP.View()
}
divider := styleDim.Render(strings.Repeat("─", cw))
typeMap := m.renderTypeMap(cw, m.typeMapH())
inner := lipgloss.JoinVertical(lipgloss.Left, title, body, divider, typeMap)
ps := stylePanel.Width(cw).Height(ch)
if m.focus == panelCenter {
ps = stylePanelActive.Width(cw).Height(ch)
}
return ps.Render(inner)
}
// renderTypeMap renders a 4-column type-flow map showing two hops in each direction:
//
// [depth-2 in] [depth-1 in] ← │ → [depth-1 out] [depth-2 out]
//
// Depth-2 columns appear only when the async fetch has completed.
func (m tuiModel) renderTypeMap(w, h int) string {
if h <= 0 || w < 6 {
return ""
}
in1 := m.grouped.inGroups
out1 := m.grouped.outGroups
in2 := m.inTypes2
out2 := m.outTypes2
rows := len(in1)
for _, n := range []int{len(out1), len(in2), len(out2)} {
if n > rows {
rows = n
}
}
if rows == 0 {
if m.loading {
return ""
}
return styleDim.Render("(no links)")
}
// Total layout: [lhW] │ [rhW] where lhW + 1 + rhW = w.
lhW := w / 2
rhW := w - lhW - 1
// When depth-2 data is present, split each half ~40 / 60.
hasD2 := len(in2) > 0 || len(out2) > 0
var d2inW, d1inW, d1outW, d2outW int
if hasD2 {
d2inW = lhW * 4 / 10
if d2inW < 3 {
d2inW = 3
}
d1inW = lhW - d2inW
d2outW = rhW * 4 / 10
if d2outW < 3 {
d2outW = 3
}
d1outW = rhW - d2outW
} else {
d1inW = lhW
d1outW = rhW
}
// cellPad right-pads or left-pads a styled string to a fixed visual width.
padL := func(s string, w int) string {
p := w - lipgloss.Width(s)
if p <= 0 {
return s
}
return strings.Repeat(" ", p) + s
}
padR := func(s string, w int) string {
p := w - lipgloss.Width(s)
if p <= 0 {
return s
}
return s + strings.Repeat(" ", p)
}
lines := make([]string, 0, h)
for i := 0; i < rows && len(lines) < h; i++ {
// ── depth-1 incoming (right-aligned, coloured ←) ──────────────────────
var d1in string
if i < len(in1) {
tpW := d1inW - 2
if tpW < 1 {
tpW = 1
}
tp := truncateCells(in1[i].tp, tpW)
d1in = padL(styleTypeHdr.Render(tp)+" "+styleIn.Render("←"), d1inW)
} else {
d1in = strings.Repeat(" ", d1inW)
}
// ── depth-2 incoming (right-aligned, dim) ─────────────────────────────
var d2in string
if hasD2 {
if i < len(in2) {
tpW := d2inW - 2
if tpW < 1 {
tpW = 1
}
tp := truncateCells(in2[i], tpW)
d2in = padL(styleDim.Render(tp)+" "+styleDim.Render("←"), d2inW)
} else {
d2in = strings.Repeat(" ", d2inW)
}
}
// ── depth-1 outgoing (left-aligned, coloured →) ───────────────────────
var d1out string
if i < len(out1) {
tpW := d1outW - 2
if tpW < 1 {
tpW = 1
}
tp := truncateCells(out1[i].tp, tpW)
d1out = padR(styleOut.Render("→")+" "+styleTypeHdr.Render(tp), d1outW)
} else {
d1out = strings.Repeat(" ", d1outW)
}
// ── depth-2 outgoing (left-aligned, dim) ──────────────────────────────
var d2out string
if hasD2 && i < len(out2) {
tpW := d2outW - 2
if tpW < 1 {
tpW = 1
}
tp := truncateCells(out2[i], tpW)
d2out = styleDim.Render("→") + " " + styleDim.Render(tp)
}
lines = append(lines, d2in+d1in+styleDim.Render("│")+d1out+d2out)
}
return strings.Join(lines, "\n")
}
// ── Query results (shown in center panel body area) ───────────────────────────
func (m tuiModel) renderQueryResults(w, h int) string {
lines := make([]string, 0, h)
for i := m.qOffset; i < len(m.queryResults) && len(lines) < h; i++ {
id := m.queryResults[i]
if i == m.qCursor {
lines = append(lines, styleSelected.Width(w).Render("▶ "+truncateCells(id, w-2)))
} else {
lines = append(lines, " "+truncateCells(id, w-2))
}
}
return strings.Join(lines, "\n")
}
// ── Body panel badge ──────────────────────────────────────────────────────────
// vertexKindBadge names what the user is standing on.
//
// EVERY kind gets a badge, including the plain one. The classification decides
// which entries the create menu offers and which API a body edit uses, so it
// has to be on screen — an unbadged vertex used to mean "plain", "half-written
// object" and "the classifier has no opinion" all at once, which left the menu
// looking arbitrary. A structural vertex says so, and that alone is the reason
// `hub/root [built-in]` can no longer be mistaken for something to hang
// objects off.
func (m tuiModel) vertexKindBadge() string {
k, typeName := m.vertexKind()
style := styleMetaKey
switch k {
case vkPlain, vkStructural:
style = styleDim
case vkBrokenObject:
style = styleErr
}
return " " + style.Render("["+k.label(typeName)+"]")
}
// ── Narrow layout (single-column fallback) ────────────────────────────────────
// viewNarrow is the single-column fallback below narrowThreshold columns.
//
// It used to render only the link lists. That meant the vertex body was
// unreachable, the [LL] marker invisible, and — worst — every chromeCenter
// form was a fully modal dialog that drew NOTHING while swallowing every
// keystroke. The user saw an unchanged screen with the ordinary nav hints
// under it, and their typing went into an input box that was not on screen.
//
// The rule now: whatever the mode is, this renders it. A mode with no visible
// surface is not a narrow-layout compromise, it is a hang.
func (m tuiModel) viewNarrow() string {
w := m.width
if w < 1 {
w = 1
}
listH := m.height - 1 - m.breadcrumbH() - 2
if listH < 1 {
listH = 1
}
divider := styleDim.Render(strings.Repeat("─", w))
// Stacked in one column, the subject goes on top and the lists below it —
// the same information the three columns carry, in reading order.
var main string
subj := m.subject()
switch {
case m.form != nil && m.form.chrome == chromeCenter:
// The modal owns the column. Nothing below it is actionable anyway.
main = m.renderFormCenter(w, listH)
case len(m.queryResults) > 0:
main = m.renderQueryResults(w, listH)
case m.loading:
main = styleLoading.Render("loading…")
default:
top := listH / 2
if top < 3 {
top = 3
}
var detail string
if subj.kind == subjLink {
detail = m.renderLinkSubject(subj.link, w, top)
} else {
detail = clipLines(m.bodyContent(), top)
}
main = detail + "\n" + divider + "\n" + m.narrowLinkLists(w, listH-top-1)
}
rows := []string{m.renderHeader(), m.narrowSubtitle(w), divider, main}
if bc := m.renderBreadcrumbs(); bc != "" {
rows = append(rows, bc)
}
rows = append(rows, m.renderStatus())
return lipgloss.JoinVertical(lipgloss.Left, rows...)
}
// narrowSubtitle carries what the wide layout puts in the centre panel title:
// the kind badge and the mode markers. Without it there is no indication at
// all that the low-level API is armed.
func (m tuiModel) narrowSubtitle(w int) string {
line := m.vertexKindBadge()
if m.rawBody {
line += styleDim.Render(" [raw]")
}
if m.llMode {
line += styleWarn.Render(" [LL]")
}
if m.mode() != modeBrowse {
line += " " + styleMetaKey.Render("· "+m.mode().String())
} else {
// The wide layout says this with a border. Stacked in one column there
// are no borders, so which of the three has focus has to be written.
switch m.focus {
case panelIn:
line += " " + styleIn.Render("· incoming")
case panelOut:
line += " " + styleOut.Render("· outgoing")
default:
line += " " + styleDim.Render("· the vertex")
}
}
return truncateCells(line, w)
}
func (m tuiModel) narrowLinkLists(w, h int) string {
if len(m.grouped.outGroups)+len(m.grouped.inGroups) == 0 {
if m.searchQuery != "" {
// The wide layout says this; the narrow one used to show a blank
// region, because its guard tested m.links while its rendering
// read m.grouped.
return styleDim.Render("no matches")
}
return styleDim.Render("(no links)")
}
half := h / 2
if half < 1 {
half = 1
}
out := m.renderPanelLinks(m.grouped.outGroups, m.grouped.outFlat,
m.rCursor, m.rOffset, m.focus == panelOut, true, w, half)
in := m.renderPanelLinks(m.grouped.inGroups, m.grouped.inFlat,
m.lCursor, m.lOffset, m.focus == panelIn, false, w, h-half)
if out == "" {
return in
}
if in == "" {
return out
}
return out + "\n" + in
}
// ── View ──────────────────────────────────────────────────────────────────────
func (m tuiModel) View() string {
if !m.ready || m.width == 0 {
return "Starting…"
}
if m.helpOpen {
return m.renderHelp()
}
if m.isNarrow() {
return m.viewNarrow()
}
panels := lipgloss.JoinHorizontal(lipgloss.Top,
m.renderSidePanel(false), // left: incoming
m.renderCenterPanel(),
m.renderSidePanel(true), // right: outgoing
)
rows := []string{m.renderHeader(), panels}
if bc := m.renderBreadcrumbs(); bc != "" {
rows = append(rows, bc)
}
rows = append(rows, m.renderStatus())
return lipgloss.JoinVertical(lipgloss.Left, rows...)
}
// ── Search highlight ──────────────────────────────────────────────────────────
func highlightMatches(text, query string) string {
if query == "" {
return text
}
lq := strings.ToLower(query)
lt := strings.ToLower(text)
var b strings.Builder
i := 0
for i < len(lt) {
idx := strings.Index(lt[i:], lq)
if idx < 0 {
b.WriteString(text[i:])
break
}
b.WriteString(text[i : i+idx])
b.WriteString(styleSearch.Render(text[i+idx : i+idx+len(lq)]))
i += idx + len(lq)
}
return b.String()
}
// ── Header, breadcrumbs, status ───────────────────────────────────────────────
func (m tuiModel) breadcrumbs() string {
if len(m.history) == 0 || m.width <= 0 {
return ""
}
const sep = " ← "
labels := make([]string, len(m.history))
for i, id := range m.history {
labels[len(m.history)-1-i] = stripDomain(id)
}
sepW := lipgloss.Width(sep)
used := sepW
count := 0
for _, lbl := range labels {
need := lipgloss.Width(lbl)
if count > 0 {
need += sepW
}
if used+need > m.width {
break
}
used += need
count++
}
if count == 0 {
return ""
}
return styleDim.Render(sep + strings.Join(labels[:count], sep))
}
func (m tuiModel) renderHeader() string {
load := ""
if m.loading {
if m.linksTotal > 0 {
load = " " + styleLoading.Render(fmt.Sprintf("loading %d links…", m.linksTotal))
} else {
load = " " + styleLoading.Render("loading…")
}
}
// Cap the vertex ID so the header never wraps to a second line.
idMax := m.width - 2 - lipgloss.Width(load) // 2 = "◈ " prefix
if idMax < 4 {
idMax = 4
}
header := styleHeader.Render("◈ "+truncateCells(m.currentID, idMax)) + load
if m.errMsg != "" {
errMax := m.width - lipgloss.Width(header) - 4 // 4 = " ⚠ "
if errMax > 4 {
header += " " + styleErr.Render("⚠ "+truncateCells(m.errMsg, errMax))
}
}
return lipgloss.NewStyle().Width(m.width).Render(header)
}
func (m tuiModel) renderBreadcrumbs() string {
bc := m.breadcrumbs()
if m.linking == nil {
if bc == "" {
return ""
}
return lipgloss.NewStyle().Width(m.width).Render(bc)
}
// A pending link is the single most important thing on screen while it
// lasts: it changes what the next keypress means. It gets the whole row,
// states both endpoints, and names its two exits — the history crumbs are
// decoration by comparison and give way first.
from := stripDomain(m.linking.fromID)
to := stripDomain(m.currentID)
target := styleDim.Render("(walk to the target)")
if m.currentID != m.linking.fromID {
target = styleMetaVal.Render(to)
}
banner := styleWarn.Render(" ◆ LINK PENDING ") + " " +
styleMetaVal.Render(from) + styleOut.Render(" ──▶ ") + target
// Only offer the exits while they ARE the exits. Once the form is open the
// commit is being filled in, and L is an ordinary letter going into a text
// field — telling the user to press it there is telling them to type it.
if m.form == nil {
banner += styleHintSep.Render(" ") + hint("L", "commit") +
styleHintSep.Render(" ") + hint("Esc", "cancel")
} else {
banner += styleHintSep.Render(" filling it in — ") + hint("ctrl+s", "create")
}
return lipgloss.NewStyle().Width(m.width).Render(truncateCells(banner, m.width))
}
func hint(key, desc string) string {
return styleHintKey.Render(key) + styleHintSep.Render(":"+desc)
}
// renderStatus draws the bottom line.
//
// It is a switch over the mode plus a NOTICE — a toast or an error — that
// rides alongside the hints rather than replacing them. It used to replace
// them: a single mutation toast took the whole line and the keymap vanished
// until something happened to clear it, which for several keys was never. And
// because the error case sat below both the toast and the active-filter case,
// an error raised while either was showing was reachable from nowhere on
// screen — it existed in the model and was displayed by nothing.
func (m tuiModel) renderStatus() string {
sep := styleHintSep.Render(" ")
// lead is what stays on the line even when a notice needs the whole of it.
lead := ""
if m.mode() == modeBrowse {
lead = m.crudModeChip()
}
var s string
switch {
case m.form != nil:
// A form's own keys. The status bar used to fall through to the browse
// hints here, so a form advertised `jk:nav Enter:go d:del` while
// every one of those keys was going into a text field.
//
// The form's TITLE is not repeated: the panel is showing it, two rows
// up, in larger type.
s = renderHintLine(formHints(m.form))
case m.gotoMode:
s = "Go to: " + m.gotoInput.View() +
styleHintSep.Render(" ") + renderHintLine(modeHints(modeGoto))
case m.queryMode:
s = "Query: " + m.queryInput.View() +
styleHintSep.Render(" ") + renderHintLine(modeHints(modeQuery))
case m.exportMode && !m.exportDepStep:
parts := make([]string, len(exportFmts))
for i, f := range exportFmts {
if i == m.exportFmtIdx {
parts[i] = styleWarn.Render("[" + f.label + "]")
} else {
parts[i] = styleDim.Render(f.label)
}
}
s = styleWarn.Render("Export") +
" format: " + strings.Join(parts, " ") +
styleHintSep.Render(" ←→ Tab:change Enter:→depth Esc:cancel")
case m.exportMode && m.exportDepStep:
depthParts := make([]string, len(exportDepthPresets))
for i, p := range exportDepthPresets {
lbl := p
if lbl == "" {
lbl = "all"
}
if i == m.exportDepthIdx {
depthParts[i] = styleWarn.Render("[" + lbl + "]")
} else {
depthParts[i] = styleDim.Render(lbl)
}
}
s = styleWarn.Render("Export") +
" " + exportFmts[m.exportFmtIdx].label +
" depth: " + m.exportInput.View() +
" " + strings.Join(depthParts, " ") +
styleHintSep.Render(" Tab:preset Enter:export Esc:back")
case m.searchMode:
s = "Search: " + m.searchInput.View() +
styleHintSep.Render(" ") + renderHintLine(modeHints(modeSearch))
case len(m.queryResults) > 0:
s = strings.Join([]string{
hint("jk", "navigate"),
hint("Enter", "go"),
hint("Esc/b", "close"),
hint("q", "quit"),
}, sep)
default:
s = m.browseHintLine(lead, sep, noticeWidth(m))
}
// The notice rides alongside the hints. An error outranks a toast: it is
// the one the user needs and the one that used to be reachable from
// nowhere on screen.
notice := m.queryResult
if m.errMsg != "" {
notice = styleErr.Render("⚠ " + m.errMsg)
}
if notice != "" {
const readable = 24 // below this a message is a stub, not information
room := m.width - lipgloss.Width(s) - 5
if room < readable {
// Not enough line for both. The notice wins: the hints are always
// one `?` away, and a message clipped to six cells tells nobody
// anything. The mode chip stays — it is one word and it is the
// context the message is read in.
s = lead
room = m.width - lipgloss.Width(s) - 5
}
if room > 0 {
s += styleHintSep.Render(" ↳ ") + truncateCells(notice, room)
}
}
return styleStatus.Width(m.width).Render(truncateCells(s, m.width-2))
}
// truncateCells clips a string to max terminal cells, ANSI-aware.
//
// The loop used to walk raw runes and measure each one, so every byte of an
// escape sequence — the ESC, the '[', the digits, the 'm' — counted as a
// visible cell. A styled string therefore lost ten to fifteen columns per span
// it walked past, and the cut could land inside a sequence, leaving an
// unterminated SGR that bleeds colour across the rest of the screen. It only
// bit once content actually overflowed, which is to say only in narrow
// terminals, which is where every caller that passes pre-styled text lives:
// the pending-link banner, the form rows, and every line of the help screen.
func truncateCells(s string, max int) string {
if max <= 0 {
return ""
}
if lipgloss.Width(s) <= max {
return s
}
if max == 1 {
return "…"
}
var b strings.Builder
used := 0
inEscape := false
for _, r := range s {
// Escape sequences occupy no cells and must be copied whole: dropping
// the terminator is what leaks styling into everything after it.
if inEscape {
b.WriteRune(r)
if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') {
inEscape = false
}
continue
}
if r == '\x1b' {
b.WriteRune(r)
inEscape = true
continue
}
rw := lipgloss.Width(string(r))
if used+rw > max-1 {
break
}
b.WriteRune(r)
used += rw
}
// Close any style still open at the cut, so the ellipsis and everything
// drawn after it are unstyled.
if strings.Contains(s, "\x1b[") {
b.WriteString("\x1b[0m")
}
b.WriteString("…")
return b.String()
}
// hintPair splits a "key:action" hint and styles the two halves.
//
// Split on the LAST colon, not the first: one of the keys IS a colon, and
// cutting at the first one rendered it as a hint with no key at all.
func hintPair(h string) string {
i := strings.LastIndex(h, ":")
if i < 0 {
return styleHintKey.Render(h)
}
return hint(h[:i], h[i+1:])
}
// renderHintLine styles a "key:action key:action" string.
func renderHintLine(s string) string {
if s == "" {
return ""
}
parts := strings.Fields(s)
out := make([]string, len(parts))
for i, p := range parts {
out[i] = hintPair(p)
}
return strings.Join(out, styleHintSep.Render(" "))
}
// crudModeChip names which API the next create, edit or delete will use.
//
// Navigation is always low-level — it walks raw edges — so this deliberately
// says CRUD rather than claiming to be a mode for the whole interface.
func (m tuiModel) crudModeChip() string {
if m.llMode {
return styleWarn.Render(" CRUD: low-level ")
}
return styleDim.Render(" CRUD: high-level ")
}
// noticeWidth is how much of the line a pending toast or error should be
// allowed to claim.
func noticeWidth(m tuiModel) int {
if m.errMsg == "" && m.queryResult == "" {
return 0
}
w := m.width / 3
if w > 44 {
w = 44
}
return w
}
// browseHintLine packs as many hints as the terminal has room for.
//
// There are more bindings than fit on one line at 80 columns, so something has
// to give. Dropping the ones that fit least well, in a fixed order the user
// can learn, beats the alternatives: truncating mid-hint produces a keymap
// nobody trusts, and wrapping adds a row no height calculation accounts for
// and pushes the top of the frame out of the screen.
//
// The CRUD chip and `?:help` are never dropped — one is the mode every write
// depends on, the other is where everything that did get dropped still lives.
func (m tuiModel) browseHintLine(lead, sep string, reserved int) string {
sepW := lipgloss.Width(sep)
help := hintPair("?:help")
budget := m.width - 2 - reserved
used := lipgloss.Width(lead) + sepW + lipgloss.Width(help)
parts := []string{lead}
if m.searchQuery != "" {
chip := styleSearch.Render(" /" + m.searchQuery + " ")
used += sepW + lipgloss.Width(chip)
parts = append(parts, chip)
}
for _, h := range browseHintsFor(m.linkPanelFocused()) {
if h == "L:link" {
h = "L:" + m.linkKeyAction()
}
rendered := hintPair(h)