okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

dawnadd charm tui skillf7fcae1

main
1.3 KiB42 linesraw
1// Package display holds small rendering helpers, not an application framework.
2package display
3
4import (
5	"strings"
6	"unicode"
7)
8
9// BodyRows receives already-measured chrome heights, not guessed constants.
10func BodyRows(total, header, footer int) int {
11	return max(0, max(0, total)-max(0, header)-max(0, footer))
12}
13
14// Window keeps the current row visible and returns a half-open item range.
15// It only applies to fixed-height, one-line rows.
16func Window(total, cursor, top, height int) (int, int) {
17	if total <= 0 || height <= 0 {
18		return 0, 0
19	}
20	cursor = min(max(0, cursor), total-1)
21	top = min(max(0, top), max(0, total-height))
22	if cursor < top {
23		top = cursor
24	}
25	if cursor >= top+height {
26		top = cursor - height + 1
27	}
28	return top, min(total, top+height)
29}
30
31// SingleLine accepts plain data, not trusted ANSI. It neutralizes terminal
32// controls by replacing every C0/C1 control with a space. Escape parameters
33// remain visible text. This is deliberately not an ANSI-preserving sanitizer
34// or a Unicode spoofing defense. Application styling is added AFTER this call.
35func SingleLine(s string) string {
36	return strings.Map(func(r rune) rune {
37		if unicode.IsControl(r) || r == '\u2028' || r == '\u2029' {
38			return ' '
39		}
40		return r
41	}, strings.ToValidUTF8(s, "\uFFFD"))
42}