diff --git a/internal/alignfor/align.go b/internal/alignfor/align.go index 8de9af2..b1bd3a5 100644 --- a/internal/alignfor/align.go +++ b/internal/alignfor/align.go @@ -90,14 +90,17 @@ func Rectangle(rect image.Rectangle, ar image.Rectangle, h align.Horizontal, v a // Text aligns the text within the given rectangle, returns the start point for the text. // For the purposes of the alignment this assumes that text will be trimmed if // it overruns the rectangle. -// This only supports a single line of text, the text must not contain non-printable characters. +// This only supports a single line of text, the text must not contain non-printable characters, +// allows empty text. func Text(rect image.Rectangle, text string, h align.Horizontal, v align.Vertical) (image.Point, error) { if strings.ContainsRune(text, '\n') { return image.ZP, fmt.Errorf("the provided text contains a newline character: %q", text) } - if err := wrap.ValidText(text); err != nil { - return image.ZP, fmt.Errorf("the provided text contains non printable character(s): %s", err) + if text != "" { + if err := wrap.ValidText(text); err != nil { + return image.ZP, fmt.Errorf("the provided text contains non printable character(s): %s", err) + } } cells := runewidth.StringWidth(text) diff --git a/internal/wrap/wrap.go b/internal/wrap/wrap.go index b0ead04..2b6f62a 100644 --- a/internal/wrap/wrap.go +++ b/internal/wrap/wrap.go @@ -59,8 +59,8 @@ const ( ) // ValidText validates the provided text for wrapping. -// The text must not contain any control or space characters other -// than '\n' and ' '. +// The text must not be empty, contain any control or +// space characters other than '\n' and ' '. func ValidText(text string) error { if text == "" { return errors.New("the text cannot be empty") diff --git a/widgets/linechart/internal/axes/label.go b/widgets/linechart/internal/axes/label.go index 5e13ba2..46e6888 100644 --- a/widgets/linechart/internal/axes/label.go +++ b/widgets/linechart/internal/axes/label.go @@ -71,7 +71,7 @@ func yLabels(scale *YScale, labelWidth int) ([]*Label, error) { if min := 2; scale.GraphHeight < min { return nil, fmt.Errorf("cannot place labels on a canvas with height %d, minimum is %d", scale.GraphHeight, min) } - if min := 1; labelWidth < min { + if min := 0; labelWidth < min { return nil, fmt.Errorf("cannot place labels in label area width %d, minimum is %d", labelWidth, min) } diff --git a/widgets/linechart/internal/axes/label_test.go b/widgets/linechart/internal/axes/label_test.go index 35221de..d84e2b4 100644 --- a/widgets/linechart/internal/axes/label_test.go +++ b/widgets/linechart/internal/axes/label_test.go @@ -45,7 +45,7 @@ func TestYLabels(t *testing.T) { min: 0, max: 1, graphHeight: 2, - labelWidth: 0, + labelWidth: -1, wantErr: true, }, { diff --git a/widgets/linechart/linechart_test.go b/widgets/linechart/linechart_test.go index ea7ccf3..b04fc08 100644 --- a/widgets/linechart/linechart_test.go +++ b/widgets/linechart/linechart_test.go @@ -1692,7 +1692,31 @@ func TestLineChartDraws(t *testing.T) { writes: func(lc *LineChart) error { return lc.Series("first", []float64{0, 100}) }, - wantDrawErr: true, + wantCapacity: 38, + want: func(size image.Point) *faketerm.Terminal { + ft := faketerm.MustNew(size) + c := testcanvas.MustNew(ft.Area()) + + // Y and X axis. + lines := []draw.HVLine{ + {Start: image.Point{0, 0}, End: image.Point{0, 8}}, + {Start: image.Point{0, 8}, End: image.Point{19, 8}}, + } + testdraw.MustHVLines(c, lines) + + // Value labels. + testdraw.MustText(c, "0", image.Point{1, 9}) + testdraw.MustText(c, "1", image.Point{19, 9}) + + // Braille line. + graphAr := image.Rect(1, 0, 20, 8) + bc := testbraille.MustNew(graphAr) + testdraw.MustBrailleLine(bc, image.Point{0, 31}, image.Point{36, 0}) + testbraille.MustCopyTo(bc, c) + + testcanvas.MustApply(c, ft) + return ft + }, }, { desc: "custom Y-axis labels using a value formatter that returns very long strings",