diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-08-17 14:05:07 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-08-17 14:05:07 -0400 |
| commit | 32f1b20c3e93457dec949e7e426fd3ab17dc3d5c (patch) | |
| tree | 201321e995e9f74cab877dac64e74eb654946f1b /layout/split.go | |
| parent | ee1ecb5c17ebe98d18dc62390ecb6c09f648a52e (diff) | |
| parent | 8d183ef96a57e3a2f42c0cb4ec0ab4c256e0d47e (diff) | |
| download | gui-32f1b20c3e93457dec949e7e426fd3ab17dc3d5c.zip | |
Merge remote-tracking branch 'keitio/master'
layout
Diffstat (limited to 'layout/split.go')
| -rw-r--r-- | layout/split.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/layout/split.go b/layout/split.go new file mode 100644 index 0000000..db04225 --- /dev/null +++ b/layout/split.go @@ -0,0 +1,26 @@ +package layout + +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 +} |