fix: include escape codes in byte counts from Fprint, Fprintf#282
fix: include escape codes in byte counts from Fprint, Fprintf#282fatih merged 1 commit intofatih:mainfrom
Fprint, Fprintf#282Conversation
9264c7c to
f8f0652
Compare
Fix the `n` byte count returned from `Color.Fprint` and `Color.Fprintf` to include the bytes in ANSI escape sequences. Both methods delegate to `SetWriter` and `UnsetWriter` methods to start and stop the excape sequence. Since both these methods are public and do not return the byte counts needed, I extracted private versions of these methods `setWriter` and `unsetWriter`, and redirected the `Fprint` and `Fprintf` to get accurate byte counts without breaking API compatibility. Finally, `unsetWriter()` is no longer deferred. If we encounter an error at any point, stop writing and return the error.
f8f0652 to
cbda2c3
Compare
|
Ping. Is there any interest in this PR? I came across this bug while trying to migrate some code to FPrint so we can use io.Writer in tests instead of stdout. |
|
@qualidafial thanks for the test and verification. This looks something we can merge. Did you encountered any issue before? If yes what was the use case you saw ? |
|
We use As a workaround, I wrapped the passed-in writer on our side in a counting writer implementation, rather than relying on the byte counts from type countingWriter struct {
writer io.Writer
N int
}
func (w *countingWriter) Write(p []byte) (int, error) {
n, err := w.writer.Write(p)
w.N += n
return n, err
} |
|
Thank you for the fix @qualidafial. |
|
My pleasure! Thanks @fatih for sharing and maintaining this library! |
Fix the
nbyte count returned fromColor.FprintandColor.Fprintfto include the bytes in ANSI escape sequences.Both methods delegate to
SetWriterandUnsetWritermethods to start and stop the excape sequence. Since both these methods are public and do not return the byte counts needed, I extracted private versions of these methodssetWriterandunsetWriter, and redirected theFprintandFprintfto get accurate byte counts without breaking API compatibility.Finally,
unsetWriter()is no longer deferred. If we encounter an error at any point, stop writing and return the error.