返回 DeepSeek-Reasonix
panel_content.go
根目录 / internal / cli / panel_content.go
1 package cli
2
3 import (
4 "strings"
5
6 "charm.land/lipgloss/v2"
7 )
8
9 // panelContent reports a terminal row and a logical selection independently of
10 // the display text. The selection still changes when a windowed list moves but
11 // keeps its selected item on the same terminal row.
12 type panelContent struct {
13 text string
14 focus, selection int
15 }
16
17 // panelBuilder records the start of the selected row while its owner writes
18 // the body. MarkFocus must be called at a row boundary, before writing the row.
19 type panelBuilder struct {
20 strings.Builder
21 focusOffset, selection int
22 hasFocus bool
23 }
24
25 func (b *panelBuilder) MarkFocus(selected bool, selection int) {
26 if selected && !b.hasFocus {
27 b.focusOffset, b.selection, b.hasFocus = b.Len(), selection, true
28 }
29 }
30
31 type panelBody struct {
32 text string
33 focusOffset, selection int
34 hasFocus bool
35 }
36
37 func (b panelBody) trimTrailingNewlines() panelBody {
38 b.text = strings.TrimRight(b.text, "\n")
39 return b
40 }
41
42 func (b *panelBuilder) body() panelBody {
43 return panelBody{b.String(), b.focusOffset, b.selection, b.hasFocus}
44 }
45
46 func (b *panelBuilder) render(style lipgloss.Style, width int) panelContent {
47 return b.body().render(style, width)
48 }
49
50 func (b panelBody) render(style lipgloss.Style, width int) panelContent {
51 p := panelContent{text: style.Render(b.text), selection: b.selection}
52 if b.hasFocus {
53 // Measure preceding rows with the same style and terminal wrapping as
54 // the actual panel. A single cell preserves the selected row's empty
55 // start; no marker is inserted into or searched for in the output.
56 prefix := style.Render(b.text[:b.focusOffset] + "x")
57 // Exclude the placeholder row and bottom frame before terminal
58 // wrapping: a manager's minimum width can exceed the terminal width.
59 rows := strings.Split(prefix, "\n")
60 before := len(rows) - 1 - style.GetPaddingBottom() - style.GetBorderBottomSize() - style.GetMarginBottom()
61 if before > 0 {
62 p.focus = strings.Count(wrapTerminalText(strings.Join(rows[:before], "\n"), max(width, 10)), "\n") + 1
63 }
64 }
65 return p
66 }
67
67 lines GO