mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-29 13:56:53 +01:00
89e93d90b3
Merge branch 'master' into use-glamour select Glamour Theme based on BackgroundColor Merge branch 'master' into use-glamour Merge branch 'master' into use-glamour update termev update go.mod label color colorate use glamour for issue content Vendor: Add glamour Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://gitea.com/gitea/tea/pulls/181 Reviewed-by: techknowlogick <techknowlogick@gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package ansi
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
|
|
"github.com/muesli/reflow/wordwrap"
|
|
)
|
|
|
|
// BlockElement provides a render buffer for children of a block element.
|
|
// After all children have been rendered into it, it applies indentation and
|
|
// margins around them and writes everything to the parent rendering buffer.
|
|
type BlockElement struct {
|
|
Block *bytes.Buffer
|
|
Style StyleBlock
|
|
Margin bool
|
|
Newline bool
|
|
}
|
|
|
|
func (e *BlockElement) Render(w io.Writer, ctx RenderContext) error {
|
|
bs := ctx.blockStack
|
|
bs.Push(*e)
|
|
|
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockPrefix)
|
|
renderText(bs.Current().Block, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, e.Style.Prefix)
|
|
return nil
|
|
}
|
|
|
|
func (e *BlockElement) Finish(w io.Writer, ctx RenderContext) error {
|
|
bs := ctx.blockStack
|
|
|
|
if e.Margin {
|
|
mw := NewMarginWriter(ctx, w, bs.Current().Style)
|
|
_, err := mw.Write(
|
|
wordwrap.Bytes(bs.Current().Block.Bytes(), int(bs.Width(ctx))))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if e.Newline {
|
|
_, err = mw.Write([]byte("\n"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
} else {
|
|
_, err := bs.Parent().Block.Write(bs.Current().Block.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, e.Style.Suffix)
|
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockSuffix)
|
|
|
|
bs.Current().Block.Reset()
|
|
bs.Pop()
|
|
return nil
|
|
}
|