okami.mom/glamour

git clone https://git.nkp.pet/okami.mom/glamour

Kieran Klukaschore: fix pre-existing lint failuresc293649

main
1.5 KiB63 linesraw
1package ansi
2
3import (
4	"bytes"
5	"fmt"
6	"io"
7	"strings"
8
9	"charm.land/lipgloss/v2"
10)
11
12// A ParagraphElement is used to render individual paragraphs.
13type ParagraphElement struct {
14	First bool
15}
16
17// Render renders a ParagraphElement.
18func (e *ParagraphElement) Render(w io.Writer, ctx RenderContext) error {
19	bs := ctx.blockStack
20	rules := ctx.options.Styles.Paragraph
21
22	if !e.First {
23		_, _ = io.WriteString(w, "\n")
24	}
25	be := BlockElement{
26		Block: &bytes.Buffer{},
27		Style: cascadeStyle(bs.Current().Style, rules, false),
28	}
29	bs.Push(be)
30
31	_, _ = renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
32	_, _ = renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, rules.Prefix)
33	return nil
34}
35
36// Finish finishes rendering a ParagraphElement.
37func (e *ParagraphElement) Finish(w io.Writer, ctx RenderContext) error {
38	bs := ctx.blockStack
39	rules := bs.Current().Style
40
41	mw := NewMarginWriter(ctx, w, rules)
42	defer mw.Close() //nolint:errcheck
43	if len(strings.TrimSpace(bs.Current().Block.String())) > 0 {
44		blk := bs.Current().Block.String()
45		if !ctx.options.PreserveNewLines {
46			blk = strings.ReplaceAll(blk, "\n", " ")
47		}
48		flow := lipgloss.Wrap(blk, int(bs.Width(ctx)), "")
49
50		_, err := io.WriteString(mw, flow)
51		if err != nil {
52			return fmt.Errorf("glamour: error writing to writer: %w", err)
53		}
54		_, _ = io.WriteString(mw, "\n")
55	}
56
57	_, _ = renderText(w, bs.Current().Style.StylePrimitive, rules.Suffix)
58	_, _ = renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
59
60	bs.Current().Block.Reset()
61	bs.Pop()
62	return nil
63}