فهرست منبع

Write as bytes

Yasuhiro Matsumoto 9 سال پیش
والد
کامیت
2f10edf433
2فایلهای تغییر یافته به همراه27 افزوده شده و 25 حذف شده
  1. 15 14
      colorable_windows.go
  2. 12 11
      noncolorable.go

+ 15 - 14
colorable_windows.go

@@ -2,7 +2,6 @@ package colorable
 
 import (
 	"bytes"
-	"fmt"
 	"io"
 	"math"
 	"os"
@@ -362,6 +361,7 @@ func (w *Writer) Write(data []byte) (n int, err error) {
 	procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
 
 	er := bytes.NewBuffer(data)
+	var bw [1]byte
 loop:
 	for {
 		r1, _, err := procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
@@ -369,32 +369,33 @@ loop:
 			break loop
 		}
 
-		c1, _, err := er.ReadRune()
+		c1, err := er.ReadByte()
 		if err != nil {
 			break loop
 		}
 		if c1 != 0x1b {
-			fmt.Fprint(w.out, string(c1))
+			bw[0] = c1
+			w.out.Write(bw[:])
 			continue
 		}
-		c2, _, err := er.ReadRune()
+		c2, err := er.ReadByte()
 		if err != nil {
-			w.lastbuf.WriteRune(c1)
+			w.lastbuf.WriteByte(c1)
 			break loop
 		}
 		if c2 != 0x5b {
-			w.lastbuf.WriteRune(c1)
-			w.lastbuf.WriteRune(c2)
+			w.lastbuf.WriteByte(c1)
+			w.lastbuf.WriteByte(c2)
 			continue
 		}
 
 		var buf bytes.Buffer
-		var m rune
+		var m byte
 		for {
-			c, _, err := er.ReadRune()
+			c, err := er.ReadByte()
 			if err != nil {
-				w.lastbuf.WriteRune(c1)
-				w.lastbuf.WriteRune(c2)
+				w.lastbuf.WriteByte(c1)
+				w.lastbuf.WriteByte(c2)
 				w.lastbuf.Write(buf.Bytes())
 				break loop
 			}
@@ -466,7 +467,7 @@ loop:
 				continue
 			}
 			procGetConsoleScreenBufferInfo.Call(uintptr(w.handle), uintptr(unsafe.Pointer(&csbi)))
-			csbi.cursorPosition.x = short(n-1)
+			csbi.cursorPosition.x = short(n - 1)
 			procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
 		case 'H':
 			token := strings.Split(buf.String(), ";")
@@ -481,8 +482,8 @@ loop:
 			if err != nil {
 				continue
 			}
-			csbi.cursorPosition.x = short(n2-1)
-			csbi.cursorPosition.y = short(n1-1)
+			csbi.cursorPosition.x = short(n2 - 1)
+			csbi.cursorPosition.y = short(n1 - 1)
 			procSetConsoleCursorPosition.Call(uintptr(w.handle), *(*uintptr)(unsafe.Pointer(&csbi.cursorPosition)))
 		case 'J':
 			n, err := strconv.Atoi(buf.String())

+ 12 - 11
noncolorable.go

@@ -2,7 +2,6 @@ package colorable
 
 import (
 	"bytes"
-	"fmt"
 	"io"
 )
 
@@ -16,34 +15,36 @@ func NewNonColorable(w io.Writer) io.Writer {
 }
 
 func (w *NonColorable) Write(data []byte) (n int, err error) {
-	er := bytes.NewBuffer(data)
+	er := bytes.NewReader(data)
+	var bw [1]byte
 loop:
 	for {
-		c1, _, err := er.ReadRune()
+		c1, err := er.ReadByte()
 		if err != nil {
 			break loop
 		}
 		if c1 != 0x1b {
-			fmt.Fprint(w.out, string(c1))
+			bw[0] = c1
+			w.out.Write(bw[:])
 			continue
 		}
-		c2, _, err := er.ReadRune()
+		c2, err := er.ReadByte()
 		if err != nil {
-			w.lastbuf.WriteRune(c1)
+			w.lastbuf.WriteByte(c1)
 			break loop
 		}
 		if c2 != 0x5b {
-			w.lastbuf.WriteRune(c1)
-			w.lastbuf.WriteRune(c2)
+			w.lastbuf.WriteByte(c1)
+			w.lastbuf.WriteByte(c2)
 			continue
 		}
 
 		var buf bytes.Buffer
 		for {
-			c, _, err := er.ReadRune()
+			c, err := er.ReadByte()
 			if err != nil {
-				w.lastbuf.WriteRune(c1)
-				w.lastbuf.WriteRune(c2)
+				w.lastbuf.WriteByte(c1)
+				w.lastbuf.WriteByte(c2)
 				w.lastbuf.Write(buf.Bytes())
 				break loop
 			}