mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 08:57:14 -04:00
Reset and restore ANSI state before writing indent
This commit is contained in:
@@ -1,39 +1,37 @@
|
|||||||
package gold
|
package gold
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IndentWriter struct {
|
type IndentWriter struct {
|
||||||
|
Forward *AnsiWriter
|
||||||
Indent uint
|
Indent uint
|
||||||
Forward io.Writer
|
|
||||||
|
|
||||||
skipIndent bool
|
skipIndent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write is used to write content to the indent 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) {
|
||||||
if w.Indent == 0 {
|
|
||||||
return w.Forward.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range string(b) {
|
for _, c := range string(b) {
|
||||||
if !w.skipIndent {
|
if !w.skipIndent {
|
||||||
_, err := w.Forward.Write([]byte(strings.Repeat(" ", int(w.Indent))))
|
_, err := w.Forward.Write([]byte(strings.Repeat(" ", int(w.Indent))))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
w.Forward.RestoreAnsi()
|
||||||
|
|
||||||
w.skipIndent = true
|
w.skipIndent = true
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := w.Forward.Write([]byte(string(c)))
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if c == '\n' {
|
if c == '\n' {
|
||||||
// end of current line
|
// end of current line
|
||||||
w.skipIndent = false
|
w.skipIndent = false
|
||||||
|
w.Forward.ResetAnsi()
|
||||||
|
}
|
||||||
|
_, err := w.Forward.Write([]byte(string(c)))
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user