mirror of
https://github.com/mum4k/termdash.git
synced 2025-04-27 13:48:49 +08:00
Check also for non printable characters on alingfor package text helper method
Signed-off-by: Xabier Larrakoetxea <slok69@gmail.com>
This commit is contained in:
parent
7571889bec
commit
a8931e2820
@ -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() {
|
||||
|
@ -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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user