okami.mom/ultraviolet
git clone https://git.nkp.pet/okami.mom/ultraviolet
89c142e
upstream
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 { 7Top ,Right ,Bottom ,Left int 8} 9 10func (p Padding )apply (area uv.Rectangle ) uv.Rectangle { 11horizontal := p .Right + p .Left 12vertical := p .Top + p .Bottom 13 14if area .Dx ()< horizontal || area .Dy ()< vertical { 15return uv.Rectangle {} 16 } 17 18return uv .Rect ( 19area .Min .X + p .Left , 20area .Min .Y + p .Top , 21max (0 ,area .Dx ()- horizontal ), 22max (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 { 35switch len (sides ) { 36case 0 : 37return Padding {} 38 39case 1 : 40side := sides [0 ] 41 42return Padding {Top :side ,Right :side ,Bottom :side ,Left :side } 43 44case 2 : 45vertical := sides [0 ] 46horizontal := sides [1 ] 47 48return Padding { 49Top :vertical , 50Right :horizontal , 51Bottom :vertical , 52Left :horizontal , 53 } 54 55case 4 : 56return Padding { 57Top :sides [0 ], 58Right :sides [1 ], 59Bottom :sides [2 ], 60Left :sides [3 ], 61 } 62 63default : 64panic ("layout.Pad: unexpected sides count" ) 65 } 66}