correct bounds checks on views

This commit is contained in:
Alex Goodman 2018-06-24 12:57:25 -04:00
parent c9b1d3dd3c
commit 6db7929a02
No known key found for this signature in database
GPG Key ID: 05328C611D8A520E
3 changed files with 11 additions and 6 deletions

View File

@ -116,6 +116,8 @@ func (view *FileTreeView) setLayer(layerIndex int) error {
} }
func (view *FileTreeView) CursorDown() error { func (view *FileTreeView) CursorDown() error {
// cannot easily (quickly) check the model length, allow the view
// to let us know what is a valid bounds (i.e. when it hits an empty line)
err := CursorDown(view.gui, view.view) err := CursorDown(view.gui, view.view)
if err == nil { if err == nil {
view.TreeIndex++ view.TreeIndex++
@ -124,10 +126,12 @@ func (view *FileTreeView) CursorDown() error {
} }
func (view *FileTreeView) CursorUp() error { func (view *FileTreeView) CursorUp() error {
if view.TreeIndex > 0 {
err := CursorUp(view.gui, view.view) err := CursorUp(view.gui, view.view)
if err == nil { if err == nil {
view.TreeIndex-- view.TreeIndex--
} }
}
return view.Render() return view.Render()
} }

View File

@ -79,7 +79,7 @@ func (view *LayerView) Render() error {
} }
func (view *LayerView) CursorDown() error { func (view *LayerView) CursorDown() error {
if int(view.LayerIndex) < len(view.Layers) { if view.LayerIndex < len(view.Layers) {
err := CursorDown(view.gui, view.view) err := CursorDown(view.gui, view.view)
if err == nil { if err == nil {
view.LayerIndex++ view.LayerIndex++
@ -91,7 +91,7 @@ func (view *LayerView) CursorDown() error {
} }
func (view *LayerView) CursorUp() error { func (view *LayerView) CursorUp() error {
if int(view.LayerIndex) > 0 { if view.LayerIndex > 0 {
err := CursorUp(view.gui, view.view) err := CursorUp(view.gui, view.view)
if err == nil { if err == nil {
view.LayerIndex-- view.LayerIndex--

View File

@ -7,6 +7,7 @@ import (
"github.com/wagoodman/docker-image-explorer/filetree" "github.com/wagoodman/docker-image-explorer/filetree"
"github.com/wagoodman/docker-image-explorer/image" "github.com/wagoodman/docker-image-explorer/image"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/wagoodman/docker-image-explorer/_vendor-20180604210951/github.com/pkg/errors"
) )
const debug = false const debug = false
@ -51,7 +52,7 @@ func CursorDown(g *gocui.Gui, v *gocui.View) error {
// todo: handle error // todo: handle error
} }
if len(line) == 0 { if len(line) == 0 {
return nil return errors.New("unable to move cursor down, empty line")
} }
if err := v.SetCursor(cx, cy+1); err != nil { if err := v.SetCursor(cx, cy+1); err != nil {
ox, oy := v.Origin() ox, oy := v.Origin()