Added TextRendererFactory.

This commit is contained in:
Matteo Kloiber 2015-04-05 21:46:27 +02:00
parent b22b4c8b71
commit be167436b7
2 changed files with 42 additions and 0 deletions

View File

@ -12,6 +12,11 @@ type TextRender interface {
RenderSequence(start, end int, lastColor, background Attribute) RenderedSequence RenderSequence(start, end int, lastColor, background Attribute) RenderedSequence
} }
// TextRendererFactory is factory for creating text renderers.
type TextRendererFactory interface {
TextRenderer(text string) TextRender
}
// MarkdownRegex is used by MarkdownTextRenderer to determine how to format the // MarkdownRegex is used by MarkdownTextRenderer to determine how to format the
// text. // text.
const MarkdownRegex = `(?:\[([^]]+)\])\(([a-z\s,]+)\)` const MarkdownRegex = `(?:\[([^]]+)\])\(([a-z\s,]+)\)`
@ -124,6 +129,15 @@ func (r MarkdownTextRenderer) RenderSequence(start, end int, lastColor, backgrou
return RenderedSequence{string(runes), lastColor, background, sequences, nil} return RenderedSequence{string(runes), lastColor, background, sequences, nil}
} }
// MarkdownTextRendererFactory is a TextRendererFactory for
// the MarkdownTextRenderer.
type MarkdownTextRendererFactory struct{}
// TextRenderer returns a MarkdownTextRenderer instance.
func (f MarkdownTextRendererFactory) TextRenderer(text string) TextRender {
return MarkdownTextRenderer{text}
}
// RenderedSequence is a string sequence that is capable of returning the // RenderedSequence is a string sequence that is capable of returning the
// Buffer used by termui for displaying the colorful string. // Buffer used by termui for displaying the colorful string.
type RenderedSequence struct { type RenderedSequence struct {
@ -220,3 +234,12 @@ func (r NoopRenderer) RenderSequence(start, end int, lastColor, background Attri
func (r NoopRenderer) Render(lastColor, background Attribute) RenderedSequence { func (r NoopRenderer) Render(lastColor, background Attribute) RenderedSequence {
return r.RenderSequence(0, -1, lastColor, background) return r.RenderSequence(0, -1, lastColor, background)
} }
// NoopRendererFactory is a TextRendererFactory for
// the NoopRenderer.
type NoopRendererFactory struct{}
// TextRenderer returns a NoopRenderer instance.
func (f NoopRendererFactory) TextRenderer(text string) TextRender {
return NoopRenderer{text}
}

View File

@ -16,6 +16,13 @@ func TestTextRender_TestInterface(t *testing.T) {
assert.Implements(t, inter, new(NoopRenderer)) assert.Implements(t, inter, new(NoopRenderer))
} }
func TestTextRendererFactory_TestInterface(t *testing.T) {
var inter *TextRendererFactory
assert.Implements(t, inter, new(MarkdownTextRendererFactory))
assert.Implements(t, inter, new(NoopRendererFactory))
}
func TestMarkdownTextRenderer_normalizeText(t *testing.T) { func TestMarkdownTextRenderer_normalizeText(t *testing.T) {
renderer := MarkdownTextRenderer{} renderer := MarkdownTextRenderer{}
@ -135,6 +142,12 @@ func TestMarkdownTextRenderer_Render(t *testing.T) {
} }
} }
func TestMarkdownTextRendererFactory(t *testing.T) {
factory := MarkdownTextRendererFactory{}
expected := MarkdownTextRenderer{"Hello world"}
assert.Equal(t, factory.TextRenderer("Hello world"), expected)
}
func TestColorSubsequencesToMap(t *testing.T) { func TestColorSubsequencesToMap(t *testing.T) {
colorSubsequences := []ColorSubsequence{ colorSubsequences := []ColorSubsequence{
{ColorRed, 1, 4}, {ColorRed, 1, 4},
@ -241,6 +254,12 @@ func TestNoopRenderer_RenderSequence(t *testing.T) {
assertRenderSequence(t, got, 9, 1, "ll", 0) assertRenderSequence(t, got, 9, 1, "ll", 0)
} }
func TestNoopRendererFactory(t *testing.T) {
factory := NoopRendererFactory{}
expected := NoopRenderer{"Hello world"}
assert.Equal(t, factory.TextRenderer("Hello world"), expected)
}
func TestPosUnicode(t *testing.T) { func TestPosUnicode(t *testing.T) {
// Every characters takes 3 bytes // Every characters takes 3 bytes
text := "你好世界" text := "你好世界"