Restore HSplitCells to original function signature

This commit is contained in:
Matthew Coleman 2024-03-07 17:14:50 -05:00
parent 4465700c5d
commit 275c62fc47
4 changed files with 52 additions and 47 deletions

View File

@ -182,7 +182,7 @@ func (c *Container) split() (image.Rectangle, image.Rectangle, error) {
if c.opts.split == splitTypeVertical {
return area.VSplitCells(ar, c.opts.splitFixed)
}
return area.HSplitCells(ar, c.opts.splitFixed, false)
return area.HSplitCells(ar, c.opts.splitFixed)
}
if c.opts.split == splitTypeVertical {

View File

@ -22,6 +22,41 @@ import (
"github.com/mum4k/termdash/private/numbers"
)
// TODO
func hSplitCells(area image.Rectangle, cells int, fromMax bool) (top image.Rectangle, bottom image.Rectangle, err error) {
if min := 0; cells < min {
return image.ZR, image.ZR, fmt.Errorf("invalid cells %d, must be a positive integer", cells)
}
if cells == 0 {
if fromMax {
return area, image.ZR, nil
} else {
return image.ZR, area, nil
}
}
height := area.Dy()
if cells >= height {
if fromMax {
return image.ZR, area, nil
} else {
return area, image.ZR, nil
}
}
splitY := area.Min.Y
if fromMax {
splitY = area.Max.Y - cells
} else {
splitY = area.Min.Y + cells
}
top = image.Rect(area.Min.X, area.Min.Y, area.Max.X, splitY)
bottom = image.Rect(area.Min.X, splitY, area.Max.X, area.Max.Y)
return top, bottom, nil
}
// Size returns the size of the provided area.
func Size(area image.Rectangle) image.Point {
return image.Point{
@ -102,34 +137,21 @@ func VSplitCells(area image.Rectangle, cells int) (left image.Rectangle, right i
}
// HSplitCells returns two new areas created by splitting the provided area
// after the specified amount of cells of its height. The number of cells must
// be a zero or a positive integer. Providing a zero returns top=image.ZR,
// bottom=area. Providing a number equal or larger to area's height returns
// top=area, bottom=image.ZR.
func HSplitCells(area image.Rectangle, cells int, fromMax bool) (top image.Rectangle, bottom image.Rectangle, err error) {
if min := 0; cells < min {
return image.ZR, image.ZR, fmt.Errorf("invalid cells %d, must be a positive integer", cells)
}
if cells == 0 {
return image.ZR, area, nil
}
// after the specified amount of cells of its height, as applied to the first
// area. The number of cells must be a zero or a positive integer. Providing a
// zero returns top=image.ZR, bottom=area. Providing a number equal or larger to
// area's height returns top=area, bottom=image.ZR.
func HSplitCells(area image.Rectangle, cells int) (top image.Rectangle, bottom image.Rectangle, err error) {
return hSplitCells(area, cells, false)
}
height := area.Dy()
if cells >= height {
return area, image.ZR, nil
}
splitY := area.Min.Y
if fromMax {
splitY = area.Max.Y - cells
} else {
splitY = area.Min.Y + cells
}
top = image.Rect(area.Min.X, area.Min.Y, area.Max.X, splitY)
bottom = image.Rect(area.Min.X, splitY, area.Max.X, area.Max.Y)
return top, bottom, nil
// HSplitCells returns two new areas created by splitting the provided area
// after the specified amount of cells of its height, as applied to the second
// area. The number of cells must be a zero or a positive integer. Providing a
// zero returns top=area, bottom=image.ZR. Providing a number equal or larger to
// area's height returns top=image.ZR, bottom=area.
func HSplitCellsReversed(area image.Rectangle, cells int) (top image.Rectangle, bottom image.Rectangle, err error) {
return hSplitCells(area, cells, true)
}
// ExcludeBorder returns a new area created by subtracting a border around the

View File

@ -372,7 +372,6 @@ func TestHSplitCells(t *testing.T) {
desc string
area image.Rectangle
cells int
fromMax bool
wantTop image.Rectangle
wantBottom image.Rectangle
wantErr bool
@ -418,14 +417,6 @@ func TestHSplitCells(t *testing.T) {
wantTop: image.Rect(1, 1, 3, 2),
wantBottom: image.Rect(1, 2, 3, 3),
},
{
desc: "splits area with even height from max",
area: image.Rect(1, 1, 3, 3),
cells: 1,
fromMax: true,
wantTop: image.Rect(1, 1, 3, 2),
wantBottom: image.Rect(1, 2, 3, 3),
},
{
desc: "splits area with odd width",
area: image.Rect(1, 1, 4, 4),
@ -440,19 +431,11 @@ func TestHSplitCells(t *testing.T) {
wantTop: image.Rect(0, 0, 4, 3),
wantBottom: image.Rect(0, 3, 4, 4),
},
{
desc: "splits to unequal areas from max",
area: image.Rect(0, 0, 4, 4),
cells: 3,
fromMax: true,
wantTop: image.Rect(0, 0, 4, 1),
wantBottom: image.Rect(0, 1, 4, 4),
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
gotTop, gotBottom, err := HSplitCells(tc.area, tc.cells, tc.fromMax)
gotTop, gotBottom, err := HSplitCells(tc.area, tc.cells)
if (err != nil) != tc.wantErr {
t.Errorf("HSplitCells => unexpected error:%v, wantErr:%v", err, tc.wantErr)
}

View File

@ -306,7 +306,7 @@ func donutAndLabel(cvsAr image.Rectangle) (donAr, labelAr image.Rectangle, err e
// Two lines for the text label at the bottom.
// One for the text itself and one for visual space between the donut and
// the label.
donAr, labelAr, err = area.HSplitCells(cvsAr, height-2, false)
donAr, labelAr, err = area.HSplitCells(cvsAr, height-2)
if err != nil {
return image.ZR, image.ZR, err
}