Skip to content

Commit

Permalink
refactor: extract slice and integer methods to helper file (utils.go)
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Oct 5, 2023
1 parent f184359 commit 959f516
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 59 deletions.
69 changes: 10 additions & 59 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -330,7 +329,7 @@ func (t *Table) String() string {
}

for width > t.width {
index := largest(differences)
index, _ := largest(differences)
if differences[index] < 1 {
break
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
61 changes: 61 additions & 0 deletions table/util.go
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 46 in table/util.go

View workflow job for this annotation

GitHub Actions / lint-soft

mnd: Magic number: 2, in <return> detected (gomnd)
}
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

Check failure on line 52 in table/util.go

View workflow job for this annotation

GitHub Actions / lint-soft

directive `// nolint: unparam` should be written without leading space as `//nolint: unparam` (nolintlint)
var largest, index int
for i, e := range n {
if n[i] > n[index] {
largest = e
index = i
}
}
return index, largest
}

0 comments on commit 959f516

Please sign in to comment.