Added TextRendererFactory.
This commit is contained in:
parent
b22b4c8b71
commit
be167436b7
@ -12,6 +12,11 @@ type TextRender interface {
|
||||
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
|
||||
// text.
|
||||
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}
|
||||
}
|
||||
|
||||
// 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
|
||||
// Buffer used by termui for displaying the colorful string.
|
||||
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 {
|
||||
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}
|
||||
}
|
||||
|
@ -16,6 +16,13 @@ func TestTextRender_TestInterface(t *testing.T) {
|
||||
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) {
|
||||
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) {
|
||||
colorSubsequences := []ColorSubsequence{
|
||||
{ColorRed, 1, 4},
|
||||
@ -241,6 +254,12 @@ func TestNoopRenderer_RenderSequence(t *testing.T) {
|
||||
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) {
|
||||
// Every characters takes 3 bytes
|
||||
text := "你好世界"
|
||||
|
Loading…
Reference in New Issue
Block a user