mirror of https://github.com/mum4k/termdash.git
Allow empty Y-axis labels on linechart using a value formatter that returns empty strings
Signed-off-by: Xabier Larrakoetxea <slok69@gmail.com>
This commit is contained in:
parent
e152366a5c
commit
150b7e26e2
|
@ -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.
|
// 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
|
// For the purposes of the alignment this assumes that text will be trimmed if
|
||||||
// it overruns the rectangle.
|
// 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) {
|
func Text(rect image.Rectangle, text string, h align.Horizontal, v align.Vertical) (image.Point, error) {
|
||||||
if strings.ContainsRune(text, '\n') {
|
if strings.ContainsRune(text, '\n') {
|
||||||
return image.ZP, fmt.Errorf("the provided text contains a newline character: %q", text)
|
return image.ZP, fmt.Errorf("the provided text contains a newline character: %q", text)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := wrap.ValidText(text); err != nil {
|
if text != "" {
|
||||||
return image.ZP, fmt.Errorf("the provided text contains non printable character(s): %s", err)
|
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)
|
cells := runewidth.StringWidth(text)
|
||||||
|
|
|
@ -59,8 +59,8 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// ValidText validates the provided text for wrapping.
|
// ValidText validates the provided text for wrapping.
|
||||||
// The text must not contain any control or space characters other
|
// The text must not be empty, contain any control or
|
||||||
// than '\n' and ' '.
|
// space characters other than '\n' and ' '.
|
||||||
func ValidText(text string) error {
|
func ValidText(text string) error {
|
||||||
if text == "" {
|
if text == "" {
|
||||||
return errors.New("the text cannot be empty")
|
return errors.New("the text cannot be empty")
|
||||||
|
|
|
@ -71,7 +71,7 @@ func yLabels(scale *YScale, labelWidth int) ([]*Label, error) {
|
||||||
if min := 2; scale.GraphHeight < min {
|
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)
|
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)
|
return nil, fmt.Errorf("cannot place labels in label area width %d, minimum is %d", labelWidth, min)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ func TestYLabels(t *testing.T) {
|
||||||
min: 0,
|
min: 0,
|
||||||
max: 1,
|
max: 1,
|
||||||
graphHeight: 2,
|
graphHeight: 2,
|
||||||
labelWidth: 0,
|
labelWidth: -1,
|
||||||
wantErr: true,
|
wantErr: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -1692,7 +1692,31 @@ func TestLineChartDraws(t *testing.T) {
|
||||||
writes: func(lc *LineChart) error {
|
writes: func(lc *LineChart) error {
|
||||||
return lc.Series("first", []float64{0, 100})
|
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",
|
desc: "custom Y-axis labels using a value formatter that returns very long strings",
|
||||||
|
|
Loading…
Reference in New Issue