1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-18 02:16:23 -05:00
OpenDiablo2/d2core/d2term/terminal_logger.go
2020-12-22 15:18:14 +01:00

47 lines
767 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.Errorf(line)
case strings.Index(lineLower, "warning") > 0:
tl.terminal.Warningf(line)
default:
tl.terminal.Printf(line)
}
return tl.writer.Write(p)
}
func (tl *terminalLogger) BindToTerminal(t *Terminal) {
tl.terminal = t
}