2020-06-28 21:40:52 -04:00
|
|
|
package d2term
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type terminalLogger struct {
|
|
|
|
terminal *terminal
|
2020-06-30 17:04:41 -04:00
|
|
|
buffer bytes.Buffer
|
|
|
|
writer io.Writer
|
2020-06-28 21:40:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tl *terminalLogger) Write(p []byte) (int, error) {
|
|
|
|
n, err := tl.buffer.Write(p)
|
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(&tl.buffer)
|
|
|
|
termBytes, _, err := reader.ReadLine()
|
2020-06-30 17:04:41 -04:00
|
|
|
|
2020-06-28 21:40:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
line := string(termBytes)
|
2020-06-28 21:40:52 -04:00
|
|
|
lineLower := strings.ToLower(line)
|
|
|
|
|
2020-06-30 17:04:41 -04:00
|
|
|
switch {
|
|
|
|
case strings.Index(lineLower, "error") > 0:
|
|
|
|
tl.terminal.OutputErrorf(line)
|
|
|
|
case strings.Index(lineLower, "warning") > 0:
|
|
|
|
tl.terminal.OutputWarningf(line)
|
|
|
|
default:
|
|
|
|
tl.terminal.Outputf(line)
|
2020-06-28 21:40:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return tl.writer.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tl *terminalLogger) BindToTerminal(t *terminal) {
|
|
|
|
tl.terminal = t
|
|
|
|
}
|