1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-28 13:48:51 +08:00

Merge pull request #126 from mum4k/constructor-error

Constructors of all widgets now return an error.
This commit is contained in:
Jakub Sobon 2019-02-15 00:35:38 -05:00 committed by GitHub
commit d4b68e905b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 498 additions and 225 deletions

View File

@ -19,8 +19,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Termbox is now initialized in 256 color mode by default.
- Generalized mouse button FSM for use in widgets that need to track mouse
button clicks.
- The constructor of the LineChart widget now also returns an error so that it
can validate its options. This is a breaking change on the LineChart API.
#### Breaking API changes
- The constructors of all the widgets now also return an error so that they
can validate the options. This is a breaking change for the following
widgets: BarChart, Gauge, LineChart, SparkLine, Text. The callers will have
to handle the returned error.
### Fixed

View File

@ -48,7 +48,14 @@ func layout(ctx context.Context, t terminalapi.Terminal) (*container.Container,
if err != nil {
return nil, err
}
spGreen, spRed := newSparkLines(ctx)
rollT, err := newRollText(ctx)
if err != nil {
return nil, err
}
spGreen, spRed, err := newSparkLines(ctx)
if err != nil {
return nil, err
}
segmentTextSpark := []container.Option{
container.SplitHorizontal(
container.Top(
@ -61,7 +68,7 @@ func layout(ctx context.Context, t terminalapi.Terminal) (*container.Container,
container.Left(
container.Border(draw.LineStyleLight),
container.BorderTitle("A rolling text"),
container.PlaceWidget(newRollText(ctx)),
container.PlaceWidget(rollT),
),
container.Right(
container.Border(draw.LineStyleLight),
@ -77,6 +84,11 @@ func layout(ctx context.Context, t terminalapi.Terminal) (*container.Container,
),
}
g, err := newGauge(ctx)
if err != nil {
return nil, err
}
heartLC, err := newHeartbeat(ctx)
if err != nil {
return nil, err
@ -87,7 +99,7 @@ func layout(ctx context.Context, t terminalapi.Terminal) (*container.Container,
container.Border(draw.LineStyleLight),
container.BorderTitle("A Gauge"),
container.BorderColor(cell.ColorNumber(39)),
container.PlaceWidget(newGauge(ctx)),
container.PlaceWidget(g),
),
container.Bottom(
container.Border(draw.LineStyleLight),
@ -106,6 +118,11 @@ func layout(ctx context.Context, t terminalapi.Terminal) (*container.Container,
),
}
bc, err := newBarChart(ctx)
if err != nil {
return nil, err
}
don, err := newDonut(ctx)
if err != nil {
return nil, err
@ -120,7 +137,7 @@ func layout(ctx context.Context, t terminalapi.Terminal) (*container.Container,
container.Top(
container.Border(draw.LineStyleLight),
container.BorderTitle("BarChart"),
container.PlaceWidget(newBarChart(ctx)),
container.PlaceWidget(bc),
container.BorderTitleAlignRight(),
),
container.Bottom(
@ -240,8 +257,11 @@ func newSegmentDisplay(ctx context.Context) (*segmentdisplay.SegmentDisplay, err
}
// newRollText creates a new Text widget that displays rolling text.
func newRollText(ctx context.Context) *text.Text {
t := text.New(text.RollContent())
func newRollText(ctx context.Context) (*text.Text, error) {
t, err := text.New(text.RollContent())
if err != nil {
return nil, err
}
i := 0
go periodic(ctx, 1*time.Second, func() error {
@ -251,15 +271,18 @@ func newRollText(ctx context.Context) *text.Text {
i++
return nil
})
return t
return t, nil
}
// newSparkLines creates two new sparklines displaying random values.
func newSparkLines(ctx context.Context) (*sparkline.SparkLine, *sparkline.SparkLine) {
spGreen := sparkline.New(
func newSparkLines(ctx context.Context) (*sparkline.SparkLine, *sparkline.SparkLine, error) {
spGreen, err := sparkline.New(
sparkline.Label("Green SparkLine", cell.FgColor(cell.ColorBlue)),
sparkline.Color(cell.ColorGreen),
)
if err != nil {
return nil, nil, err
}
const max = 100
go periodic(ctx, 250*time.Millisecond, func() error {
@ -267,21 +290,27 @@ func newSparkLines(ctx context.Context) (*sparkline.SparkLine, *sparkline.SparkL
return spGreen.Add([]int{v})
})
spRed := sparkline.New(
spRed, err := sparkline.New(
sparkline.Label("Red SparkLine", cell.FgColor(cell.ColorBlue)),
sparkline.Color(cell.ColorRed),
)
if err != nil {
return nil, nil, err
}
go periodic(ctx, 500*time.Millisecond, func() error {
v := int(rand.Int31n(max + 1))
return spRed.Add([]int{v})
})
return spGreen, spRed
return spGreen, spRed, nil
}
// newGauge creates a demo Gauge widget.
func newGauge(ctx context.Context) *gauge.Gauge {
g := gauge.New()
func newGauge(ctx context.Context) (*gauge.Gauge, error) {
g, err := gauge.New()
if err != nil {
return nil, err
}
const start = 35
progress := start
@ -296,7 +325,7 @@ func newGauge(ctx context.Context) *gauge.Gauge {
}
return nil
})
return g
return g, nil
}
// newDonut creates a demo Donut widget.
@ -354,8 +383,8 @@ func newHeartbeat(ctx context.Context) (*linechart.LineChart, error) {
}
// newBarChart returns a BarcChart that displays random values on multiple bars.
func newBarChart(ctx context.Context) *barchart.BarChart {
bc := barchart.New(
func newBarChart(ctx context.Context) (*barchart.BarChart, error) {
bc, err := barchart.New(
barchart.BarColors([]cell.Color{
cell.ColorNumber(33),
cell.ColorNumber(39),
@ -374,6 +403,9 @@ func newBarChart(ctx context.Context) *barchart.BarChart {
}),
barchart.ShowValues(),
)
if err != nil {
return nil, err
}
const (
bars = 6
@ -387,7 +419,7 @@ func newBarChart(ctx context.Context) *barchart.BarChart {
return bc.Values(values, max)
})
return bc
return bc, nil
}
// newSines returns a line chart that displays multiple sine series.

View File

@ -53,14 +53,17 @@ type BarChart struct {
}
// New returns a new BarChart.
func New(opts ...Option) *BarChart {
func New(opts ...Option) (*BarChart, error) {
opt := newOptions()
for _, o := range opts {
o.set(opt)
}
if err := opt.validate(); err != nil {
return nil, err
}
return &BarChart{
opts: opt,
}
}, nil
}
// Draw draws the BarChart widget onto the canvas.
@ -155,7 +158,7 @@ func (bc *BarChart) barWidth(cvs *canvas.Canvas) int {
}
if bc.opts.barWidth >= 1 {
// Prefer width set via the options if it is positive.
// Prefer width set via the options.
return bc.opts.barWidth
}

View File

@ -28,21 +28,50 @@ import (
"github.com/mum4k/termdash/widgetapi"
)
func TestGauge(t *testing.T) {
func TestBarChart(t *testing.T) {
tests := []struct {
desc string
bc *BarChart
opts []Option
update func(*BarChart) error // update gets called before drawing of the widget.
canvas image.Rectangle
want func(size image.Point) *faketerm.Terminal
wantErr bool
wantUpdateErr bool // whether to expect an error on a call to the update function
wantDrawErr bool
}{
{
desc: "fails on negative bar width",
opts: []Option{
BarWidth(-1),
},
update: func(bc *BarChart) error {
return nil
},
canvas: image.Rect(0, 0, 3, 10),
want: func(size image.Point) *faketerm.Terminal {
return faketerm.MustNew(size)
},
wantErr: true,
},
{
desc: "fails on negative bar gap",
opts: []Option{
BarGap(-1),
},
update: func(bc *BarChart) error {
return nil
},
canvas: image.Rect(0, 0, 3, 10),
want: func(size image.Point) *faketerm.Terminal {
return faketerm.MustNew(size)
},
wantErr: true,
},
{
desc: "draws empty for no values",
bc: New(
opts: []Option{
Char('o'),
),
},
update: func(bc *BarChart) error {
return nil
},
@ -53,9 +82,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "fails for zero max",
bc: New(
opts: []Option{
Char('o'),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{0, 2, 5, 10}, 0)
},
@ -67,9 +96,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "fails for negative max",
bc: New(
opts: []Option{
Char('o'),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{0, 2, 5, 10}, -1)
},
@ -81,9 +110,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "fails when negative value",
bc: New(
opts: []Option{
Char('o'),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{0, -2, 5, 10}, 10)
},
@ -95,9 +124,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "fails for value larger than max",
bc: New(
opts: []Option{
Char('o'),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{0, 2, 5, 11}, 10)
},
@ -109,9 +138,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "draws resize needed character when canvas is smaller than requested",
bc: New(
opts: []Option{
Char('o'),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{0, 2, 5, 10}, 10)
},
@ -127,9 +156,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "displays bars",
bc: New(
opts: []Option{
Char('o'),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{0, 2, 5, 10}, 10)
},
@ -156,14 +185,14 @@ func TestGauge(t *testing.T) {
},
{
desc: "displays bars with labels",
bc: New(
opts: []Option{
Char('o'),
Labels([]string{
"1",
"2",
"3",
}),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{1, 2, 5, 10}, 10)
},
@ -205,14 +234,14 @@ func TestGauge(t *testing.T) {
},
{
desc: "trims too long labels",
bc: New(
opts: []Option{
Char('o'),
Labels([]string{
"1",
"22",
"3",
}),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{1, 2, 5, 10}, 10)
},
@ -254,7 +283,7 @@ func TestGauge(t *testing.T) {
},
{
desc: "displays bars with labels and values",
bc: New(
opts: []Option{
Char('o'),
Labels([]string{
"1",
@ -262,7 +291,7 @@ func TestGauge(t *testing.T) {
"3",
}),
ShowValues(),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{1, 2, 5, 10}, 10)
},
@ -320,9 +349,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "bars take as much width as available",
bc: New(
opts: []Option{
Char('o'),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{1, 2}, 10)
},
@ -345,10 +374,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "respects set bar width",
bc: New(
opts: []Option{
Char('o'),
BarWidth(1),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{1, 2}, 10)
},
@ -371,7 +400,6 @@ func TestGauge(t *testing.T) {
},
{
desc: "options can be set on a call to Values",
bc: New(),
update: func(bc *BarChart) error {
return bc.Values([]int{1, 2}, 10, Char('o'), BarWidth(1))
},
@ -394,10 +422,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "respects set bar gap",
bc: New(
opts: []Option{
Char('o'),
BarGap(2),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{1, 2}, 10)
},
@ -420,11 +448,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "respects both width and gap",
bc: New(
opts: []Option{
Char('o'),
BarGap(2),
BarWidth(2),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{5, 3}, 10)
},
@ -447,7 +475,7 @@ func TestGauge(t *testing.T) {
},
{
desc: "respects bar and label colors",
bc: New(
opts: []Option{
Char('o'),
BarColors([]cell.Color{
cell.ColorBlue,
@ -461,7 +489,7 @@ func TestGauge(t *testing.T) {
"1",
"2",
}),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{1, 2, 3}, 10)
},
@ -496,14 +524,14 @@ func TestGauge(t *testing.T) {
},
{
desc: "respects value colors",
bc: New(
opts: []Option{
Char('o'),
ValueColors([]cell.Color{
cell.ColorBlue,
cell.ColorBlack,
}),
ShowValues(),
),
},
update: func(bc *BarChart) error {
return bc.Values([]int{0, 2, 3}, 10)
},
@ -541,12 +569,20 @@ func TestGauge(t *testing.T) {
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
bc, err := New(tc.opts...)
if (err != nil) != tc.wantErr {
t.Errorf("New => unexpected error: %v, wantErr: %v", err, tc.wantErr)
}
if err != nil {
return
}
c, err := canvas.New(tc.canvas)
if err != nil {
t.Fatalf("canvas.New => unexpected error: %v", err)
}
err = tc.update(tc.bc)
err = tc.update(bc)
if (err != nil) != tc.wantUpdateErr {
t.Errorf("update => unexpected error: %v, wantUpdateErr: %v", err, tc.wantUpdateErr)
@ -555,7 +591,7 @@ func TestGauge(t *testing.T) {
return
}
err = tc.bc.Draw(c)
err = bc.Draw(c)
if (err != nil) != tc.wantDrawErr {
t.Errorf("Draw => unexpected error: %v, wantDrawErr: %v", err, tc.wantDrawErr)
}
@ -588,7 +624,7 @@ func TestOptions(t *testing.T) {
{
desc: "minimum size for no bars",
create: func() (*BarChart, error) {
return New(), nil
return New()
},
want: widgetapi.Options{
MinimumSize: image.Point{1, 1},
@ -601,7 +637,7 @@ func TestOptions(t *testing.T) {
create: func() (*BarChart, error) {
return New(
Labels([]string{"foo"}),
), nil
)
},
want: widgetapi.Options{
MinimumSize: image.Point{1, 1},
@ -612,7 +648,10 @@ func TestOptions(t *testing.T) {
{
desc: "minimum size for one bar, default width, gap and no labels",
create: func() (*BarChart, error) {
bc := New()
bc, err := New()
if err != nil {
return nil, err
}
if err := bc.Values([]int{1}, 3); err != nil {
return nil, err
}
@ -627,7 +666,10 @@ func TestOptions(t *testing.T) {
{
desc: "minimum size for two bars, default width, gap and no labels",
create: func() (*BarChart, error) {
bc := New()
bc, err := New()
if err != nil {
return nil, err
}
if err := bc.Values([]int{1, 2}, 3); err != nil {
return nil, err
}
@ -642,10 +684,13 @@ func TestOptions(t *testing.T) {
{
desc: "minimum size for two bars, custom width, gap and no labels",
create: func() (*BarChart, error) {
bc := New(
bc, err := New(
BarWidth(3),
BarGap(2),
)
if err != nil {
return nil, err
}
if err := bc.Values([]int{1, 2}, 3); err != nil {
return nil, err
}
@ -660,10 +705,13 @@ func TestOptions(t *testing.T) {
{
desc: "minimum size for two bars, custom width, gap and labels",
create: func() (*BarChart, error) {
bc := New(
bc, err := New(
BarWidth(3),
BarGap(2),
)
if err != nil {
return nil, err
}
if err := bc.Values([]int{1, 2}, 3, Labels([]string{"foo", "bar"})); err != nil {
return nil, err
}

View File

@ -67,7 +67,7 @@ func main() {
defer t.Close()
ctx, cancel := context.WithCancel(context.Background())
bc := barchart.New(
bc, err := barchart.New(
barchart.BarColors([]cell.Color{
cell.ColorBlue,
cell.ColorRed,
@ -91,6 +91,9 @@ func main() {
"CPU3",
}),
)
if err != nil {
panic(err)
}
go playBarChart(ctx, bc, 1*time.Second)
c, err := container.New(

View File

@ -17,6 +17,8 @@ package barchart
// options.go contains configurable options for BarChart.
import (
"fmt"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/draw"
)
@ -47,6 +49,17 @@ type options struct {
labels []string
}
// validate validates the provided options.
func (o *options) validate() error {
if got, min := o.barWidth, 0; got < min {
return fmt.Errorf("invalid BarWidth %d, must be %d <= BarWidth", got, min)
}
if got, min := o.barGap, 0; got < min {
return fmt.Errorf("invalid BarGap %d, must be %d <= BarGap", got, min)
}
return nil
}
// newOptions returns options with the default values set.
func newOptions() *options {
return &options{
@ -66,8 +79,9 @@ func Char(ch rune) Option {
})
}
// BarWidth sets the width of the bars. If not set, the bars use all the space
// available to the widget.
// BarWidth sets the width of the bars. If not set, or set to zero, the bars
// use all the space available to the widget. Must be a positive or zero
// integer.
func BarWidth(width int) Option {
return option(func(opts *options) {
opts.barWidth = width
@ -78,6 +92,7 @@ func BarWidth(width int) Option {
const DefaultBarGap = 1
// BarGap sets the width of the space between the bars.
// Must be a positive or zero integer.
// Defaults to DefaultBarGap.
func BarGap(width int) Option {
return option(func(opts *options) {

View File

@ -77,14 +77,18 @@ type Gauge struct {
}
// New returns a new Gauge.
func New(opts ...Option) *Gauge {
func New(opts ...Option) (*Gauge, error) {
opt := newOptions()
for _, o := range opts {
o.set(opt)
}
if err := opt.validate(); err != nil {
return nil, err
}
return &Gauge{
opts: opt,
}
}, nil
}
// Absolute sets the progress in absolute numbers, i.e. 7 out of 10.

View File

@ -45,20 +45,31 @@ type absoluteCall struct {
func TestGauge(t *testing.T) {
tests := []struct {
desc string
gauge *Gauge
opts []Option
percent *percentCall // if set, the test case calls Gauge.Percent().
absolute *absoluteCall // if set the test case calls Gauge.Absolute().
canvas image.Rectangle
opts []Option
want func(size image.Point) *faketerm.Terminal
wantErr bool
wantUpdateErr bool // whether to expect an error on a call to Gauge.Percent() or Gauge.Absolute().
wantDrawErr bool
}{
{
desc: "fails on negative height",
opts: []Option{
Height(-1),
},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
return faketerm.MustNew(size)
},
wantErr: true,
},
{
desc: "gauge showing percentage",
gauge: New(
opts: []Option{
Char('o'),
),
},
percent: &percentCall{p: 35},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -76,10 +87,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "draws resize needed character when canvas is smaller than requested",
gauge: New(
opts: []Option{
Char('o'),
Border(draw.LineStyleLight),
),
},
percent: &percentCall{p: 35},
canvas: image.Rect(0, 0, 1, 1),
want: func(size image.Point) *faketerm.Terminal {
@ -93,11 +104,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "aligns the progress text top and left",
gauge: New(
opts: []Option{
Char('o'),
HorizontalTextAlign(align.HorizontalLeft),
VerticalTextAlign(align.VerticalTop),
),
},
percent: &percentCall{p: 0},
canvas: image.Rect(0, 0, 10, 4),
want: func(size image.Point) *faketerm.Terminal {
@ -111,12 +122,12 @@ func TestGauge(t *testing.T) {
},
{
desc: "aligns the progress text top and left with border",
gauge: New(
opts: []Option{
Char('o'),
HorizontalTextAlign(align.HorizontalLeft),
VerticalTextAlign(align.VerticalTop),
Border(draw.LineStyleLight),
),
},
percent: &percentCall{p: 0},
canvas: image.Rect(0, 0, 10, 4),
want: func(size image.Point) *faketerm.Terminal {
@ -131,11 +142,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "aligns the progress text bottom and right",
gauge: New(
opts: []Option{
Char('o'),
HorizontalTextAlign(align.HorizontalRight),
VerticalTextAlign(align.VerticalBottom),
),
},
percent: &percentCall{p: 0},
canvas: image.Rect(0, 0, 10, 4),
want: func(size image.Point) *faketerm.Terminal {
@ -149,12 +160,12 @@ func TestGauge(t *testing.T) {
},
{
desc: "aligns the progress text bottom and right with border",
gauge: New(
opts: []Option{
Char('o'),
HorizontalTextAlign(align.HorizontalRight),
VerticalTextAlign(align.VerticalBottom),
Border(draw.LineStyleLight),
),
},
percent: &percentCall{p: 0},
canvas: image.Rect(0, 0, 10, 4),
want: func(size image.Point) *faketerm.Terminal {
@ -169,11 +180,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge showing percentage with border",
gauge: New(
opts: []Option{
Char('o'),
Border(draw.LineStyleLight),
BorderTitle("title"),
),
},
percent: &percentCall{p: 35},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -194,12 +205,12 @@ func TestGauge(t *testing.T) {
},
{
desc: "respects border options",
gauge: New(
opts: []Option{
Char('o'),
Border(draw.LineStyleLight, cell.FgColor(cell.ColorBlue)),
BorderTitle("title"),
BorderTitleAlign(align.HorizontalRight),
),
},
percent: &percentCall{p: 35},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -222,9 +233,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge showing zero percentage",
gauge: New(
opts: []Option{
Char('o'),
),
},
percent: &percentCall{},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -238,9 +249,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge showing 100 percent",
gauge: New(
opts: []Option{
Char('o'),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -260,10 +271,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge showing 100 percent with border",
gauge: New(
opts: []Option{
Char('o'),
Border(draw.LineStyleLight),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -284,9 +295,9 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge showing absolute progress",
gauge: New(
opts: []Option{
Char('o'),
),
},
absolute: &absoluteCall{done: 20, total: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -304,10 +315,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge without text progress",
gauge: New(
opts: []Option{
Char('o'),
HideTextProgress(),
),
},
percent: &percentCall{p: 35},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -324,10 +335,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "passing option to Percent() overrides one provided to New()",
gauge: New(
opts: []Option{
Char('o'),
HideTextProgress(),
),
},
percent: &percentCall{p: 35, opts: []Option{ShowTextProgress()}},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -345,10 +356,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "passing option to Absolute() overrides one provided to New()",
gauge: New(
opts: []Option{
Char('o'),
HideTextProgress(),
),
},
absolute: &absoluteCall{done: 20, total: 100, opts: []Option{ShowTextProgress()}},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -366,10 +377,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge takes full size of the canvas",
gauge: New(
opts: []Option{
Char('o'),
HideTextProgress(),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 5, 2),
want: func(size image.Point) *faketerm.Terminal {
@ -386,11 +397,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge with text label, half-width runes",
gauge: New(
opts: []Option{
Char('o'),
HideTextProgress(),
TextLabel("label"),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -410,11 +421,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge with text label, full-width runes",
gauge: New(
opts: []Option{
Char('o'),
HideTextProgress(),
TextLabel("你好"),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -434,11 +445,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge with text label, full-width runes, gauge falls on rune boundary",
gauge: New(
opts: []Option{
Char('o'),
HideTextProgress(),
TextLabel("你好"),
),
},
percent: &percentCall{p: 50},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -461,11 +472,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge with text label, full-width runes, gauge extended to cover full rune",
gauge: New(
opts: []Option{
Char('o'),
HideTextProgress(),
TextLabel("你好"),
),
},
percent: &percentCall{p: 40},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -488,10 +499,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "gauge with progress text and text label",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("l"),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -511,12 +522,12 @@ func TestGauge(t *testing.T) {
},
{
desc: "text fully outside of gauge respects EmptyTextColor",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("l"),
EmptyTextColor(cell.ColorMagenta),
FilledTextColor(cell.ColorBlue),
),
},
percent: &percentCall{p: 10},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -536,12 +547,12 @@ func TestGauge(t *testing.T) {
},
{
desc: "text fully inside of gauge respects FilledTextColor",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("l"),
EmptyTextColor(cell.ColorMagenta),
FilledTextColor(cell.ColorBlue),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -561,12 +572,12 @@ func TestGauge(t *testing.T) {
},
{
desc: "part of the text is inside and part outside of gauge",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("l"),
EmptyTextColor(cell.ColorMagenta),
FilledTextColor(cell.ColorBlue),
),
},
percent: &percentCall{p: 50},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -589,10 +600,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "truncates text that is outside of gauge",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("long label"),
),
},
percent: &percentCall{p: 0},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -608,11 +619,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "truncates text that is outside of gauge when drawn with border",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("long label"),
Border(draw.LineStyleLight),
),
},
percent: &percentCall{p: 0},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -629,10 +640,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "truncates text that is inside of gauge",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("long label"),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -652,11 +663,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "truncates text that is inside of gauge when drawn with border",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("long label"),
Border(draw.LineStyleLight),
),
},
percent: &percentCall{p: 100},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -677,10 +688,10 @@ func TestGauge(t *testing.T) {
},
{
desc: "truncates text that is inside and outside of gauge",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("long label"),
),
},
percent: &percentCall{p: 50},
canvas: image.Rect(0, 0, 10, 3),
want: func(size image.Point) *faketerm.Terminal {
@ -703,11 +714,11 @@ func TestGauge(t *testing.T) {
},
{
desc: "truncates text that is inside and outside of gauge with border",
gauge: New(
opts: []Option{
Char('o'),
TextLabel("long label"),
Border(draw.LineStyleLight),
),
},
percent: &percentCall{p: 50},
canvas: image.Rect(0, 0, 10, 4),
want: func(size image.Point) *faketerm.Terminal {
@ -733,6 +744,14 @@ func TestGauge(t *testing.T) {
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
g, err := New(tc.opts...)
if (err != nil) != tc.wantErr {
t.Errorf("New => unexpected error: %v, wantErr: %v", err, tc.wantErr)
}
if err != nil {
return
}
c, err := canvas.New(tc.canvas)
if err != nil {
t.Fatalf("canvas.New => unexpected error: %v", err)
@ -740,7 +759,7 @@ func TestGauge(t *testing.T) {
switch {
case tc.percent != nil:
err := tc.gauge.Percent(tc.percent.p, tc.percent.opts...)
err := g.Percent(tc.percent.p, tc.percent.opts...)
if (err != nil) != tc.wantUpdateErr {
t.Errorf("Percent => unexpected error: %v, wantUpdateErr: %v", err, tc.wantUpdateErr)
}
@ -749,7 +768,7 @@ func TestGauge(t *testing.T) {
}
case tc.absolute != nil:
err := tc.gauge.Absolute(tc.absolute.done, tc.absolute.total, tc.absolute.opts...)
err := g.Absolute(tc.absolute.done, tc.absolute.total, tc.absolute.opts...)
if (err != nil) != tc.wantUpdateErr {
t.Errorf("Absolute => unexpected error: %v, wantUpdateErr: %v", err, tc.wantUpdateErr)
}
@ -759,7 +778,7 @@ func TestGauge(t *testing.T) {
}
err = tc.gauge.Draw(c)
err = g.Draw(c)
if (err != nil) != tc.wantDrawErr {
t.Errorf("Draw => unexpected error: %v, wantDrawErr: %v", err, tc.wantDrawErr)
}
@ -785,13 +804,12 @@ func TestGauge(t *testing.T) {
func TestOptions(t *testing.T) {
tests := []struct {
desc string
gauge *Gauge
want widgetapi.Options
desc string
opts []Option
want widgetapi.Options
}{
{
desc: "reports correct minimum and maximum size",
gauge: New(),
desc: "reports correct minimum and maximum size",
want: widgetapi.Options{
MaximumSize: image.Point{0, 0}, // Unlimited.
MinimumSize: image.Point{1, 1},
@ -801,9 +819,9 @@ func TestOptions(t *testing.T) {
},
{
desc: "maximum size is limited when height is specified",
gauge: New(
opts: []Option{
Height(2),
),
},
want: widgetapi.Options{
MaximumSize: image.Point{0, 2},
MinimumSize: image.Point{1, 1},
@ -813,10 +831,10 @@ func TestOptions(t *testing.T) {
},
{
desc: "border is accounted for in maximum and minimum size",
gauge: New(
opts: []Option{
Border(draw.LineStyleLight),
Height(2),
),
},
want: widgetapi.Options{
MaximumSize: image.Point{0, 4},
MinimumSize: image.Point{3, 3},
@ -828,7 +846,11 @@ func TestOptions(t *testing.T) {
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
got := tc.gauge.Options()
g, err := New(tc.opts...)
if err != nil {
t.Fatalf("New => unexpected error: %v", err)
}
got := g.Options()
if diff := pretty.Compare(tc.want, got); diff != "" {
t.Errorf("Options => unexpected diff (-want, +got):\n%s", diff)

View File

@ -86,33 +86,48 @@ func main() {
defer t.Close()
ctx, cancel := context.WithCancel(context.Background())
slim := gauge.New(
slim, err := gauge.New(
gauge.Height(1),
gauge.Border(draw.LineStyleLight),
gauge.BorderTitle("Percentage progress"),
)
if err != nil {
panic(err)
}
go playGauge(ctx, slim, 10, 500*time.Millisecond, playTypePercent)
absolute := gauge.New(
absolute, err := gauge.New(
gauge.Height(1),
gauge.Color(cell.ColorBlue),
gauge.Border(draw.LineStyleLight),
gauge.BorderTitle("Absolute progress"),
)
if err != nil {
panic(err)
}
go playGauge(ctx, absolute, 17, 500*time.Millisecond, playTypeAbsolute)
noProgress := gauge.New(
noProgress, err := gauge.New(
gauge.Height(1),
gauge.Border(draw.LineStyleLight, cell.FgColor(cell.ColorMagenta)),
gauge.BorderTitle("Without progress text"),
gauge.HideTextProgress(),
)
if err != nil {
panic(err)
}
go playGauge(ctx, noProgress, 5, 250*time.Millisecond, playTypePercent)
withLabel := gauge.New(
withLabel, err := gauge.New(
gauge.Height(3),
gauge.TextLabel("你好,世界! text label and no border"),
gauge.Color(cell.ColorRed),
gauge.FilledTextColor(cell.ColorBlack),
gauge.EmptyTextColor(cell.ColorYellow),
)
if err != nil {
panic(err)
}
go playGauge(ctx, withLabel, 3, 500*time.Millisecond, playTypePercent)
c, err := container.New(

View File

@ -17,6 +17,8 @@ package gauge
// options.go contains configurable options for Gauge.
import (
"fmt"
"github.com/mum4k/termdash/align"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/draw"
@ -58,6 +60,14 @@ func newOptions() *options {
}
}
// validate validates the provided options.
func (o *options) validate() error {
if got, min := o.height, 0; got < min {
return fmt.Errorf("invalid Height %d, must be %d <= Height", got, min)
}
return nil
}
// option implements Option.
type option func(*options)
@ -95,8 +105,8 @@ func HideTextProgress() Option {
})
}
// Height sets the height of the drawn Gauge.
// Defaults to the height of the container.
// Height sets the height of the drawn Gauge. Must be a positive number.
// Defaults to zero which means the height of the container.
func Height(height int) Option {
return option(func(opts *options) {
opts.height = height

View File

@ -16,7 +16,11 @@ package sparkline
// options.go contains configurable options for SparkLine.
import "github.com/mum4k/termdash/cell"
import (
"fmt"
"github.com/mum4k/termdash/cell"
)
// Option is used to provide options.
type Option interface {
@ -47,6 +51,14 @@ func newOptions() *options {
}
}
// validate validates the provided options.
func (o *options) validate() error {
if got, min := o.height, 0; got < min {
return fmt.Errorf("invalid Height %d, must be %d <= Height", got, min)
}
return nil
}
// Label adds a label above the SparkLine.
func Label(text string, cOpts ...cell.Option) Option {
return option(func(opts *options) {
@ -56,8 +68,8 @@ func Label(text string, cOpts ...cell.Option) Option {
}
// Height sets a fixed height for the SparkLine.
// If not provided, the SparkLine takes all the available vertical space in the
// container.
// If not provided or set to zero, the SparkLine takes all the available
// vertical space in the container. Must be a positive or zero integer.
func Height(h int) Option {
return option(func(opts *options) {
opts.height = h

View File

@ -47,14 +47,18 @@ type SparkLine struct {
}
// New returns a new SparkLine.
func New(opts ...Option) *SparkLine {
func New(opts ...Option) (*SparkLine, error) {
opt := newOptions()
for _, o := range opts {
o.set(opt)
}
if err := opt.validate(); err != nil {
return nil, err
}
return &SparkLine{
opts: opt,
}
}, nil
}
// Draw draws the SparkLine widget onto the canvas.

View File

@ -31,16 +31,30 @@ import (
func TestSparkLine(t *testing.T) {
tests := []struct {
desc string
sparkLine *SparkLine
opts []Option
update func(*SparkLine) error // update gets called before drawing of the widget.
canvas image.Rectangle
want func(size image.Point) *faketerm.Terminal
wantErr bool
wantUpdateErr bool // whether to expect an error on a call to the update function
wantDrawErr bool
}{
{
desc: "draws empty for no data points",
sparkLine: New(),
desc: "fails on negative height",
opts: []Option{
Height(-1),
},
update: func(sl *SparkLine) error {
return nil
},
canvas: image.Rect(0, 0, 1, 1),
want: func(size image.Point) *faketerm.Terminal {
return faketerm.MustNew(size)
},
wantErr: true,
},
{
desc: "draws empty for no data points",
update: func(sl *SparkLine) error {
return nil
},
@ -50,8 +64,7 @@ func TestSparkLine(t *testing.T) {
},
},
{
desc: "fails on negative data points",
sparkLine: New(),
desc: "fails on negative data points",
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 3, -1, 2})
},
@ -62,8 +75,7 @@ func TestSparkLine(t *testing.T) {
wantUpdateErr: true,
},
{
desc: "single height sparkline",
sparkLine: New(),
desc: "single height sparkline",
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 1, 2, 3, 4, 5, 6, 7, 8})
},
@ -80,8 +92,7 @@ func TestSparkLine(t *testing.T) {
},
},
{
desc: "sparkline can be cleared",
sparkLine: New(),
desc: "sparkline can be cleared",
update: func(sl *SparkLine) error {
if err := sl.Add([]int{0, 1, 2, 3, 4, 5, 6, 7, 8}); err != nil {
return err
@ -96,9 +107,9 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "sets sparkline color",
sparkLine: New(
opts: []Option{
Color(cell.ColorMagenta),
),
},
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 1, 2, 3, 4, 5, 6, 7, 8})
},
@ -115,8 +126,7 @@ func TestSparkLine(t *testing.T) {
},
},
{
desc: "sets sparkline color on a call to Add",
sparkLine: New(),
desc: "sets sparkline color on a call to Add",
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 1, 2, 3, 4, 5, 6, 7, 8}, Color(cell.ColorMagenta))
},
@ -134,8 +144,7 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "draws data points from the right",
sparkLine: New(),
desc: "draws data points from the right",
update: func(sl *SparkLine) error {
return sl.Add([]int{7, 8})
},
@ -154,9 +163,9 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "single height sparkline with label",
sparkLine: New(
opts: []Option{
Label("Hello"),
),
},
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 1, 2, 3, 8, 3, 2, 1, 1})
},
@ -176,9 +185,9 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "too long label is trimmed",
sparkLine: New(
opts: []Option{
Label("Hello world"),
),
},
update: func(sl *SparkLine) error {
return sl.Add([]int{8})
},
@ -197,8 +206,7 @@ func TestSparkLine(t *testing.T) {
},
},
{
desc: "stretches up to the height of the container",
sparkLine: New(),
desc: "stretches up to the height of the container",
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 100, 50, 85})
},
@ -232,9 +240,9 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "stretches up to the height of the container with label",
sparkLine: New(
opts: []Option{
Label("zoo"),
),
},
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 90, 30, 85})
},
@ -266,9 +274,9 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "respects fixed height",
sparkLine: New(
opts: []Option{
Height(2),
),
},
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 100, 50, 85})
},
@ -293,9 +301,9 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "draws resize needed character when canvas is smaller than requested",
sparkLine: New(
opts: []Option{
Height(2),
),
},
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 100, 50, 85})
},
@ -311,10 +319,10 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "respects fixed height with label",
sparkLine: New(
opts: []Option{
Label("zoo"),
Height(2),
),
},
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 100, 50, 0})
},
@ -339,13 +347,13 @@ func TestSparkLine(t *testing.T) {
},
{
desc: "sets label color",
sparkLine: New(
opts: []Option{
Label(
"Hello",
cell.FgColor(cell.ColorBlue),
cell.BgColor(cell.ColorYellow),
),
),
},
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 1})
},
@ -367,8 +375,7 @@ func TestSparkLine(t *testing.T) {
},
},
{
desc: "displays only data points that fit the width",
sparkLine: New(),
desc: "displays only data points that fit the width",
update: func(sl *SparkLine) error {
return sl.Add([]int{0, 1, 2, 3, 4, 5, 6, 7, 8})
},
@ -386,8 +393,7 @@ func TestSparkLine(t *testing.T) {
},
},
{
desc: "data points not visible don't affect the determined max data point",
sparkLine: New(),
desc: "data points not visible don't affect the determined max data point",
update: func(sl *SparkLine) error {
return sl.Add([]int{10, 4, 8})
},
@ -408,21 +414,28 @@ func TestSparkLine(t *testing.T) {
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
c, err := canvas.New(tc.canvas)
if err != nil {
t.Fatalf("canvas.New => unexpected error: %v", err)
}
err = tc.update(tc.sparkLine)
if (err != nil) != tc.wantUpdateErr {
t.Errorf("update => unexpected error: %v, wantUpdateErr: %v", err, tc.wantUpdateErr)
sp, err := New(tc.opts...)
if (err != nil) != tc.wantErr {
t.Errorf("New => unexpected error: %v, wantErr: %v", err, tc.wantErr)
}
if err != nil {
return
}
err = tc.sparkLine.Draw(c)
c, err := canvas.New(tc.canvas)
if err != nil {
t.Fatalf("canvas.New => unexpected error: %v", err)
}
err = tc.update(sp)
if (err != nil) != tc.wantUpdateErr {
t.Errorf("update => unexpected error: %v, wantUpdateErr: %v", err, tc.wantUpdateErr)
}
if err != nil {
return
}
err = sp.Draw(c)
if (err != nil) != tc.wantDrawErr {
t.Errorf("Draw => unexpected error: %v, wantDrawErr: %v", err, tc.wantDrawErr)
}
@ -448,13 +461,12 @@ func TestSparkLine(t *testing.T) {
func TestOptions(t *testing.T) {
tests := []struct {
desc string
sparkLine *SparkLine
want widgetapi.Options
desc string
opts []Option
want widgetapi.Options
}{
{
desc: "no label and no fixed height",
sparkLine: New(),
desc: "no label and no fixed height",
want: widgetapi.Options{
MinimumSize: image.Point{1, 1},
WantKeyboard: false,
@ -463,9 +475,9 @@ func TestOptions(t *testing.T) {
},
{
desc: "label and no fixed height",
sparkLine: New(
opts: []Option{
Label("foo"),
),
},
want: widgetapi.Options{
MinimumSize: image.Point{1, 2},
WantKeyboard: false,
@ -474,9 +486,9 @@ func TestOptions(t *testing.T) {
},
{
desc: "no label and fixed height",
sparkLine: New(
opts: []Option{
Height(3),
),
},
want: widgetapi.Options{
MinimumSize: image.Point{1, 3},
MaximumSize: image.Point{1, 3},
@ -486,10 +498,10 @@ func TestOptions(t *testing.T) {
},
{
desc: "label and fixed height",
sparkLine: New(
opts: []Option{
Label("foo"),
Height(3),
),
},
want: widgetapi.Options{
MinimumSize: image.Point{1, 4},
MaximumSize: image.Point{1, 4},
@ -501,7 +513,11 @@ func TestOptions(t *testing.T) {
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
got := tc.sparkLine.Options()
sp, err := New(tc.opts...)
if err != nil {
t.Fatalf("New => unexpected error: %v", err)
}
got := sp.Options()
if diff := pretty.Compare(tc.want, got); diff != "" {
t.Errorf("Options => unexpected diff (-want, +got):\n%s", diff)
}

View File

@ -59,20 +59,29 @@ func main() {
defer t.Close()
ctx, cancel := context.WithCancel(context.Background())
green := sparkline.New(
green, err := sparkline.New(
sparkline.Label("Green SparkLine", cell.FgColor(cell.ColorBlue)),
sparkline.Color(cell.ColorGreen),
)
if err != nil {
panic(err)
}
go playSparkLine(ctx, green, 250*time.Millisecond)
red := sparkline.New(
red, err := sparkline.New(
sparkline.Label("Red SparkLine", cell.FgColor(cell.ColorBlue)),
sparkline.Color(cell.ColorRed),
)
if err != nil {
panic(err)
}
go playSparkLine(ctx, red, 500*time.Millisecond)
yellow := sparkline.New(
yellow, err := sparkline.New(
sparkline.Label("Yellow SparkLine", cell.FgColor(cell.ColorGreen)),
sparkline.Color(cell.ColorYellow),
)
if err != nil {
panic(err)
}
go playSparkLine(ctx, yellow, 1*time.Second)
c, err := container.New(

View File

@ -15,6 +15,8 @@
package text
import (
"fmt"
"github.com/mum4k/termdash/keyboard"
"github.com/mum4k/termdash/mouse"
)
@ -56,6 +58,23 @@ func newOptions(opts ...Option) *options {
return opt
}
// validate validates the provided options.
func (o *options) validate() error {
keys := map[keyboard.Key]bool{
o.keyUp: true,
o.keyDown: true,
o.keyPgUp: true,
o.keyPgDown: true,
}
if len(keys) != 4 {
return fmt.Errorf("invalid ScrollKeys(up:%v, down:%v, pageUp:%v, pageDown:%v), the keys must be unique", o.keyUp, o.keyDown, o.keyPgUp, o.keyPgDown)
}
if o.mouseUpButton == o.mouseDownButton {
return fmt.Errorf("invalid ScrollMouseButtons(up:%v, down:%v), the buttons must be unique", o.mouseUpButton, o.mouseDownButton)
}
return nil
}
// option implements Option.
type option func(*options)
@ -97,6 +116,8 @@ const (
)
// ScrollMouseButtons configures the mouse buttons that scroll the content.
// The provided buttons must be unique, e.g. the same button cannot be both up
// and down.
func ScrollMouseButtons(up, down mouse.Button) Option {
return option(func(opts *options) {
opts.mouseUpButton = up
@ -113,6 +134,8 @@ const (
)
// ScrollKeys configures the mouse buttons that scroll the content.
// The provided keys must be unique, e.g. the same key cannot be both up and
// down.
func ScrollKeys(up, down, pageUp, pageDown keyboard.Key) Option {
return option(func(opts *options) {
opts.keyUp = up

View File

@ -69,13 +69,16 @@ type Text struct {
}
// New returns a new text widget.
func New(opts ...Option) *Text {
func New(opts ...Option) (*Text, error) {
opt := newOptions(opts...)
if err := opt.validate(); err != nil {
return nil, err
}
return &Text{
wOptsTracker: attrrange.NewTracker(),
scroll: newScrollTracker(opt),
opts: opt,
}
}, nil
}
// Reset resets the widget back to empty content.

View File

@ -39,8 +39,31 @@ func TestTextDraws(t *testing.T) {
writes func(*Text) error
events func(*Text)
want func(size image.Point) *faketerm.Terminal
wantErr bool
wantWriteErr bool
}{
{
desc: "fails when scroll keys aren't unique",
opts: []Option{
ScrollKeys('a', 'a', 'a', 'a'),
},
canvas: image.Rect(0, 0, 1, 1),
want: func(size image.Point) *faketerm.Terminal {
return faketerm.MustNew(size)
},
wantErr: true,
},
{
desc: "fails when scroll mouse buttons aren't unique",
opts: []Option{
ScrollMouseButtons(mouse.ButtonLeft, mouse.ButtonLeft),
},
canvas: image.Rect(0, 0, 1, 1),
want: func(size image.Point) *faketerm.Terminal {
return faketerm.MustNew(size)
},
wantErr: true,
},
{
desc: "empty when no written text",
canvas: image.Rect(0, 0, 1, 1),
@ -691,7 +714,14 @@ func TestTextDraws(t *testing.T) {
t.Fatalf("canvas.New => unexpected error: %v", err)
}
widget := New(tc.opts...)
widget, err := New(tc.opts...)
if (err != nil) != tc.wantErr {
t.Errorf("New => unexpected error: %v, wantErr: %v", err, tc.wantErr)
}
if err != nil {
return
}
if tc.writes != nil {
err := tc.writes(widget)
if (err != nil) != tc.wantWriteErr {
@ -755,7 +785,11 @@ func TestOptions(t *testing.T) {
for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
text := New(tc.opts...)
text, err := New(tc.opts...)
if err != nil {
t.Fatalf("New => unexpected error: %v", err)
}
got := text.Options()
if diff := pretty.Compare(tc.want, got); diff != "" {
t.Errorf("Options => unexpected diff (-want, +got):\n%s", diff)

View File

@ -74,22 +74,34 @@ func main() {
defer t.Close()
ctx, cancel := context.WithCancel(context.Background())
borderless := text.New()
borderless, err := text.New()
if err != nil {
panic(err)
}
if err := borderless.Write("Text without border."); err != nil {
panic(err)
}
unicode := text.New()
unicode, err := text.New()
if err != nil {
panic(err)
}
if err := unicode.Write("你好,世界!"); err != nil {
panic(err)
}
trimmed := text.New()
trimmed, err := text.New()
if err != nil {
panic(err)
}
if err := trimmed.Write("Trims lines that don't fit onto the canvas because they are too long for its width.."); err != nil {
panic(err)
}
wrapped := text.New(text.WrapAtRunes())
wrapped, err := text.New(text.WrapAtRunes())
if err != nil {
panic(err)
}
if err := wrapped.Write("Supports", text.WriteCellOpts(cell.FgColor(cell.ColorRed))); err != nil {
panic(err)
}
@ -100,7 +112,10 @@ func main() {
panic(err)
}
rolled := text.New(text.RollContent(), text.WrapAtRunes())
rolled, err := text.New(text.RollContent(), text.WrapAtRunes())
if err != nil {
panic(err)
}
if err := rolled.Write("Rolls the content upwards if RollContent() option is provided.\nSupports keyboard and mouse scrolling.\n\n"); err != nil {
panic(err)
}