Added NoopRenderer.

This commit is contained in:
Matteo Kloiber 2015-04-05 21:36:41 +02:00
parent c649a7675c
commit b22b4c8b71
2 changed files with 58 additions and 1 deletions

View File

@ -7,7 +7,7 @@ import (
// TextRender adds common methods for rendering a text on screeen.
type TextRender interface {
NormalizedText(text string) string
NormalizedText() string
Render(lastColor, background Attribute) RenderedSequence
RenderSequence(start, end int, lastColor, background Attribute) RenderedSequence
}
@ -192,3 +192,31 @@ func (s *RenderedSequence) PointAt(n, x, y int) (Point, int) {
char := []rune(s.NormalizedText)[n]
return Point{char, s.BackgroundColor, color, x, y}, charWidth(char)
}
// A NoopRenderer does not render the text at all.
type NoopRenderer struct {
Text string
}
// NormalizedText returns the text given in
func (r NoopRenderer) NormalizedText() string {
return r.Text
}
// RenderSequence returns a RenderedSequence that does not have any color
// sequences.
func (r NoopRenderer) RenderSequence(start, end int, lastColor, background Attribute) RenderedSequence {
runes := []rune(r.Text)
if end < 0 {
end = len(runes)
}
runes = runes[start:end]
var s []ColorSubsequence
return RenderedSequence{string(runes), lastColor, background, s, nil}
}
// Render just like RenderSequence
func (r NoopRenderer) Render(lastColor, background Attribute) RenderedSequence {
return r.RenderSequence(0, -1, lastColor, background)
}

View File

@ -9,6 +9,13 @@ import (
"github.com/stretchr/testify/require"
)
func TestTextRender_TestInterface(t *testing.T) {
var inter *TextRender
assert.Implements(t, inter, new(MarkdownTextRenderer))
assert.Implements(t, inter, new(NoopRenderer))
}
func TestMarkdownTextRenderer_normalizeText(t *testing.T) {
renderer := MarkdownTextRenderer{}
@ -212,6 +219,28 @@ func TestRenderedSequence_PointAt(t *testing.T) {
AssertPoint(t, pointAt(10, 7, 1), 'd', 7, 1)
}
func getTestNoopRenderer() NoopRenderer {
return NoopRenderer{"[Hello](red) \x1b[31mworld"}
}
func TestNoopRenderer_NormalizedText(t *testing.T) {
r := getTestNoopRenderer()
assert.Equal(t, "[Hello](red) \x1b[31mworld", r.NormalizedText())
assert.Equal(t, "[Hello](red) \x1b[31mworld", r.Text)
}
func TestNoopRenderer_Render(t *testing.T) {
renderer := getTestNoopRenderer()
got := renderer.Render(5, 7)
assertRenderSequence(t, got, 5, 7, "[Hello](red) \x1b[31mworld", 0)
}
func TestNoopRenderer_RenderSequence(t *testing.T) {
renderer := getTestNoopRenderer()
got := renderer.RenderSequence(3, 5, 9, 1)
assertRenderSequence(t, got, 9, 1, "ll", 0)
}
func TestPosUnicode(t *testing.T) {
// Every characters takes 3 bytes
text := "你好世界"