From a8931e2820edf62fc9a2f245396d107f3bf67959 Mon Sep 17 00:00:00 2001 From: Xabier Larrakoetxea Date: Fri, 3 May 2019 07:04:10 +0200 Subject: [PATCH] Check also for non printable characters on alingfor package text helper method Signed-off-by: Xabier Larrakoetxea --- internal/alignfor/align.go | 7 ++++++- internal/alignfor/align_test.go | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/alignfor/align.go b/internal/alignfor/align.go index d19663a..8de9af2 100644 --- a/internal/alignfor/align.go +++ b/internal/alignfor/align.go @@ -22,6 +22,7 @@ import ( "github.com/mum4k/termdash/align" "github.com/mum4k/termdash/internal/runewidth" + "github.com/mum4k/termdash/internal/wrap" ) // hAlign aligns the given area in the rectangle horizontally. @@ -89,12 +90,16 @@ 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 newlines. +// This only supports a single line of text, the text must not contain non-printable characters. 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) + } + cells := runewidth.StringWidth(text) var textLen int if cells < rect.Dx() { diff --git a/internal/alignfor/align_test.go b/internal/alignfor/align_test.go index 276fcd1..a7492cb 100644 --- a/internal/alignfor/align_test.go +++ b/internal/alignfor/align_test.go @@ -243,6 +243,12 @@ func TestText(t *testing.T) { text: "a\nb", wantErr: true, }, + { + desc: "fails when text contains non-printable characters", + rect: image.Rect(0, 0, 3, 3), + text: "a\tb", + wantErr: true, + }, { desc: "aligns text top and left", rect: image.Rect(1, 1, 4, 4),