From 959f5160a6d4760cb0d21ddd1fedb5d03f43b313 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Thu, 5 Oct 2023 11:23:14 -0400 Subject: [PATCH] refactor: extract slice and integer methods to helper file (utils.go) --- table/table.go | 69 ++++++++------------------------------------------ table/util.go | 61 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 59 deletions(-) create mode 100644 table/util.go diff --git a/table/table.go b/table/table.go index e7f0c3b5..5826c316 100644 --- a/table/table.go +++ b/table/table.go @@ -6,7 +6,6 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/mattn/go-runewidth" - "golang.org/x/exp/slices" ) // StyleFunc is the style function that determines the style of a Cell. @@ -330,7 +329,7 @@ func (t *Table) String() string { } for width > t.width { - index := largest(differences) + index, _ := largest(differences) if differences[index] < 1 { break } @@ -344,7 +343,7 @@ func (t *Table) String() string { // Table is still too wide, begin shrinking the columns based on the // largest column. for width > t.width { - index := largest(t.widths) + index, _ := largest(t.widths) if t.widths[index] < 1 { break } @@ -399,62 +398,8 @@ func (t *Table) Render() string { return t.String() } -// btoi converts a boolean to an integer, 1 if true, 0 if false. -func btoi(b bool) int { - if b { - return 1 - } - return 0 -} - -// max returns the greater of two integers. -func max(a, b int) int { - if a > b { - return a - } - return b -} - -// min returns the greater of two integers. -func min(a, b int) int { - if a < b { - return a - } - return b -} - -// sum returns the sum of all integers in a slice. -func sum(n []int) int { - var sum int - for _, i := range n { - sum += i - } - return sum -} - -// median returns the median of a slice of integers. -func median(n []int) int { - slices.Sort(n) - - if len(n) <= 0 { - return 0 - } - if len(n)%2 == 0 { - return (n[len(n)/2-1] + n[len(n)/2]) / 2 - } - return n[len(n)/2] -} - -func largest(n []int) int { - var largest int - for i := range n { - if n[i] > n[largest] { - largest = i - } - } - return largest -} - +// constructTopBorder constructs the top border for the table given it's current +// border configuration and data. func (t *Table) constructTopBorder() string { var s strings.Builder if t.borderLeft { @@ -472,6 +417,8 @@ func (t *Table) constructTopBorder() string { return s.String() } +// constructBottomBorder constructs the bottom border for the table given it's current +// border configuration and data. func (t *Table) constructBottomBorder() string { var s strings.Builder if t.borderLeft { @@ -489,6 +436,8 @@ func (t *Table) constructBottomBorder() string { return s.String() } +// constructHeaders constructs the headers for the table given it's current +// header configuration and data. func (t *Table) constructHeaders() string { var s strings.Builder if t.borderLeft { @@ -528,6 +477,8 @@ func (t *Table) constructHeaders() string { return s.String() } +// constructRow constructs the row for the table given an index and row data +// based on the current configuration. func (t *Table) constructRow(index int, row Row) string { var s strings.Builder diff --git a/table/util.go b/table/util.go new file mode 100644 index 00000000..715cf942 --- /dev/null +++ b/table/util.go @@ -0,0 +1,61 @@ +package table + +import "golang.org/x/exp/slices" + +// btoi converts a boolean to an integer, 1 if true, 0 if false. +func btoi(b bool) int { + if b { + return 1 + } + return 0 +} + +// max returns the greater of two integers. +func max(a, b int) int { + if a > b { + return a + } + return b +} + +// min returns the greater of two integers. +func min(a, b int) int { + if a < b { + return a + } + return b +} + +// sum returns the sum of all integers in a slice. +func sum(n []int) int { + var sum int + for _, i := range n { + sum += i + } + return sum +} + +// median returns the median of a slice of integers. +func median(n []int) int { + slices.Sort(n) + + if len(n) <= 0 { + return 0 + } + if len(n)%2 == 0 { + return (n[len(n)/2-1] + n[len(n)/2]) / 2 + } + return n[len(n)/2] +} + +// largest returns the largest element and it's index from a slice of integers. +func largest(n []int) (int, int) { // nolint: unparam + var largest, index int + for i, e := range n { + if n[i] > n[index] { + largest = e + index = i + } + } + return index, largest +}