2018-04-15 06:06:57 +08:00
|
|
|
// Copyright 2018 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-04-07 20:24:55 +08:00
|
|
|
// Package testcanvas provides helpers for tests that use the canvas package.
|
|
|
|
package testcanvas
|
|
|
|
|
|
|
|
import (
|
2018-05-07 02:28:52 +08:00
|
|
|
"fmt"
|
2018-04-07 20:24:55 +08:00
|
|
|
"image"
|
|
|
|
|
|
|
|
"github.com/mum4k/termdash/canvas"
|
|
|
|
"github.com/mum4k/termdash/cell"
|
|
|
|
"github.com/mum4k/termdash/terminal/faketerm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MustNew returns a new canvas or panics.
|
|
|
|
func MustNew(area image.Rectangle) *canvas.Canvas {
|
|
|
|
cvs, err := canvas.New(area)
|
|
|
|
if err != nil {
|
2018-05-07 02:28:52 +08:00
|
|
|
panic(fmt.Sprintf("canvas.New => unexpected error: %v", err))
|
2018-04-07 20:24:55 +08:00
|
|
|
}
|
|
|
|
return cvs
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustApply applies the canvas on the terminal or panics.
|
|
|
|
func MustApply(c *canvas.Canvas, t *faketerm.Terminal) {
|
|
|
|
if err := c.Apply(t); err != nil {
|
2018-05-07 02:28:52 +08:00
|
|
|
panic(fmt.Sprintf("canvas.Apply => unexpected error: %v", err))
|
2018-04-07 20:24:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 05:50:07 +08:00
|
|
|
// MustSetCell sets the cell value or panics. Returns the number of cells the
|
|
|
|
// rune occupies, wide runes can occupy multiple cells when printed on the
|
|
|
|
// terminal. See http://www.unicode.org/reports/tr11/.
|
|
|
|
func MustSetCell(c *canvas.Canvas, p image.Point, r rune, opts ...cell.Option) int {
|
|
|
|
cells, err := c.SetCell(p, r, opts...)
|
|
|
|
if err != nil {
|
2018-05-07 02:28:52 +08:00
|
|
|
panic(fmt.Sprintf("canvas.SetCell => unexpected error: %v", err))
|
2018-04-07 20:24:55 +08:00
|
|
|
}
|
2018-05-21 05:50:07 +08:00
|
|
|
return cells
|
2018-04-07 20:24:55 +08:00
|
|
|
}
|