| 1 |
package godo |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
|
| 8 |
"github.com/mgutz/ansi" |
| 9 |
) |
| 10 |
|
| 11 |
type fileWrapper struct { |
| 12 |
file *os.File |
| 13 |
buf *bytes.Buffer |
| 14 |
readLines string |
| 15 |
|
| 16 |
recorder *bytes.Buffer |
| 17 |
|
| 18 |
|
| 19 |
colorStart string |
| 20 |
} |
| 21 |
|
| 22 |
func newFileWrapper(file *os.File, recorder *bytes.Buffer, color string) *fileWrapper { |
| 23 |
streamer := &fileWrapper{ |
| 24 |
file: file, |
| 25 |
buf: bytes.NewBufferString(""), |
| 26 |
recorder: recorder, |
| 27 |
colorStart: color, |
| 28 |
} |
| 29 |
|
| 30 |
return streamer |
| 31 |
} |
| 32 |
|
| 33 |
func (l *fileWrapper) Write(p []byte) (n int, err error) { |
| 34 |
if n, err = l.recorder.Write(p); err != nil { |
| 35 |
return |
| 36 |
} |
| 37 |
|
| 38 |
err = l.out(string(p)) |
| 39 |
return |
| 40 |
} |
| 41 |
|
| 42 |
func (l *fileWrapper) WriteString(s string) (n int, err error) { |
| 43 |
if n, err = l.recorder.WriteString(s); err != nil { |
| 44 |
return |
| 45 |
} |
| 46 |
|
| 47 |
err = l.out(s) |
| 48 |
return |
| 49 |
} |
| 50 |
|
| 51 |
func (l *fileWrapper) Close() error { |
| 52 |
l.buf = bytes.NewBuffer([]byte("")) |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
func (l *fileWrapper) out(str string) (err error) { |
| 57 |
|
| 58 |
if l.colorStart != "" { |
| 59 |
fmt.Fprint(l.file, l.colorStart) |
| 60 |
fmt.Fprint(l.file, str) |
| 61 |
fmt.Fprint(l.file, ansi.Reset) |
| 62 |
} else { |
| 63 |
fmt.Fprint(l.file, str) |
| 64 |
} |
| 65 |
|
| 66 |
return nil |
| 67 |
} |