mirror of
https://github.com/mum4k/termdash.git
synced 2025-04-25 13:48:50 +08:00
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:
parent
cec3153dc6
commit
28f6bfdb3e
@ -14,6 +14,7 @@ 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/cell"
|
||||||
"github.com/mum4k/termdash/draw"
|
"github.com/mum4k/termdash/draw"
|
||||||
"github.com/mum4k/termdash/terminalapi"
|
"github.com/mum4k/termdash/terminalapi"
|
||||||
)
|
)
|
||||||
@ -126,7 +127,7 @@ func (c *Container) draw() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,6 +277,29 @@ func TestDraw(t *testing.T) {
|
|||||||
return ft
|
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.
|
// TODO(mum4k): Tests where widget removes children and vice versa.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ package container
|
|||||||
// options.go defines container options.
|
// options.go defines container options.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/mum4k/termdash/cell"
|
||||||
"github.com/mum4k/termdash/draw"
|
"github.com/mum4k/termdash/draw"
|
||||||
"github.com/mum4k/termdash/widget"
|
"github.com/mum4k/termdash/widget"
|
||||||
)
|
)
|
||||||
@ -36,6 +37,8 @@ type options struct {
|
|||||||
|
|
||||||
// border is the border around the container.
|
// border is the border around the container.
|
||||||
border draw.LineStyle
|
border draw.LineStyle
|
||||||
|
// borderColor is the color used for the border.
|
||||||
|
borderColor cell.Color
|
||||||
}
|
}
|
||||||
|
|
||||||
// option implements Option.
|
// 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.
|
// splitType identifies how a container is split.
|
||||||
type splitType int
|
type splitType int
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mum4k/termdash/cell"
|
||||||
"github.com/mum4k/termdash/container"
|
"github.com/mum4k/termdash/container"
|
||||||
"github.com/mum4k/termdash/draw"
|
"github.com/mum4k/termdash/draw"
|
||||||
"github.com/mum4k/termdash/terminal/termbox"
|
"github.com/mum4k/termdash/terminal/termbox"
|
||||||
@ -54,6 +55,7 @@ func main() {
|
|||||||
),
|
),
|
||||||
container.Bottom(
|
container.Bottom(
|
||||||
container.Border(draw.LineStyleLight),
|
container.Border(draw.LineStyleLight),
|
||||||
|
container.BorderColor(cell.ColorYellow),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -1,11 +1,25 @@
|
|||||||
package faketerm
|
package faketerm
|
||||||
|
|
||||||
|
// diff.go provides functions that highlight differences between fake terminals.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"image"
|
||||||
"reflect"
|
"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
|
// Diff compares the two terminals, returning an empty string if there is not
|
||||||
// difference. If a difference is found, returns a human readable description
|
// 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(got.String())
|
||||||
b.WriteString(" want:\n")
|
b.WriteString(" want:\n")
|
||||||
b.WriteString(want.String())
|
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()
|
size := got.Size()
|
||||||
|
var optDiffs []*optDiff
|
||||||
for row := 0; row < size.Y; row++ {
|
for row := 0; row < size.Y; row++ {
|
||||||
for col := 0; col < size.X; col++ {
|
for col := 0; col < size.X; col++ {
|
||||||
r := got.BackBuffer()[col][row].Rune
|
gotCell := got.BackBuffer()[col][row]
|
||||||
if r != want.BackBuffer()[col][row].Rune {
|
wantCell := want.BackBuffer()[col][row]
|
||||||
|
r := gotCell.Rune
|
||||||
|
if r != wantCell.Rune {
|
||||||
r = '࿃'
|
r = '࿃'
|
||||||
} else if r == 0 {
|
} else if r == 0 {
|
||||||
r = ' '
|
r = ' '
|
||||||
}
|
}
|
||||||
b.WriteRune(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')
|
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()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user