Propagate the error when drawing the containers.

This commit is contained in:
Jakub Sobon 2018-04-01 01:02:37 +02:00
parent 3a3531d7e1
commit af6c5e9c81
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
2 changed files with 14 additions and 9 deletions

View File

@ -8,6 +8,7 @@ canvases assigned to the placed widgets.
package container
import (
"errors"
"fmt"
"image"
@ -203,22 +204,24 @@ func (c *Container) draw() error {
// Draw draws this container and all of its sub containers.
func (c *Container) Draw() error {
// TODO(mum4k): Handle resize or split to area too small.
// TODO(mum4k): Propagate error.
// TODO(mum4k): Don't require .Root() at the end.
drawTree(c)
var errStr string
drawTree(c, &errStr)
if errStr != "" {
return errors.New(errStr)
}
return nil
}
// drawTree implements pre-order BST walk through the containers and draws each
// visited container.
func drawTree(c *Container) {
if c == nil {
func drawTree(c *Container, errStr *string) {
if c == nil || *errStr != "" {
return
}
if err := c.draw(); err != nil {
panic(err)
*errStr = err.Error()
return
}
drawTree(c.first)
drawTree(c.second)
drawTree(c.first, errStr)
drawTree(c.second, errStr)
}

View File

@ -27,6 +27,8 @@ func Example() {
).Root().Second( // Bottom half of the terminal.
PlaceWidget( /* widget = */ nil),
).Root()
// TODO(mum4k): Don't require .Root() at the end.
// TODO(mum4k): Allow splits on different ratios.
}
func TestParentAndRoot(t *testing.T) {