Adding option to draw container borders in color.

And updating the faketerm diff function to also display differences in
options.
This commit is contained in:
Jakub Sobon 2018-04-05 19:57:46 +02:00
parent cec3153dc6
commit 28f6bfdb3e
No known key found for this signature in database
GPG Key ID: F2451A77FB05D3B7
5 changed files with 76 additions and 5 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/mum4k/termdash/area"
"github.com/mum4k/termdash/canvas"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/draw"
"github.com/mum4k/termdash/terminalapi"
)
@ -126,7 +127,7 @@ func (c *Container) draw() error {
if err != nil {
return err
}
if err := draw.Box(cvs, ar, c.opts.border); err != nil {
if err := draw.Box(cvs, ar, c.opts.border, cell.FgColor(c.opts.borderColor)); err != nil {
return err
}
}

View File

@ -277,6 +277,29 @@ func TestDraw(t *testing.T) {
return ft
},
},
{
desc: "sets border color",
termSize: image.Point{4, 4},
container: func(ft *faketerm.Terminal) *Container {
return New(
ft,
Border(draw.LineStyleLight),
BorderColor(cell.ColorRed),
)
},
want: func(size image.Point) *faketerm.Terminal {
ft := faketerm.MustNew(size)
cvs := mustCanvas(image.Rect(0, 0, 4, 4))
mustBox(
cvs,
image.Rect(0, 0, 4, 4),
draw.LineStyleLight,
cell.FgColor(cell.ColorRed),
)
mustApply(cvs, ft)
return ft
},
},
// TODO(mum4k): Tests where widget removes children and vice versa.
}

View File

@ -3,6 +3,7 @@ package container
// options.go defines container options.
import (
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/draw"
"github.com/mum4k/termdash/widget"
)
@ -36,6 +37,8 @@ type options struct {
// border is the border around the container.
border draw.LineStyle
// borderColor is the color used for the border.
borderColor cell.Color
}
// option implements Option.
@ -142,6 +145,13 @@ func Border(ls draw.LineStyle) Option {
})
}
// BorderColor sets the color of the border.
func BorderColor(color cell.Color) Option {
return option(func(c *Container) {
c.opts.borderColor = color
})
}
// splitType identifies how a container is split.
type splitType int

View File

@ -7,6 +7,7 @@ import (
"log"
"time"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/container"
"github.com/mum4k/termdash/draw"
"github.com/mum4k/termdash/terminal/termbox"
@ -54,6 +55,7 @@ func main() {
),
container.Bottom(
container.Border(draw.LineStyleLight),
container.BorderColor(cell.ColorYellow),
),
),
),

View File

@ -1,11 +1,25 @@
package faketerm
// diff.go provides functions that highlight differences between fake terminals.
import (
"bytes"
"fmt"
"image"
"reflect"
"github.com/kylelemons/godebug/pretty"
"github.com/mum4k/termdash/cell"
)
// diff.go provides functions that highlight differences between fake terminals.
// optDiff is used to display differences in cell options.
type optDiff struct {
// point indicates the cell with the differing options.
point image.Point
got *cell.Options
want *cell.Options
}
// Diff compares the two terminals, returning an empty string if there is not
// difference. If a difference is found, returns a human readable description
@ -21,20 +35,41 @@ func Diff(want, got *Terminal) string {
b.WriteString(got.String())
b.WriteString(" want:\n")
b.WriteString(want.String())
b.WriteString(" diff (unexpected cells highlighted with rune '࿃'):\n")
b.WriteString(" diff (unexpected cells highlighted with rune '࿃')\n")
b.WriteString(" note - this excludes cell options:\n")
size := got.Size()
var optDiffs []*optDiff
for row := 0; row < size.Y; row++ {
for col := 0; col < size.X; col++ {
r := got.BackBuffer()[col][row].Rune
if r != want.BackBuffer()[col][row].Rune {
gotCell := got.BackBuffer()[col][row]
wantCell := want.BackBuffer()[col][row]
r := gotCell.Rune
if r != wantCell.Rune {
r = '࿃'
} else if r == 0 {
r = ' '
}
b.WriteRune(r)
if !reflect.DeepEqual(gotCell.Opts, wantCell.Opts) {
optDiffs = append(optDiffs, &optDiff{
point: image.Point{col, row},
got: gotCell.Opts,
want: wantCell.Opts,
})
}
}
b.WriteRune('\n')
}
if len(optDiffs) > 0 {
b.WriteString(" Found differences in options on some of the cells:\n")
for _, od := range optDiffs {
if diff := pretty.Compare(od.want, od.got); diff != "" {
b.WriteString(fmt.Sprintf("cell %v, diff (-want +got):\n%s\n", od.point, diff))
}
}
}
return b.String()
}