Skip to content

Commit a0247c1

Browse files
author
treilik
committed
working end and restart of ansi at linebreak
but hardwrap not working yet
1 parent eff0c4c commit a0247c1

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

wordwrap/wordwrap.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ type WordWrap struct {
3030

3131
hardWriter ansi.Writer
3232

33-
lineLen int
34-
ansi bool
35-
lastAnsi bytes.Buffer
33+
wroteBegin bool
34+
lineLen int
35+
ansi bool
36+
lastAnsi bytes.Buffer
3637
}
3738

3839
// NewWriter returns a new instance of a word-wrapping writer, initialized with
@@ -86,6 +87,7 @@ func (w *WordWrap) addNewLine() {
8687
w.buf.WriteRune('\n')
8788
w.lineLen = 0
8889
w.space.Reset()
90+
w.wroteBegin = false
8991
}
9092

9193
func inGroup(a []rune, c rune) bool {
@@ -110,10 +112,11 @@ func (w *WordWrap) Write(b []byte) (int, error) {
110112

111113
for _, c := range s {
112114
// Restart Ansi after line break if there is more text
113-
if w.lineLen == 0 && w.buf.Len() == 0 && w.lastAnsi.Len() != 0 {
115+
if !w.wroteBegin && !w.ansi && w.lastAnsi.Len() != 0 {
114116
w.buf.Write(w.lastAnsi.Bytes())
115117
w.addWord()
116118
}
119+
w.wroteBegin = true
117120
if c == '\x1B' {
118121
// ANSI escape sequence
119122
w.word.WriteRune(c)
@@ -126,7 +129,7 @@ func (w *WordWrap) Write(b []byte) (int, error) {
126129
// ANSI sequence terminated
127130
w.ansi = false
128131
}
129-
if w.lastAnsi.String() == "\x1b[0m" {
132+
if c == 'm' && strings.HasSuffix(w.lastAnsi.String(), "\x1b[0m") {
130133
w.lastAnsi.Reset()
131134
}
132135
} else if inGroup(w.Newline, c) {

0 commit comments

Comments
 (0)