winop.go

 1package ansi
 2
 3import (
 4	"strconv"
 5	"strings"
 6)
 7
 8const (
 9	// ResizeWindowWinOp is a window operation that resizes the terminal
10	// window.
11	ResizeWindowWinOp = 4
12
13	// RequestWindowSizeWinOp is a window operation that requests a report of
14	// the size of the terminal window in pixels. The response is in the form:
15	//  CSI 4 ; height ; width t
16	RequestWindowSizeWinOp = 14
17
18	// RequestCellSizeWinOp is a window operation that requests a report of
19	// the size of the terminal cell size in pixels. The response is in the form:
20	//  CSI 6 ; height ; width t
21	RequestCellSizeWinOp = 16
22)
23
24// WindowOp (XTWINOPS) is a sequence that manipulates the terminal window.
25//
26//	CSI Ps ; Ps ; Ps t
27//
28// Ps is a semicolon-separated list of parameters.
29// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h4-Functions-using-CSI-_-ordered-by-the-final-character-lparen-s-rparen:CSI-Ps;Ps;Ps-t.1EB0
30func WindowOp(p int, ps ...int) string {
31	if p <= 0 {
32		return ""
33	}
34
35	if len(ps) == 0 {
36		return "\x1b[" + strconv.Itoa(p) + "t"
37	}
38
39	params := make([]string, 0, len(ps)+1)
40	params = append(params, strconv.Itoa(p))
41	for _, p := range ps {
42		if p >= 0 {
43			params = append(params, strconv.Itoa(p))
44		}
45	}
46
47	return "\x1b[" + strings.Join(params, ";") + "t"
48}
49
50// XTWINOPS is an alias for [WindowOp].
51func XTWINOPS(p int, ps ...int) string {
52	return WindowOp(p, ps...)
53}