Don't indent line if all we're outputting is ANSI sequences

This commit is contained in:
Christian Muehlhaeuser
2019-12-04 08:56:15 +01:00
parent f7244397d2
commit 550c0bdbd5
3 changed files with 30 additions and 18 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ func (w *AnsiWriter) Write(b []byte) (int, error) {
w.ansiseq += string(c) w.ansiseq += string(c)
} else if w.ansi { } else if w.ansi {
w.ansiseq += string(c) w.ansiseq += string(c)
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) { if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
// ANSI sequence terminated // ANSI sequence terminated
w.ansi = false w.ansi = false
+28 -16
View File
@@ -13,32 +13,44 @@ type IndentWriter struct {
IndentFunc IndentFunc IndentFunc IndentFunc
skipIndent bool skipIndent bool
ansi 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) {
for _, c := range string(b) { for _, c := range string(b) {
if !w.skipIndent { if c == '\x1B' {
if w.IndentFunc != nil { // ANSI escape sequence
for i := 0; i < int(w.Indent); i++ { w.ansi = true
w.IndentFunc(w.Forward) } else if w.ansi {
} if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
} else { // ANSI sequence terminated
_, err := w.Forward.Write([]byte(strings.Repeat(" ", int(w.Indent)))) w.ansi = false
if err != nil { }
return 0, err } else {
if !w.skipIndent {
w.Forward.ResetAnsi()
if w.IndentFunc != nil {
for i := 0; i < int(w.Indent); i++ {
w.IndentFunc(w.Forward)
}
} else {
_, err := w.Forward.Write([]byte(strings.Repeat(" ", int(w.Indent))))
if err != nil {
return 0, err
}
} }
w.skipIndent = true
w.Forward.RestoreAnsi()
} }
w.Forward.RestoreAnsi() if c == '\n' {
w.skipIndent = true // end of current line
w.skipIndent = false
}
} }
if c == '\n' {
// end of current line
w.skipIndent = false
w.Forward.ResetAnsi()
}
_, err := w.Forward.Write([]byte(string(c))) _, err := w.Forward.Write([]byte(string(c)))
if err != nil { if err != nil {
return 0, err return 0, err
+1 -1
View File
@@ -22,7 +22,7 @@ func (w *PaddingWriter) Write(b []byte) (int, error) {
// ANSI escape sequence // ANSI escape sequence
w.ansi = true w.ansi = true
} else if w.ansi { } else if w.ansi {
if (c >= 0x40 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) { if (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) {
// ANSI sequence terminated // ANSI sequence terminated
w.ansi = false w.ansi = false
w.lineLen-- w.lineLen--