1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-14 08:16:06 -05:00
OpenDiablo2/d2core/d2term/terminal_logger.go
Gürkan Kaymak ae6bfb839e
Lint fixes for the following packages (#505)
* fixed lint issues of the package d2core/d2inventory

* fixed lint issues of the d2script package

* fixed lint issues of the d2common/d2interface package

* fixed lint issues of the d2common/d2resource package

* fixed lint issues of the d2core/d2term package

* fixed conflict errors
2020-06-30 17:04:41 -04:00

47 lines
780 B
Go

package d2term
import (
"bufio"
"bytes"
"io"
"strings"
)
type terminalLogger struct {
terminal *terminal
buffer bytes.Buffer
writer io.Writer
}
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()
if err != nil {
return n, err
}
line := string(termBytes)
lineLower := strings.ToLower(line)
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)
}
return tl.writer.Write(p)
}
func (tl *terminalLogger) BindToTerminal(t *terminal) {
tl.terminal = t
}