diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-01-19 22:07:42 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-01-19 22:07:42 -0500 |
| commit | 54d71a24b6eaa191b2777f6070de252fc26801a3 (patch) | |
| tree | 1d26f49dfcacc5578b4e6c91e8ffb31b09a9f662 /gui/layout/split.go | |
| parent | 39a3b948e525fe2b22b90e07df323ab77f6a81f6 (diff) | |
| download | volute-54d71a24b6eaa191b2777f6070de252fc26801a3.zip | |
add layout from Keitio
Diffstat (limited to 'gui/layout/split.go')
| -rw-r--r-- | gui/layout/split.go | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/gui/layout/split.go b/gui/layout/split.go new file mode 100644 index 0000000..db04225 --- /dev/null +++ b/gui/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 +} |