1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

WTF-389 Don't load widgets that have invalid position co-ordinates in their config

This commit is contained in:
Chris Cummer
2019-04-12 04:59:16 -07:00
parent 293e191f1e
commit c9c7e124cc
8 changed files with 203 additions and 100 deletions

View File

@@ -18,7 +18,6 @@ type BarGraph struct {
View *tview.TextView
Position
}
type Bar struct {
@@ -78,6 +77,12 @@ func (widget *BarGraph) FocusChar() string {
return ""
}
// IsPositionable returns TRUE if the widget has valid position parameters, FALSE if it has
// invalid position parameters (ie: cannot be placed onscreen)
func (widget *BarGraph) IsPositionable() bool {
return widget.Position.IsValid()
}
func (widget *BarGraph) RefreshInterval() int {
return widget.RefreshInt
}
@@ -154,11 +159,11 @@ func BuildStars(data []Bar, maxStars int, starChar string) string {
fmt.Sprintf(
"%s%s[[red]%s[white]%s] %s\n",
bar.Label,
strings.Repeat(" ", longestLabel - len(bar.Label)),
strings.Repeat(" ", longestLabel-len(bar.Label)),
strings.Repeat(starChar, starCount),
strings.Repeat(" ", maxStars - starCount),
strings.Repeat(" ", maxStars-starCount),
label,
),
),
)
}

View File

@@ -26,6 +26,10 @@ func (display *Display) add(widget Wtfable) {
return
}
if !widget.IsPositionable() {
return
}
display.Grid.AddItem(
widget.TextView(),
widget.Top(),

View File

@@ -3,5 +3,7 @@ package wtf
type Enabler interface {
Disabled() bool
Enabled() bool
IsPositionable() bool
Disable()
}

View File

@@ -18,18 +18,26 @@ func NewPosition(top, left, width, height int) Position {
return pos
}
func (pos *Position) Top() int {
return pos.top
func (pos *Position) IsValid() bool {
if pos.height < 1 || pos.left < 0 || pos.top < 0 || pos.width < 1 {
return false
}
return true
}
func (pos *Position) Height() int {
return pos.height
}
func (pos *Position) Left() int {
return pos.left
}
func (pos *Position) Top() int {
return pos.top
}
func (pos *Position) Width() int {
return pos.width
}
func (pos *Position) Height() int {
return pos.height
}

86
wtf/position_test.go Normal file
View File

@@ -0,0 +1,86 @@
package wtf
import (
"testing"
)
func Test_NewPosition(t *testing.T) {
pos := NewPosition(0, 1, 2, 3)
if pos.Height() != 3 {
t.Fatalf("Expected 3 but got %d", pos.Height())
}
if pos.Left() != 1 {
t.Fatalf("Expected 1 but got %d", pos.Left())
}
if pos.Top() != 0 {
t.Fatalf("Expected 0 but got %d", pos.Top())
}
if pos.Width() != 2 {
t.Fatalf("Expected 2 but got %d", pos.Width())
}
}
func Test_IsValid(t *testing.T) {
tests := []struct {
name string
height int
left int
top int
width int
expected bool
}{
{
name: "valid position",
height: 2,
left: 0,
top: 1,
width: 2,
expected: true,
},
{
name: "invalid height",
height: 0,
left: 0,
top: 1,
width: 2,
expected: false,
},
{
name: "invalid left",
height: 2,
left: -1,
top: 1,
width: 2,
expected: false,
},
{
name: "invalid top",
height: 2,
left: 0,
top: -1,
width: 2,
expected: false,
},
{
name: "invalid width",
height: 2,
left: 0,
top: 1,
width: 0,
expected: false,
},
}
for _, tt := range tests {
pos := NewPosition(tt.top, tt.left, tt.width, tt.height)
actual := pos.IsValid()
if actual != tt.expected {
t.Errorf("%s: expected: %v, got: %v", tt.name, tt.expected, actual)
}
}
}

View File

@@ -86,6 +86,12 @@ func (widget *TextWidget) FocusChar() string {
return widget.focusChar
}
// IsPositionable returns TRUE if the widget has valid position parameters, FALSE if it has
// invalid position parameters (ie: cannot be placed onscreen)
func (widget *TextWidget) IsPositionable() bool {
return widget.Position.IsValid()
}
func (widget *TextWidget) RefreshInterval() int {
return widget.RefreshInt
}