Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds scribe.WithPrefix option for scribe.Writer #353

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions scribe/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,23 @@ func WithIndent(indent int) Option {
}
}

// WithPrefix takes a prefix string and returns an Option which can be passed
// in while creating a new Writer to configure a prefix to be prepended to the
// output of the Writer.
func WithPrefix(prefix string) Option {
return func(l Writer) Writer {
l.prefix = prefix
return l
}
}

// A Writer conforms to the io.Writer interface and allows for configuration of
// output from the writter such as the color or indentation through Options.
type Writer struct {
writer io.Writer
color Color
indent int
prefix string
linestart bool
}

Expand Down Expand Up @@ -72,6 +83,8 @@ func (w *Writer) Write(b []byte) (int, error) {
var indentedLines [][]byte
for index, line := range lines {
if !(index == 0 && !w.linestart) {
line = append([]byte(w.prefix), line...)

for i := 0; i < w.indent; i++ {
line = append([]byte(" "), line...)
}
Expand Down
35 changes: 31 additions & 4 deletions scribe/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,54 @@ func testWriter(t *testing.T, context spec.G, it spec.S) {
})
})

context("when the writer has a prefix", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithPrefix("[some-prefix] "))
})

it("prints to the writer with the given prefix", func() {
_, err := writer.Write([]byte("some-text\nother-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("[some-prefix] some-text\n[some-prefix] other-text"))
})

context("when sequential write inputs are not newline terminated", func() {
it("handles the indentation correctly", func() {
_, err := writer.Write([]byte("some-text"))
Expect(err).NotTo(HaveOccurred())

_, err = writer.Write([]byte(" followed by other-text\n"))
Expect(err).NotTo(HaveOccurred())

_, err = writer.Write([]byte("followed by\neven-more-text\n"))
Expect(err).NotTo(HaveOccurred())

Expect(buffer.String()).To(Equal("[some-prefix] some-text followed by other-text\n[some-prefix] followed by\n[some-prefix] even-more-text\n"))
})
})
})

context("when the writer has a return prefix", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.RedColor), scribe.WithIndent(2))
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.RedColor), scribe.WithIndent(2), scribe.WithPrefix("[some-prefix] "))
})

it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("\rsome-text"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\r\x1b[0;38;5;1m some-text\x1b[0m"))
Expect(buffer.String()).To(Equal("\r\x1b[0;38;5;1m [some-prefix] some-text\x1b[0m"))
})
})

context("when the writer has a newline suffix", func() {
it.Before(func() {
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.RedColor), scribe.WithIndent(2))
writer = scribe.NewWriter(buffer, scribe.WithColor(scribe.RedColor), scribe.WithIndent(2), scribe.WithPrefix("[some-prefix] "))
})

it("prints to the writer with the correct indentation", func() {
_, err := writer.Write([]byte("some-text\n"))
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(Equal("\x1b[0;38;5;1m some-text\x1b[0m\n"))
Expect(buffer.String()).To(Equal("\x1b[0;38;5;1m [some-prefix] some-text\x1b[0m\n"))
})
})

Expand Down