Skip to content

Commit 9f1ebbe

Browse files
committed
fix: use visual line estimate for adaptive classified and line indicator
1 parent e1c7073 commit 9f1ebbe

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

internal/tui/editor_panel.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,12 @@ func (p editorPanel) View(focused bool) string {
239239

240240
// Classified view — adaptive: shrink when text overflows editor.
241241
if len(p.sentences) > 0 {
242-
totalLines := p.textarea.LineCount()
242+
visualLines := p.estimateVisualLines()
243243
visibleLines := p.textarea.Height()
244244
maxLines := 6
245-
if totalLines > visibleLines*2 {
245+
if visualLines > visibleLines*2 {
246246
maxLines = 0 // hide entirely for very long text
247-
} else if totalLines > visibleLines {
247+
} else if visualLines > visibleLines {
248248
maxLines = 2 // collapse for moderately long text
249249
}
250250

@@ -324,16 +324,39 @@ func (p editorPanel) renderDashboard() string {
324324
)
325325

326326
// Show line indicator when text overflows the visible area.
327-
totalLines := p.textarea.LineCount()
327+
visualLines := p.estimateVisualLines()
328328
visibleLines := p.textarea.Height()
329-
if totalLines > visibleLines {
329+
if visualLines > visibleLines {
330330
cursorLine := p.textarea.Line() + 1 // 0-based to 1-based
331+
totalLines := p.textarea.LineCount()
331332
line += fmt.Sprintf(" | L%d/%d", cursorLine, totalLines)
332333
}
333334

334335
return styleMuted.Render(line)
335336
}
336337

338+
// estimateVisualLines returns how many visual lines the text occupies,
339+
// accounting for word wrapping. A single long sentence that wraps across
340+
// 3 visual lines returns 3, not 1.
341+
func (p editorPanel) estimateVisualLines() int {
342+
w := p.textarea.Width()
343+
if w <= 0 {
344+
w = 60
345+
}
346+
total := 0
347+
for _, line := range strings.Split(p.textarea.Value(), "\n") {
348+
if len(line) == 0 {
349+
total++
350+
} else {
351+
total += (len(line) + w - 1) / w
352+
}
353+
}
354+
if total == 0 {
355+
total = 1
356+
}
357+
return total
358+
}
359+
337360
func renderClassifiedLine(line string) string {
338361
if strings.Contains(line, "[LOCKED]") || strings.Contains(line, "[LOCKED:") {
339362
return styleLocked.Render(" " + line)

0 commit comments

Comments
 (0)