Skip to content

Commit

Permalink
Merge pull request #56 from secDre4mer/master
Browse files Browse the repository at this point in the history
Write plain text in chunks
  • Loading branch information
mattn authored Sep 25, 2021
2 parents a53eda5 + 39a0e9a commit 6a118b2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
10 changes: 7 additions & 3 deletions colorable_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,18 +452,22 @@ func (w *Writer) Write(data []byte) (n int, err error) {
} else {
er = bytes.NewReader(data)
}
var bw [1]byte
var plaintext bytes.Buffer
loop:
for {
c1, err := er.ReadByte()
if err != nil {
plaintext.WriteTo(w.out)
break loop
}
if c1 != 0x1b {
bw[0] = c1
w.out.Write(bw[:])
plaintext.WriteByte(c1)
continue
}
_, err = plaintext.WriteTo(w.out)
if err != nil {
break loop
}
c2, err := er.ReadByte()
if err != nil {
break loop
Expand Down
13 changes: 7 additions & 6 deletions noncolorable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ func NewNonColorable(w io.Writer) io.Writer {
// Write writes data on console
func (w *NonColorable) Write(data []byte) (n int, err error) {
er := bytes.NewReader(data)
var bw [1]byte
var plaintext bytes.Buffer
loop:
for {
c1, err := er.ReadByte()
if err != nil {
plaintext.WriteTo(w.out)
break loop
}
if c1 != 0x1b {
bw[0] = c1
_, err = w.out.Write(bw[:])
if err != nil {
break loop
}
plaintext.WriteByte(c1)
continue
}
_, err = plaintext.WriteTo(w.out)
if err != nil {
break loop
}
c2, err := er.ReadByte()
if err != nil {
break loop
Expand Down

0 comments on commit 6a118b2

Please sign in to comment.