okami.mom/ultraviolet

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

Ayman Bagabasdocs(layout): add ratatui acknowledgement89c142e

upstream
1.3 KiB66 linesraw
1package layout
2
3import uv "github.com/charmbracelet/ultraviolet"
4
5// Padding defines the inset applied to a [Layout]'s outer area before solving.
6type Padding struct {
7	Top, Right, Bottom, Left int
8}
9
10func (p Padding) apply(area uv.Rectangle) uv.Rectangle {
11	horizontal := p.Right + p.Left
12	vertical := p.Top + p.Bottom
13
14	if area.Dx() < horizontal || area.Dy() < vertical {
15		return uv.Rectangle{}
16	}
17
18	return uv.Rect(
19		area.Min.X+p.Left,
20		area.Min.Y+p.Top,
21		max(0, area.Dx()-horizontal),
22		max(0, area.Dy()-vertical),
23	)
24}
25
26// Pad builds a [Padding] value from a variable number of sides,
27// following the same shorthand convention as CSS:
28//   - 0 args: all sides zero.
29//   - 1 arg: uniform on every side.
30//   - 2 args: first is top/bottom, second is left/right.
31//   - 4 args: top, right, bottom, left.
32//
33// Any other count causes a panic.
34func Pad(sides ...int) Padding {
35	switch len(sides) {
36	case 0:
37		return Padding{}
38
39	case 1:
40		side := sides[0]
41
42		return Padding{Top: side, Right: side, Bottom: side, Left: side}
43
44	case 2:
45		vertical := sides[0]
46		horizontal := sides[1]
47
48		return Padding{
49			Top:    vertical,
50			Right:  horizontal,
51			Bottom: vertical,
52			Left:   horizontal,
53		}
54
55	case 4:
56		return Padding{
57			Top:    sides[0],
58			Right:  sides[1],
59			Bottom: sides[2],
60			Left:   sides[3],
61		}
62
63	default:
64		panic("layout.Pad: unexpected sides count")
65	}
66}