From 009f865bc5484e54f09d2173b2b3dbbf3838d391 Mon Sep 17 00:00:00 2001 From: Clement Benard Date: Thu, 11 Jul 2019 10:50:37 +0200 Subject: Added SplitFunc type, documented everything exported --- layout/split.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 layout/split.go (limited to 'layout/split.go') diff --git a/layout/split.go b/layout/split.go new file mode 100644 index 0000000..4bf00a7 --- /dev/null +++ b/layout/split.go @@ -0,0 +1,19 @@ +package layout + +// SplitFunc represents a way to split a space among a number of elements. +// The space 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 + +// EvenSplit implements SplitFunc 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 { + ret := make([]int, 0, elements) + for elements > 0 { + v := width / elements + width -= v + elements -= 1 + ret = append(ret, v) + } + return ret +} -- cgit v1.2.3 From 0cc6367a881ab7dba48ace7b111e0eb1151c10bb Mon Sep 17 00:00:00 2001 From: Clement Benard Date: Fri, 12 Jul 2019 17:23:28 +0200 Subject: Better separation between Layout and Mux --- layout/split.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'layout/split.go') diff --git a/layout/split.go b/layout/split.go index 4bf00a7..fb6d36e 100644 --- a/layout/split.go +++ b/layout/split.go @@ -1,13 +1,18 @@ package layout +import "fmt" + // SplitFunc represents a way to split a space among a number of elements. -// The space of the returned slice must be equal to the 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 -// EvenSplit implements SplitFunc to split a space (almost) evenly among the elements. +// 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, 0, elements) for elements > 0 { v := width / elements -- cgit v1.2.3 From 8d183ef96a57e3a2f42c0cb4ec0ab4c256e0d47e Mon Sep 17 00:00:00 2001 From: Clement Benard Date: Wed, 7 Aug 2019 16:02:33 +0200 Subject: Made the layout package actually usable --- layout/split.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'layout/split.go') diff --git a/layout/split.go b/layout/split.go index fb6d36e..db04225 100644 --- a/layout/split.go +++ b/layout/split.go @@ -7,18 +7,20 @@ import "fmt" // 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, 0, elements) + ret := make([]int, elements) for elements > 0 { v := width / elements width -= v elements -= 1 - ret = append(ret, v) + ret[elements] = v } return ret } -- cgit v1.2.3