Skip to content

Commit

Permalink
Fix: prevent prefix color change (#119)
Browse files Browse the repository at this point in the history
* fix: prevent prefix color change by output

* fix: use l.currentColor

---------

Co-authored-by: ras0q <[email protected]>
  • Loading branch information
ras0q and ras0q authored Aug 1, 2024
1 parent 583d236 commit 1cc75e6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
44 changes: 33 additions & 11 deletions run/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@ package run

import (
"bytes"
"fmt"
"io"
"regexp"
)

var newLine = byte('\n')
var (
prefixColor = []byte("\033[0m")
delimiter = []byte("| ")
newLine = byte('\n')
colorRegexp = regexp.MustCompile(`\033\[[0-9;]*m`)
)

type prefixLogger struct {
w io.Writer
buf *bytes.Buffer
prefix []byte
w io.Writer
buf *bytes.Buffer
prefix []byte
currentColor []byte
}

func newPrefixLogger(w io.Writer, prefix string) *prefixLogger {
streamer := &prefixLogger{
w: w,
buf: bytes.NewBuffer([]byte("")),
}
p := make([]byte, 0, len(prefixColor)+len(prefix)+len(delimiter))
if prefix != "" {
streamer.prefix = []byte(fmt.Sprintf("%s| ", prefix))
p = append(p, prefixColor...)
p = append(p, []byte(prefix)...)
p = append(p, delimiter...)
}

streamer := &prefixLogger{
w: w,
buf: bytes.NewBuffer([]byte("")),
prefix: p,
currentColor: []byte{},
}

return streamer
Expand Down Expand Up @@ -61,6 +73,11 @@ func (l *prefixLogger) outputLines() error {
if err := l.out(line); err != nil {
return err
}

colors := colorRegexp.FindAll(line, -1)
if len(colors) > 0 {
l.currentColor = colors[len(colors)-1]
}
} else {
// put back into buffer, it's not a complete line yet
// Close() or Flush() have to be used to flush out
Expand Down Expand Up @@ -88,6 +105,11 @@ func (l *prefixLogger) out(p []byte) error {
return nil
}

_, err := l.w.Write(append(l.prefix, p...))
s := make([]byte, 0, len(l.prefix)+len(l.currentColor)+len(p))
s = append(s, l.prefix...)
s = append(s, l.currentColor...)
s = append(s, p...)

_, err := l.w.Write(s)
return err
}
39 changes: 20 additions & 19 deletions run/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,9 @@ package run

import (
"bytes"
"strings"
"testing"
)

func setExpect(prefixB []byte, s string) string {
prefix := string(prefixB)
if len(prefix) == 0 {
return s
}
if s == "" {
return s
}

s = strings.ReplaceAll(s, "\n", "\n"+prefix)
s = strings.TrimSuffix(s, prefix)

return prefix + s
}

func TestPrefixLogger_Write(t *testing.T) {
tests := map[string]string{"empty": "", "prefix": "prefix"}

Expand All @@ -35,21 +19,38 @@ func TestPrefixLogger_Write(t *testing.T) {
l.Write([]byte("hello world"))

// No new line so it should be empty
expect := setExpect(l.prefix, "")
expect := ""
if w.String() != expect {
t.Errorf("got %v, want %v", w.String(), expect)
}

// Write a new line
l.Write([]byte("\n"))
expect = setExpect(l.prefix, "hello world\n")
expect += string(l.prefix) + "hello world\n"
if w.String() != expect {
t.Errorf("got %v, want %v", w.String(), expect)
}

// Write a line with a new line
l.Write([]byte("foo bar\n"))
expect = setExpect(l.prefix, "hello world\nfoo bar\n")
// Each line is prefixed
expect += string(l.prefix) + "foo bar\n"
if w.String() != expect {
t.Errorf("got %v, want %v", w.String(), expect)
}

// Write a line with a new line with a red color
red := "\033[31m"
reset := "\033[m"
l.Write([]byte(
red + "this line is red\n" +
"this line is still red" + reset + "\n" +
"this line is reset\n",
))
// Each line specifies the output color
expect += string(l.prefix) + red + "this line is red\n"
expect += string(l.prefix) + red + "this line is still red" + reset + "\n"
expect += string(l.prefix) + reset + "this line is reset\n"
if w.String() != expect {
t.Errorf("got %v, want %v", w.String(), expect)
}
Expand Down

0 comments on commit 1cc75e6

Please sign in to comment.