1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-25 13:48:50 +08:00

Allow reverse horizontal area splits

This commit is contained in:
Matthew Coleman 2024-03-05 11:00:54 -05:00
parent bc63a16bf8
commit 4465700c5d
4 changed files with 32 additions and 7 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)
return area.HSplitCells(ar, c.opts.splitFixed, false)
}
if c.opts.split == splitTypeVertical {

View File

@ -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
}

View File

@ -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)
}

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)
donAr, labelAr, err = area.HSplitCells(cvsAr, height-2, false)
if err != nil {
return image.ZR, image.ZR, err
}