diff --git a/container/container.go b/container/container.go index 8902f5a..178d533 100644 --- a/container/container.go +++ b/container/container.go @@ -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 } } diff --git a/container/container_test.go b/container/container_test.go index 962ae31..2d266da 100644 --- a/container/container_test.go +++ b/container/container_test.go @@ -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. } diff --git a/container/options.go b/container/options.go index 6f06703..c665e99 100644 --- a/container/options.go +++ b/container/options.go @@ -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 diff --git a/experimental/boxes/boxes.go b/experimental/boxes/boxes.go index ef63749..a2f4e11 100644 --- a/experimental/boxes/boxes.go +++ b/experimental/boxes/boxes.go @@ -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), ), ), ), diff --git a/terminal/faketerm/diff.go b/terminal/faketerm/diff.go index 01ff3c8..8aa70ac 100644 --- a/terminal/faketerm/diff.go +++ b/terminal/faketerm/diff.go @@ -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() }