12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package richtext
- import "strings"
- type RenderOptionsConsole struct {
- AnsiPalette AnsiPalette
- }
- func (opts RenderOptionsConsole) WrapSpanContent(content string, tags ([] string)) string {
- if opts.AnsiPalette != nil {
- var buf strings.Builder
- for _, tag := range tags {
- buf.WriteString(opts.AnsiPalette(tag))
- }
- buf.WriteString(content)
- buf.WriteString(reset)
- return buf.String()
- } else {
- return content
- }
- }
- type RenderOptionsHtml struct {
- // empty now
- }
- func (b Block) RenderPlainText() string {
- return b.RenderConsole(RenderOptionsConsole {})
- }
- func (b Block) RenderConsole(opts RenderOptionsConsole) string {
- var buf strings.Builder
- for _, l := range b.Lines {
- for i, span := range l.Spans {
- buf.WriteString(opts.WrapSpanContent(span.Content, span.Tags))
- if (i + 1) < len(l.Spans) {
- var next = l.Spans[(i + 1)]
- if !(strings.HasSuffix(span.Content, " ")) &&
- !(strings.HasPrefix(next.Content, " ")) &&
- next.Content != "," &&
- next.Content != ":" {
- buf.WriteRune(' ')
- }
- }
- }
- if len(l.Spans) > 0 {
- buf.WriteRune('\n')
- }
- }
- for _, child := range b.Children {
- buf.WriteString(child.RenderConsole(opts))
- }
- return buf.String()
- }
- func (b Block) RenderHtml(_ RenderOptionsHtml) Html {
- panic("not implemented") // TODO
- }
|