gitstack

grindlemire/go-tui code browser

2.7 KB Go 95 lines 2026-06-03 · d15bb9f raw
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
package tui

import (
	"fmt"
	"strings"
)

func (a *App) setupInitialScreen(width, termHeight int) {
	if a.inlineHeight > 0 {
		a.setupInlineScreen(width, termHeight)
		return
	}

	// Full screen mode: use alternate screen.
	a.terminal.EnterAltScreen()
	a.buffer = NewBuffer(width, termHeight)

	// First frame must do a full redraw so every cell is explicitly painted.
	// Without this, the diff-based renderer skips unchanged cells and the
	// alternate screen may show the terminal's default background on cells
	// the component didn't touch.
	a.needsFullRedraw = true
}

func (a *App) setupInlineScreen(width, termHeight int) {
	// Clamp inline height to terminal height.
	if a.inlineHeight > termHeight {
		a.inlineHeight = termHeight
	}
	if a.inlineHeight < 0 {
		a.inlineHeight = 0
	}

	// Reserve space for the inline widget at the bottom.
	a.reserveInlineRegion()

	// Calculate where our inline region starts.
	a.inlineStartRow = max(termHeight-a.inlineHeight, 0)
	a.inlineSession = newInlineSession(a.terminal)

	// Create buffer sized for inline region only.
	a.buffer = NewBuffer(width, a.inlineHeight)

	a.applyInlineStartupPolicy(termHeight)

	// First inline frame must repaint owned rows fully; startup state in the
	// terminal is not guaranteed to match the buffer's initial front state.
	a.needsFullRedraw = true
}

func (a *App) reserveInlineRegion() {
	if a.inlineHeight < 1 {
		return
	}

	// Reserve rows via linefeeds and return to the start of the reserved block.
	_, _ = a.terminal.WriteDirect([]byte(strings.Repeat("\r\n", a.inlineHeight)))
	_, _ = a.terminal.WriteDirect(fmt.Appendf(nil, "\033[%dA", a.inlineHeight))
}

func (a *App) applyInlineStartupPolicy(termHeight int) {
	historyCapacity := a.inlineStartRow

	switch a.inlineStartupMode {
	case InlineStartupFreshViewport:
		a.clearVisibleViewport()
		a.inlineLayout = newInlineLayoutState(historyCapacity)
	case InlineStartupSoftReset:
		a.softResetVisibleViewport(termHeight)
		a.inlineLayout = newInlineLayoutState(historyCapacity)
	case InlineStartupPreserveVisible:
		fallthrough
	default:
		// Unknown content may already be present in the history region.
		// Mark layout invalid so first append conservatively treats history as full.
		a.inlineLayout = newInlineLayoutState(historyCapacity)
		a.inlineLayout.invalidate(historyCapacity)
	}
}

func (a *App) clearVisibleViewport() {
	a.terminal.SetCursor(0, 0)
	a.terminal.ClearToEnd()
}

func (a *App) softResetVisibleViewport(termHeight int) {
	if termHeight < 1 {
		return
	}

	// Scroll existing visible rows into scrollback without issuing a global
	// scrollback erase command.
	a.terminal.SetCursor(0, termHeight-1)
	_, _ = a.terminal.WriteDirect([]byte(strings.Repeat("\r\n", termHeight)))
}