BrailleLine can clear pixels as well.

This commit is contained in:
Jakub Sobon 2019-01-19 00:58:59 -05:00
parent 5919767099
commit 3eea41990e
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
3 changed files with 72 additions and 4 deletions

View File

@ -48,6 +48,13 @@ func MustSetPixel(bc *braille.Canvas, p image.Point, opts ...cell.Option) {
}
}
// MustClearPixel clears the specified pixel or panics.
func MustClearPixel(bc *braille.Canvas, p image.Point, opts ...cell.Option) {
if err := bc.ClearPixel(p, opts...); err != nil {
panic(fmt.Sprintf("braille.ClearPixel => unexpected error: %v", err))
}
}
// MustCopyTo copies the braille canvas onto the provided canvas or panics.
func MustCopyTo(bc *braille.Canvas, dst *canvas.Canvas) {
if err := bc.CopyTo(dst); err != nil {

View File

@ -24,6 +24,32 @@ import (
"github.com/mum4k/termdash/cell"
)
// braillePixelChange represents an action on a pixel on the braille canvas.
type braillePixelChange int
// String implements fmt.Stringer()
func (bpc braillePixelChange) String() string {
if n, ok := braillePixelChangeNames[bpc]; ok {
return n
}
return "braillePixelChangeUnknown"
}
// braillePixelChangeNames maps braillePixelChange values to human readable names.
var braillePixelChangeNames = map[braillePixelChange]string{
braillePixelChangeSet: "braillePixelChangeSet",
braillePixelChangeClear: "braillePixelChangeClear",
braillePixelToggle: "braillePixelToggle",
}
const (
braillePixelChangeUnknown braillePixelChange = iota
braillePixelChangeSet
braillePixelChangeClear
braillePixelToggle
)
// BrailleLineOption is used to provide options to BrailleLine().
type BrailleLineOption interface {
// set sets the provided option.
@ -32,12 +58,15 @@ type BrailleLineOption interface {
// brailleLineOptions stores the provided options.
type brailleLineOptions struct {
cellOpts []cell.Option
cellOpts []cell.Option
pixelChange braillePixelChange
}
// newBrailleLineOptions returns a new brailleLineOptions instance.
func newBrailleLineOptions() *brailleLineOptions {
return &brailleLineOptions{}
return &brailleLineOptions{
pixelChange: braillePixelChangeSet,
}
}
// brailleLineOption implements BrailleLineOption.
@ -57,6 +86,15 @@ func BrailleLineCellOpts(cOpts ...cell.Option) BrailleLineOption {
})
}
// BrailleLineClearPixels changes the behavior of BrailleLine, so that it
// clears the pixels belonging to the line instead of setting them.
// Useful in order to "erase" a line from the canvas as opposed to drawing one.
func BrailleLineClearPixels(cOpts ...cell.Option) BrailleLineOption {
return brailleLineOption(func(opts *brailleLineOptions) {
opts.pixelChange = braillePixelChangeClear
})
}
// BrailleLine draws an approximated line segment on the braille canvas between
// the two provided points.
// Both start and end must be valid points within the canvas. Start and end can
@ -78,8 +116,15 @@ func BrailleLine(bc *braille.Canvas, start, end image.Point, opts ...BrailleLine
points := brailleLinePoints(start, end)
for _, p := range points {
if err := bc.SetPixel(p, opt.cellOpts...); err != nil {
return fmt.Errorf("bc.SetPixel(%v) => %v", p, err)
switch opt.pixelChange {
case braillePixelChangeSet:
if err := bc.SetPixel(p, opt.cellOpts...); err != nil {
return fmt.Errorf("bc.SetPixel(%v) => %v", p, err)
}
case braillePixelChangeClear:
if err := bc.ClearPixel(p, opt.cellOpts...); err != nil {
return fmt.Errorf("bc.ClearPixel(%v) => %v", p, err)
}
}
}
return nil

View File

@ -97,6 +97,22 @@ func TestBrailleLine(t *testing.T) {
return ft
},
},
{
desc: "clears a single point",
canvas: image.Rect(0, 0, 1, 1),
start: image.Point{0, 0},
end: image.Point{0, 0},
opts: []BrailleLineOption{
BrailleLineClearPixels(),
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
bc := testbraille.MustNew(ft.Area())
testbraille.MustClearPixel(bc, image.Point{0, 0})
testbraille.MustApply(bc, ft)
return ft
},
},
{
desc: "draws single point with cell options",
canvas: image.Rect(0, 0, 1, 1),