mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-05 16:37:19 -04:00
Fix IndentWriter indenting after trailing line-breaks
This commit is contained in:
@@ -3,37 +3,29 @@ package gold
|
|||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type IndentWriter struct {
|
type IndentWriter struct {
|
||||||
Indent uint
|
Indent uint
|
||||||
Forward io.Writer
|
Forward io.Writer
|
||||||
|
|
||||||
initialWrite sync.Once
|
skipIndent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write is used to write more content to the reflow buffer.
|
// Write is used to write content to the indent buffer.
|
||||||
func (w *IndentWriter) Write(b []byte) (int, error) {
|
func (w *IndentWriter) Write(b []byte) (int, error) {
|
||||||
w.initialWrite.Do(func() {
|
|
||||||
w.Forward.Write([]byte(strings.Repeat(" ", int(w.Indent))))
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, c := range string(b) {
|
for _, c := range string(b) {
|
||||||
|
if !w.skipIndent {
|
||||||
|
w.Forward.Write([]byte(strings.Repeat(" ", int(w.Indent))))
|
||||||
|
w.skipIndent = true
|
||||||
|
}
|
||||||
|
|
||||||
w.Forward.Write([]byte{byte(c)})
|
w.Forward.Write([]byte{byte(c)})
|
||||||
if c == '\n' {
|
if c == '\n' {
|
||||||
// end of current line
|
// end of current line
|
||||||
w.Forward.Write([]byte(strings.Repeat(" ", int(w.Indent))))
|
w.skipIndent = false
|
||||||
} else {
|
|
||||||
// any other character
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return len(b), nil
|
return len(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close will finish the reflow operation. Always call it before trying to
|
|
||||||
// retrieve the final result.
|
|
||||||
func (w *IndentWriter) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user