summaryrefslogtreecommitdiff
path: root/vendor/github.com/Azure/go-ansiterm/parser.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/Azure/go-ansiterm/parser.go')
-rw-r--r--vendor/github.com/Azure/go-ansiterm/parser.go99
1 files changed, 57 insertions, 42 deletions
diff --git a/vendor/github.com/Azure/go-ansiterm/parser.go b/vendor/github.com/Azure/go-ansiterm/parser.go
index 3286a9cb5..03cec7ada 100644
--- a/vendor/github.com/Azure/go-ansiterm/parser.go
+++ b/vendor/github.com/Azure/go-ansiterm/parser.go
@@ -2,14 +2,10 @@ package ansiterm
import (
"errors"
- "io/ioutil"
+ "log"
"os"
-
- "github.com/sirupsen/logrus"
)
-var logger *logrus.Logger
-
type AnsiParser struct {
currState state
eventHandler AnsiEventHandler
@@ -23,50 +19,69 @@ type AnsiParser struct {
ground state
oscString state
stateMap []state
+
+ logf func(string, ...interface{})
}
-func CreateParser(initialState string, evtHandler AnsiEventHandler) *AnsiParser {
- logFile := ioutil.Discard
+type Option func(*AnsiParser)
- if isDebugEnv := os.Getenv(LogEnv); isDebugEnv == "1" {
- logFile, _ = os.Create("ansiParser.log")
- }
-
- logger = &logrus.Logger{
- Out: logFile,
- Formatter: new(logrus.TextFormatter),
- Level: logrus.InfoLevel,
+func WithLogf(f func(string, ...interface{})) Option {
+ return func(ap *AnsiParser) {
+ ap.logf = f
}
+}
- parser := &AnsiParser{
+func CreateParser(initialState string, evtHandler AnsiEventHandler, opts ...Option) *AnsiParser {
+ ap := &AnsiParser{
eventHandler: evtHandler,
context: &ansiContext{},
}
+ for _, o := range opts {
+ o(ap)
+ }
+
+ if isDebugEnv := os.Getenv(LogEnv); isDebugEnv == "1" {
+ logFile, _ := os.Create("ansiParser.log")
+ logger := log.New(logFile, "", log.LstdFlags)
+ if ap.logf != nil {
+ l := ap.logf
+ ap.logf = func(s string, v ...interface{}) {
+ l(s, v...)
+ logger.Printf(s, v...)
+ }
+ } else {
+ ap.logf = logger.Printf
+ }
+ }
+
+ if ap.logf == nil {
+ ap.logf = func(string, ...interface{}) {}
+ }
- parser.csiEntry = csiEntryState{baseState{name: "CsiEntry", parser: parser}}
- parser.csiParam = csiParamState{baseState{name: "CsiParam", parser: parser}}
- parser.dcsEntry = dcsEntryState{baseState{name: "DcsEntry", parser: parser}}
- parser.escape = escapeState{baseState{name: "Escape", parser: parser}}
- parser.escapeIntermediate = escapeIntermediateState{baseState{name: "EscapeIntermediate", parser: parser}}
- parser.error = errorState{baseState{name: "Error", parser: parser}}
- parser.ground = groundState{baseState{name: "Ground", parser: parser}}
- parser.oscString = oscStringState{baseState{name: "OscString", parser: parser}}
-
- parser.stateMap = []state{
- parser.csiEntry,
- parser.csiParam,
- parser.dcsEntry,
- parser.escape,
- parser.escapeIntermediate,
- parser.error,
- parser.ground,
- parser.oscString,
+ ap.csiEntry = csiEntryState{baseState{name: "CsiEntry", parser: ap}}
+ ap.csiParam = csiParamState{baseState{name: "CsiParam", parser: ap}}
+ ap.dcsEntry = dcsEntryState{baseState{name: "DcsEntry", parser: ap}}
+ ap.escape = escapeState{baseState{name: "Escape", parser: ap}}
+ ap.escapeIntermediate = escapeIntermediateState{baseState{name: "EscapeIntermediate", parser: ap}}
+ ap.error = errorState{baseState{name: "Error", parser: ap}}
+ ap.ground = groundState{baseState{name: "Ground", parser: ap}}
+ ap.oscString = oscStringState{baseState{name: "OscString", parser: ap}}
+
+ ap.stateMap = []state{
+ ap.csiEntry,
+ ap.csiParam,
+ ap.dcsEntry,
+ ap.escape,
+ ap.escapeIntermediate,
+ ap.error,
+ ap.ground,
+ ap.oscString,
}
- parser.currState = getState(initialState, parser.stateMap)
+ ap.currState = getState(initialState, ap.stateMap)
- logger.Infof("CreateParser: parser %p", parser)
- return parser
+ ap.logf("CreateParser: parser %p", ap)
+ return ap
}
func getState(name string, states []state) state {
@@ -97,7 +112,7 @@ func (ap *AnsiParser) handle(b byte) error {
}
if newState == nil {
- logger.Warning("newState is nil")
+ ap.logf("WARNING: newState is nil")
return errors.New("New state of 'nil' is invalid.")
}
@@ -111,23 +126,23 @@ func (ap *AnsiParser) handle(b byte) error {
}
func (ap *AnsiParser) changeState(newState state) error {
- logger.Infof("ChangeState %s --> %s", ap.currState.Name(), newState.Name())
+ ap.logf("ChangeState %s --> %s", ap.currState.Name(), newState.Name())
// Exit old state
if err := ap.currState.Exit(); err != nil {
- logger.Infof("Exit state '%s' failed with : '%v'", ap.currState.Name(), err)
+ ap.logf("Exit state '%s' failed with : '%v'", ap.currState.Name(), err)
return err
}
// Perform transition action
if err := ap.currState.Transition(newState); err != nil {
- logger.Infof("Transition from '%s' to '%s' failed with: '%v'", ap.currState.Name(), newState.Name, err)
+ ap.logf("Transition from '%s' to '%s' failed with: '%v'", ap.currState.Name(), newState.Name, err)
return err
}
// Enter new state
if err := newState.Enter(); err != nil {
- logger.Infof("Enter state '%s' failed with: '%v'", newState.Name(), err)
+ ap.logf("Enter state '%s' failed with: '%v'", newState.Name(), err)
return err
}