diff --git a/container/container.go b/container/container.go index 6070efe..0bfa7c5 100644 --- a/container/container.go +++ b/container/container.go @@ -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) + return area.HSplitCells(ar, c.opts.splitFixed, false) } if c.opts.split == splitTypeVertical { diff --git a/private/area/area.go b/private/area/area.go index 34b21a1..9e2040c 100644 --- a/private/area/area.go +++ b/private/area/area.go @@ -39,7 +39,7 @@ func FromSize(size image.Point) (image.Rectangle, error) { } // HSplit returns two new areas created by splitting the provided area at the -// specified percentage of its width. The percentage must be in the range +// specified percentage of its height. The percentage must be in the range // 0 <= heightPerc <= 100. // Can return zero size areas. func HSplit(area image.Rectangle, heightPerc int) (top image.Rectangle, bottom image.Rectangle, err error) { @@ -106,7 +106,7 @@ func VSplitCells(area image.Rectangle, cells int) (left image.Rectangle, right i // 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) { +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) } @@ -119,8 +119,16 @@ func HSplitCells(area image.Rectangle, cells int) (top image.Rectangle, bottom i return area, image.ZR, nil } - top = image.Rect(area.Min.X, area.Min.Y, area.Max.X, area.Min.Y+cells) - bottom = image.Rect(area.Min.X, area.Min.Y+cells, area.Max.X, area.Max.Y) + 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 } diff --git a/private/area/area_test.go b/private/area/area_test.go index f4a46eb..62141ce 100644 --- a/private/area/area_test.go +++ b/private/area/area_test.go @@ -372,6 +372,7 @@ func TestHSplitCells(t *testing.T) { desc string area image.Rectangle cells int + fromMax bool wantTop image.Rectangle wantBottom image.Rectangle wantErr bool @@ -417,6 +418,14 @@ 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), @@ -431,11 +440,19 @@ 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) + gotTop, gotBottom, err := HSplitCells(tc.area, tc.cells, tc.fromMax) if (err != nil) != tc.wantErr { t.Errorf("HSplitCells => unexpected error:%v, wantErr:%v", err, tc.wantErr) } diff --git a/widgets/donut/donut.go b/widgets/donut/donut.go index 7d98792..a95268d 100644 --- a/widgets/donut/donut.go +++ b/widgets/donut/donut.go @@ -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) + donAr, labelAr, err = area.HSplitCells(cvsAr, height-2, false) if err != nil { return image.ZR, image.ZR, err }