From e725799ad17c575a7a3cd2536f57bd82f85782e5 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sat, 24 Aug 2024 15:13:09 -0400 Subject: adapt grid to new layout design --- split.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 split.go (limited to 'split.go') diff --git a/split.go b/split.go new file mode 100644 index 0000000..ab6789b --- /dev/null +++ b/split.go @@ -0,0 +1,26 @@ +package gui + +import "fmt" + +// SplitFunc represents a way to split a space among a number of elements. +// The length of the returned slice must be equal to the number of elements. +// The sum of all elements of the returned slice must be eqal to the space. +type SplitFunc func(elements int, space int) []int + +var _ SplitFunc = EvenSplit + +// EvenSplit is a SplitFunc used to split a space (almost) evenly among the elements. +// It is almost evenly because width may not be divisible by elements. +func EvenSplit(elements int, width int) []int { + if elements <= 0 { + panic(fmt.Errorf("EvenSplit: elements must be greater than 0")) + } + ret := make([]int, elements) + for elements > 0 { + v := width / elements + width -= v + elements -= 1 + ret[elements] = v + } + return ret +} -- cgit v1.2.3