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

Functions that move areas up and down.

This commit is contained in:
Jakub Sobon 2019-04-29 22:55:49 -04:00
parent f81cb554b0
commit b8a6427d47
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
2 changed files with 140 additions and 0 deletions

View File

@ -224,3 +224,35 @@ func ShrinkPercent(area image.Rectangle, topPerc, rightPerc, bottomPerc, leftPer
left := area.Dx() * leftPerc / 100 left := area.Dx() * leftPerc / 100
return Shrink(area, top, right, bottom, left) return Shrink(area, top, right, bottom, left)
} }
// MoveUp returns a new area that is moved up by the specified amount of cells.
// Returns an error if the move would result in negative Y coordinates.
// The values must be zero or positive integers.
func MoveUp(area image.Rectangle, cells int) (image.Rectangle, error) {
if min := 0; cells < min {
return image.ZR, fmt.Errorf("cannot move area %v up by %d cells, must be in range %d <= value", area, cells, min)
}
if area.Min.Y < cells {
return image.ZR, fmt.Errorf("cannot move area %v up by %d cells, would result in negative Y coordinate", area, cells)
}
moved := area
moved.Min.Y -= cells
moved.Max.Y -= cells
return moved, nil
}
// MoveDown returns a new area that is moved down by the specified amount of
// cells.
// The values must be zero or positive integers.
func MoveDown(area image.Rectangle, cells int) (image.Rectangle, error) {
if min := 0; cells < min {
return image.ZR, fmt.Errorf("cannot move area %v down by %d cells, must be in range %d <= value", area, cells, min)
}
moved := area
moved.Min.Y += cells
moved.Max.Y += cells
return moved, nil
}

View File

@ -871,3 +871,111 @@ func TestShrinkPercent(t *testing.T) {
}) })
} }
} }
func TestMoveUp(t *testing.T) {
tests := []struct {
desc string
area image.Rectangle
cells int
want image.Rectangle
wantErr bool
}{
{
desc: "fails on negative cells",
area: image.Rect(0, 0, 1, 1),
cells: -1,
wantErr: true,
},
{
desc: "zero area cannot be moved",
area: image.ZR,
cells: 1,
wantErr: true,
},
{
desc: "cannot move area beyond zero Y coordinate",
area: image.Rect(0, 5, 1, 10),
cells: 6,
wantErr: true,
},
{
desc: "move by zero cells is idempotent",
area: image.Rect(0, 5, 1, 10),
cells: 0,
want: image.Rect(0, 5, 1, 10),
},
{
desc: "moves area up",
area: image.Rect(0, 5, 1, 10),
cells: 3,
want: image.Rect(0, 2, 1, 7),
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
got, err := MoveUp(tc.area, tc.cells)
if (err != nil) != tc.wantErr {
t.Errorf("MoveUp => unexpected error: %v, wantErr: %v", err, tc.wantErr)
}
if err != nil {
return
}
if diff := pretty.Compare(tc.want, got); diff != "" {
t.Errorf("MoveUp => unexpected diff (-want, +got):\n%s", diff)
}
})
}
}
func TestMoveDown(t *testing.T) {
tests := []struct {
desc string
area image.Rectangle
cells int
want image.Rectangle
wantErr bool
}{
{
desc: "fails on negative cells",
area: image.Rect(0, 0, 1, 1),
cells: -1,
wantErr: true,
},
{
desc: "moves zero area",
area: image.ZR,
cells: 1,
want: image.Rect(0, 1, 0, 1),
},
{
desc: "move by zero cells is idempotent",
area: image.Rect(0, 5, 1, 10),
cells: 0,
want: image.Rect(0, 5, 1, 10),
},
{
desc: "moves area down",
area: image.Rect(0, 5, 1, 10),
cells: 3,
want: image.Rect(0, 8, 1, 13),
},
}
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
got, err := MoveDown(tc.area, tc.cells)
if (err != nil) != tc.wantErr {
t.Errorf("MoveDown => unexpected error: %v, wantErr: %v", err, tc.wantErr)
}
if err != nil {
return
}
if diff := pretty.Compare(tc.want, got); diff != "" {
t.Errorf("MoveDown => unexpected diff (-want, +got):\n%s", diff)
}
})
}
}