mirror of https://github.com/mum4k/termdash.git
First test for sixteen.
This commit is contained in:
parent
a2c4512b47
commit
2e597e8b60
|
@ -24,25 +24,25 @@ import (
|
||||||
"github.com/mum4k/termdash/draw"
|
"github.com/mum4k/termdash/draw"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SegmentType identifies the type of the segment that is drawn.
|
// Type identifies the type of the segment that is drawn.
|
||||||
type SegmentType int
|
type Type int
|
||||||
|
|
||||||
// String implements fmt.Stringer()
|
// String implements fmt.Stringer()
|
||||||
func (st SegmentType) String() string {
|
func (st Type) String() string {
|
||||||
if n, ok := segmentTypeNames[st]; ok {
|
if n, ok := segmentTypeNames[st]; ok {
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
return "SegmentTypeUnknown"
|
return "TypeUnknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
// segmentTypeNames maps SegmentType values to human readable names.
|
// segmentTypeNames maps Type values to human readable names.
|
||||||
var segmentTypeNames = map[SegmentType]string{
|
var segmentTypeNames = map[Type]string{
|
||||||
Horizontal: "Horizontal",
|
Horizontal: "Horizontal",
|
||||||
Vertical: "Vertical",
|
Vertical: "Vertical",
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
segmentTypeUnknown SegmentType = iota
|
segmentTypeUnknown Type = iota
|
||||||
|
|
||||||
// Horizontal is a horizontal segment.
|
// Horizontal is a horizontal segment.
|
||||||
Horizontal
|
Horizontal
|
||||||
|
@ -125,7 +125,7 @@ func validArea(ar image.Rectangle) error {
|
||||||
|
|
||||||
// HV draws a horizontal or a vertical display segment, filling the provided area.
|
// HV draws a horizontal or a vertical display segment, filling the provided area.
|
||||||
// The segment will have slopes on both of its ends.
|
// The segment will have slopes on both of its ends.
|
||||||
func HV(bc *braille.Canvas, ar image.Rectangle, st SegmentType, opts ...Option) error {
|
func HV(bc *braille.Canvas, ar image.Rectangle, st Type, opts ...Option) error {
|
||||||
if err := validArea(ar); err != nil {
|
if err := validArea(ar); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@ func TestHV(t *testing.T) {
|
||||||
opts []Option
|
opts []Option
|
||||||
cellCanvas image.Rectangle // Canvas in cells that will be converted to braille canvas for drawing.
|
cellCanvas image.Rectangle // Canvas in cells that will be converted to braille canvas for drawing.
|
||||||
ar image.Rectangle
|
ar image.Rectangle
|
||||||
st SegmentType
|
st Type
|
||||||
want func(size image.Point) *faketerm.Terminal
|
want func(size image.Point) *faketerm.Terminal
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
|
@ -84,14 +84,14 @@ func TestHV(t *testing.T) {
|
||||||
desc: "fails on unsupported segment type (too small)",
|
desc: "fails on unsupported segment type (too small)",
|
||||||
cellCanvas: image.Rect(0, 0, 1, 1),
|
cellCanvas: image.Rect(0, 0, 1, 1),
|
||||||
ar: image.Rect(0, 0, 2, 2),
|
ar: image.Rect(0, 0, 2, 2),
|
||||||
st: SegmentType(0),
|
st: Type(0),
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "fails on unsupported segment type (too large)",
|
desc: "fails on unsupported segment type (too large)",
|
||||||
cellCanvas: image.Rect(0, 0, 1, 1),
|
cellCanvas: image.Rect(0, 0, 1, 1),
|
||||||
ar: image.Rect(0, 0, 2, 2),
|
ar: image.Rect(0, 0, 2, 2),
|
||||||
st: SegmentType(int(Vertical) + 1),
|
st: Type(int(Vertical) + 1),
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1112,7 +1112,7 @@ func TestHV(t *testing.T) {
|
||||||
// hvSegment is one horizontal or vertical segment.
|
// hvSegment is one horizontal or vertical segment.
|
||||||
type hvSegment struct {
|
type hvSegment struct {
|
||||||
ar image.Rectangle
|
ar image.Rectangle
|
||||||
st SegmentType
|
st Type
|
||||||
}
|
}
|
||||||
|
|
||||||
// diagSegment is one diagonal segment.
|
// diagSegment is one diagonal segment.
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
// Package testsegment provides helpers for tests that use the segment package.
|
||||||
|
package testsegment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
|
|
||||||
|
"github.com/mum4k/termdash/canvas/braille"
|
||||||
|
"github.com/mum4k/termdash/draw/segdisp/segment"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MustHV draws the segment or panics.
|
||||||
|
func MustHV(bc *braille.Canvas, ar image.Rectangle, st segment.Type, opts ...segment.Option) {
|
||||||
|
if err := segment.HV(bc, ar, st, opts...); err != nil {
|
||||||
|
panic(fmt.Sprintf("segment.HV => unexpected error: %v", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustDiagonal draws the segment or panics.
|
||||||
|
func MustDiagonal(bc *braille.Canvas, ar image.Rectangle, width int, dt segment.DiagonalType, opts ...segment.DiagonalOption) {
|
||||||
|
if err := segment.Diagonal(bc, ar, width, dt, opts...); err != nil {
|
||||||
|
panic(fmt.Sprintf("segment.Diagonal => unexpected error: %v", err))
|
||||||
|
}
|
||||||
|
}
|
|
@ -82,7 +82,7 @@ var segmentNames = map[Segment]string{
|
||||||
K: "K",
|
K: "K",
|
||||||
L: "L",
|
L: "L",
|
||||||
M: "M",
|
M: "M",
|
||||||
N: "M",
|
N: "N",
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -254,7 +254,6 @@ func (d *Display) Draw(cvs *canvas.Canvas, opts ...Option) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
bcAr := area.WithRatio(bc.Area(), image.Point{3, 5})
|
bcAr := area.WithRatio(bc.Area(), image.Point{3, 5})
|
||||||
log.Printf("XXX before:%v after:%v", bc.Area(), bcAr)
|
|
||||||
segW := segWidth(bcAr)
|
segW := segWidth(bcAr)
|
||||||
if segW == 4 {
|
if segW == 4 {
|
||||||
segW = 5
|
segW = 5
|
||||||
|
@ -310,7 +309,7 @@ func (d *Display) Draw(cvs *canvas.Canvas, opts ...Option) error {
|
||||||
|
|
||||||
for _, segArg := range []struct {
|
for _, segArg := range []struct {
|
||||||
s Segment
|
s Segment
|
||||||
st segment.SegmentType
|
st segment.Type
|
||||||
ar image.Rectangle
|
ar image.Rectangle
|
||||||
opts []segment.Option
|
opts []segment.Option
|
||||||
}{
|
}{
|
||||||
|
@ -334,6 +333,7 @@ func (d *Display) Draw(cvs *canvas.Canvas, opts ...Option) error {
|
||||||
if !d.segments[segArg.s] {
|
if !d.segments[segArg.s] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
log.Printf("segment.HV for %v, ar:%v", segArg.s, segArg.ar)
|
||||||
if err := segment.HV(bc, segArg.ar, segArg.st, segArg.opts...); err != nil {
|
if err := segment.HV(bc, segArg.ar, segArg.st, segArg.opts...); err != nil {
|
||||||
return fmt.Errorf("failed to draw segment %v, segment.HV => %v", segArg.s, err)
|
return fmt.Errorf("failed to draw segment %v, segment.HV => %v", segArg.s, err)
|
||||||
}
|
}
|
||||||
|
@ -376,6 +376,7 @@ func (d *Display) Draw(cvs *canvas.Canvas, opts ...Option) error {
|
||||||
if !d.segments[segArg.s] {
|
if !d.segments[segArg.s] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
log.Printf("segment.Diagonal for %v, ar:%v", segArg.s, segArg.ar)
|
||||||
if err := segment.Diagonal(bc, segArg.ar, segW, segArg.dt); err != nil {
|
if err := segment.Diagonal(bc, segArg.ar, segW, segArg.dt); err != nil {
|
||||||
return fmt.Errorf("failed to draw segment %v, segment.Diagonal => %v", segArg.s, err)
|
return fmt.Errorf("failed to draw segment %v, segment.Diagonal => %v", segArg.s, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,9 @@ import (
|
||||||
|
|
||||||
"github.com/mum4k/termdash/area"
|
"github.com/mum4k/termdash/area"
|
||||||
"github.com/mum4k/termdash/canvas"
|
"github.com/mum4k/termdash/canvas"
|
||||||
|
"github.com/mum4k/termdash/canvas/braille/testbraille"
|
||||||
|
"github.com/mum4k/termdash/draw/segdisp/segment"
|
||||||
|
"github.com/mum4k/termdash/draw/segdisp/segment/testsegment"
|
||||||
"github.com/mum4k/termdash/terminal/faketerm"
|
"github.com/mum4k/termdash/terminal/faketerm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,40 +38,12 @@ func TestDraw(t *testing.T) {
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "smallest display 6x5, all segments",
|
desc: "empty when no segments set",
|
||||||
cellCanvas: image.Rect(0, 0, 6, 5),
|
cellCanvas: image.Rect(0, 0, 6, 5),
|
||||||
/*
|
|
||||||
update: func(d *Display) error {
|
|
||||||
for _, s := range AllSegments() {
|
|
||||||
if err := d.SetSegment(s); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},*/
|
|
||||||
update: func(d *Display) error {
|
|
||||||
return d.SetCharacter('w')
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "8x6, all segments",
|
desc: "smallest valid display 6x5, all segments",
|
||||||
cellCanvas: image.Rect(0, 0, 8, 6),
|
cellCanvas: image.Rect(0, 0, 6, 5),
|
||||||
/*
|
|
||||||
update: func(d *Display) error {
|
|
||||||
for _, s := range AllSegments() {
|
|
||||||
if err := d.SetSegment(s); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},*/
|
|
||||||
update: func(d *Display) error {
|
|
||||||
return d.SetCharacter('W')
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "16x12, all segments",
|
|
||||||
cellCanvas: image.Rect(0, 0, 16, 12),
|
|
||||||
update: func(d *Display) error {
|
update: func(d *Display) error {
|
||||||
for _, s := range AllSegments() {
|
for _, s := range AllSegments() {
|
||||||
if err := d.SetSegment(s); err != nil {
|
if err := d.SetSegment(s); err != nil {
|
||||||
|
@ -77,60 +52,33 @@ func TestDraw(t *testing.T) {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
/*
|
want: func(size image.Point) *faketerm.Terminal {
|
||||||
update: func(d *Display) error {
|
ft := faketerm.MustNew(size)
|
||||||
return d.SetCharacter('W')
|
bc := testbraille.MustNew(ft.Area())
|
||||||
},*/
|
|
||||||
},
|
testsegment.MustHV(bc, image.Rect(1, 0, 4, 1), segment.Horizontal) // A1
|
||||||
{
|
testsegment.MustHV(bc, image.Rect(5, 0, 8, 1), segment.Horizontal) // A2
|
||||||
desc: "32x24, all segments",
|
|
||||||
cellCanvas: image.Rect(0, 0, 32, 24),
|
testsegment.MustHV(bc, image.Rect(0, 1, 1, 8), segment.Vertical) // F
|
||||||
/*
|
testsegment.MustHV(bc, image.Rect(4, 1, 5, 8), segment.Vertical) // J
|
||||||
update: func(d *Display) error {
|
testsegment.MustHV(bc, image.Rect(8, 1, 9, 8), segment.Vertical) // B
|
||||||
for _, s := range AllSegments() {
|
|
||||||
if err := d.SetSegment(s); err != nil {
|
testsegment.MustHV(bc, image.Rect(1, 8, 4, 9), segment.Horizontal) // G1
|
||||||
return err
|
testsegment.MustHV(bc, image.Rect(5, 8, 8, 9), segment.Horizontal) // G2
|
||||||
}
|
|
||||||
}
|
testsegment.MustHV(bc, image.Rect(0, 9, 1, 16), segment.Vertical) // E
|
||||||
return nil
|
testsegment.MustHV(bc, image.Rect(4, 9, 5, 16), segment.Vertical) // M
|
||||||
},*/
|
testsegment.MustHV(bc, image.Rect(8, 9, 9, 16), segment.Vertical) // C
|
||||||
update: func(d *Display) error {
|
|
||||||
return d.SetCharacter('W')
|
testsegment.MustHV(bc, image.Rect(1, 16, 4, 17), segment.Horizontal) // D1
|
||||||
},
|
testsegment.MustHV(bc, image.Rect(5, 16, 8, 17), segment.Horizontal) // D2
|
||||||
},
|
|
||||||
{
|
testsegment.MustDiagonal(bc, image.Rect(1, 1, 4, 8), 1, segment.LeftToRight) // H
|
||||||
desc: "64x48, all segments",
|
testsegment.MustDiagonal(bc, image.Rect(5, 1, 8, 8), 1, segment.RightToLeft) // K
|
||||||
cellCanvas: image.Rect(0, 0, 64, 48),
|
testsegment.MustDiagonal(bc, image.Rect(1, 9, 4, 16), 1, segment.RightToLeft) // N
|
||||||
/*
|
testsegment.MustDiagonal(bc, image.Rect(5, 9, 8, 16), 1, segment.LeftToRight) // L
|
||||||
update: func(d *Display) error {
|
testbraille.MustApply(bc, ft)
|
||||||
for _, s := range AllSegments() {
|
return ft
|
||||||
if err := d.SetSegment(s); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},*/
|
|
||||||
update: func(d *Display) error {
|
|
||||||
return d.SetCharacter('W')
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "80x64, all segments",
|
|
||||||
cellCanvas: image.Rect(0, 0, 80, 64),
|
|
||||||
update: func(d *Display) error {
|
|
||||||
for _, s := range AllSegments() {
|
|
||||||
if err := d.SetSegment(s); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "80x64, W",
|
|
||||||
cellCanvas: image.Rect(0, 0, 80, 64),
|
|
||||||
update: func(d *Display) error {
|
|
||||||
return d.SetCharacter('W')
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue