Skip to content

Commit

Permalink
Check negative input
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Apr 24, 2020
1 parent 1ad3d1d commit dbe37b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion widget/textgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,13 @@ func (t *TextGrid) SetStyle(row, col int, style TextGridStyle) {

// SetStyleRange sets a grid style to all the cells between the start row and column through to the end row and column.
func (t *TextGrid) SetStyleRange(startRow, startCol, endRow, endCol int, style TextGridStyle) {
if startRow >= len(t.Content) {
if startRow >= len(t.Content) || endRow < 0 {
return
}
if startRow < 0 {
startRow = 0
startCol = 0
}
if endRow >= len(t.Content) {
endRow = len(t.Content) - 1
endCol = len(t.Content[endRow]) - 1
Expand Down
5 changes: 5 additions & 0 deletions widget/textgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,18 @@ func TestTextGrid_SetStyleRange(t *testing.T) {
func TestTextGrid_SetStyleRange_Overflow(t *testing.T) {
grid := NewTextGridFromString("Ab\ncd")

grid.SetStyleRange(-2, 0, -1, 2, &CustomTextGridStyle{FGColor: color.White, BGColor: color.Black})
grid.SetStyleRange(2, 2, 4, 2, &CustomTextGridStyle{FGColor: color.White, BGColor: color.Black})
assert.Nil(t, grid.Content[0][0].Style)
assert.Nil(t, grid.Content[0][1].Style)
assert.Nil(t, grid.Content[1][0].Style)
assert.Nil(t, grid.Content[1][1].Style)

grid.SetStyleRange(-2, 0, 0, 0, &CustomTextGridStyle{FGColor: color.Black, BGColor: color.White})
grid.SetStyleRange(1, 1, 4, 0, &CustomTextGridStyle{FGColor: color.White, BGColor: color.Black})
assert.Equal(t, color.Black, grid.Content[0][0].Style.TextColor())
assert.Equal(t, color.White, grid.Content[0][0].Style.BackgroundColor())
assert.Nil(t, grid.Content[0][1].Style)
assert.Nil(t, grid.Content[1][0].Style)
assert.Equal(t, color.White, grid.Content[1][1].Style.TextColor())
assert.Equal(t, color.Black, grid.Content[1][1].Style.BackgroundColor())
Expand Down

0 comments on commit dbe37b9

Please sign in to comment.