From 7fe486d64cc14f15e27532ec1cd845112a0c5b2a Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Mon, 25 Sep 2017 07:09:45 +0700 Subject: [PATCH] Remove logrus dependency, and add functional options to specify logger This changeset removes the non-standard dependency to logrus, leaving the only dependencies used in ansiterm and winterm as Go standard lib packages. This also has the added benefit of removing a superfulous package level variable (logger) in both ansiterm and winterm. The changes made here introduce a backwards compatible change to the API signatures of ansiterm.CreateParser and winterm.CreateWinEventHandler by introducing the optional functional parameters pattern on both. Additionally, both ansiterm and winterm packages receive a WithLogf() func to specify a logging func to use, which can be passed a func with the standard Printf signature, ie: func(string, ...interface{}). This maintains the existing behavior of checking for the environment variable DEBUG_TERMINAL, and writing to the files ansiParser.log and winEventHandler.log. For ansiterm/winterm, if a logging func has been specified as well as having DEBUG_TERMINAL set, then both the respective *.log file and the passed logging func will receive the data. Note: for simplicity purposes, the single instance in ansiterm where there was a "warning" and "error" was logged has been changed to use the single log, but the messages have been prefixed with WARNING and ERROR respectively. This was done entirely for simplicity reasons. Additionally 2 other logging calls have been removed where it was prudent to do so, and one other logging line was moved to a different location in order to avoid changing some utility funcs. --- csi_entry_state.go | 4 +- csi_param_state.go | 4 +- escape_intermediate_state.go | 4 +- escape_state.go | 4 +- osc_string_state.go | 2 +- parser.go | 99 ++++++++++++++++------------- parser_action_helpers.go | 4 -- parser_actions.go | 17 +++-- winterm/cursor_helpers.go | 2 +- winterm/scroll_helper.go | 4 +- winterm/win_event_handler.go | 117 ++++++++++++++++++++--------------- 11 files changed, 143 insertions(+), 118 deletions(-) diff --git a/csi_entry_state.go b/csi_entry_state.go index 1bd6057..bcbe00d 100644 --- a/csi_entry_state.go +++ b/csi_entry_state.go @@ -5,7 +5,7 @@ type csiEntryState struct { } func (csiState csiEntryState) Handle(b byte) (s state, e error) { - logger.Infof("CsiEntry::Handle %#x", b) + csiState.parser.logf("CsiEntry::Handle %#x", b) nextState, err := csiState.baseState.Handle(b) if nextState != nil || err != nil { @@ -25,7 +25,7 @@ func (csiState csiEntryState) Handle(b byte) (s state, e error) { } func (csiState csiEntryState) Transition(s state) error { - logger.Infof("CsiEntry::Transition %s --> %s", csiState.Name(), s.Name()) + csiState.parser.logf("CsiEntry::Transition %s --> %s", csiState.Name(), s.Name()) csiState.baseState.Transition(s) switch s { diff --git a/csi_param_state.go b/csi_param_state.go index 4be35c5..7ed5e01 100644 --- a/csi_param_state.go +++ b/csi_param_state.go @@ -5,7 +5,7 @@ type csiParamState struct { } func (csiState csiParamState) Handle(b byte) (s state, e error) { - logger.Infof("CsiParam::Handle %#x", b) + csiState.parser.logf("CsiParam::Handle %#x", b) nextState, err := csiState.baseState.Handle(b) if nextState != nil || err != nil { @@ -26,7 +26,7 @@ func (csiState csiParamState) Handle(b byte) (s state, e error) { } func (csiState csiParamState) Transition(s state) error { - logger.Infof("CsiParam::Transition %s --> %s", csiState.Name(), s.Name()) + csiState.parser.logf("CsiParam::Transition %s --> %s", csiState.Name(), s.Name()) csiState.baseState.Transition(s) switch s { diff --git a/escape_intermediate_state.go b/escape_intermediate_state.go index 2189eb6..1c719db 100644 --- a/escape_intermediate_state.go +++ b/escape_intermediate_state.go @@ -5,7 +5,7 @@ type escapeIntermediateState struct { } func (escState escapeIntermediateState) Handle(b byte) (s state, e error) { - logger.Infof("escapeIntermediateState::Handle %#x", b) + escState.parser.logf("escapeIntermediateState::Handle %#x", b) nextState, err := escState.baseState.Handle(b) if nextState != nil || err != nil { return nextState, err @@ -24,7 +24,7 @@ func (escState escapeIntermediateState) Handle(b byte) (s state, e error) { } func (escState escapeIntermediateState) Transition(s state) error { - logger.Infof("escapeIntermediateState::Transition %s --> %s", escState.Name(), s.Name()) + escState.parser.logf("escapeIntermediateState::Transition %s --> %s", escState.Name(), s.Name()) escState.baseState.Transition(s) switch s { diff --git a/escape_state.go b/escape_state.go index 7b1b9ad..6390abd 100644 --- a/escape_state.go +++ b/escape_state.go @@ -5,7 +5,7 @@ type escapeState struct { } func (escState escapeState) Handle(b byte) (s state, e error) { - logger.Infof("escapeState::Handle %#x", b) + escState.parser.logf("escapeState::Handle %#x", b) nextState, err := escState.baseState.Handle(b) if nextState != nil || err != nil { return nextState, err @@ -28,7 +28,7 @@ func (escState escapeState) Handle(b byte) (s state, e error) { } func (escState escapeState) Transition(s state) error { - logger.Infof("Escape::Transition %s --> %s", escState.Name(), s.Name()) + escState.parser.logf("Escape::Transition %s --> %s", escState.Name(), s.Name()) escState.baseState.Transition(s) switch s { diff --git a/osc_string_state.go b/osc_string_state.go index 24062d4..593b10a 100644 --- a/osc_string_state.go +++ b/osc_string_state.go @@ -5,7 +5,7 @@ type oscStringState struct { } func (oscState oscStringState) Handle(b byte) (s state, e error) { - logger.Infof("OscString::Handle %#x", b) + oscState.parser.logf("OscString::Handle %#x", b) nextState, err := oscState.baseState.Handle(b) if nextState != nil || err != nil { return nextState, err diff --git a/parser.go b/parser.go index 3286a9c..03cec7a 100644 --- a/parser.go +++ b/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 } diff --git a/parser_action_helpers.go b/parser_action_helpers.go index 8b69a67..de0a1f9 100644 --- a/parser_action_helpers.go +++ b/parser_action_helpers.go @@ -27,7 +27,6 @@ func parseParams(bytes []byte) ([]string, error) { params = append(params, s) } - logger.Infof("Parsed params: %v with length: %d", params, len(params)) return params, nil } @@ -37,7 +36,6 @@ func parseCmd(context ansiContext) (string, error) { func getInt(params []string, dflt int) int { i := getInts(params, 1, dflt)[0] - logger.Infof("getInt: %v", i) return i } @@ -60,8 +58,6 @@ func getInts(params []string, minCount int, dflt int) []int { } } - logger.Infof("getInts: %v", ints) - return ints } diff --git a/parser_actions.go b/parser_actions.go index 58750a2..0bb5e51 100644 --- a/parser_actions.go +++ b/parser_actions.go @@ -1,19 +1,15 @@ package ansiterm -import ( - "fmt" -) - func (ap *AnsiParser) collectParam() error { currChar := ap.context.currentChar - logger.Infof("collectParam %#x", currChar) + ap.logf("collectParam %#x", currChar) ap.context.paramBuffer = append(ap.context.paramBuffer, currChar) return nil } func (ap *AnsiParser) collectInter() error { currChar := ap.context.currentChar - logger.Infof("collectInter %#x", currChar) + ap.logf("collectInter %#x", currChar) ap.context.paramBuffer = append(ap.context.interBuffer, currChar) return nil } @@ -21,8 +17,8 @@ func (ap *AnsiParser) collectInter() error { func (ap *AnsiParser) escDispatch() error { cmd, _ := parseCmd(*ap.context) intermeds := ap.context.interBuffer - logger.Infof("escDispatch currentChar: %#x", ap.context.currentChar) - logger.Infof("escDispatch: %v(%v)", cmd, intermeds) + ap.logf("escDispatch currentChar: %#x", ap.context.currentChar) + ap.logf("escDispatch: %v(%v)", cmd, intermeds) switch cmd { case "D": // IND @@ -43,8 +39,9 @@ func (ap *AnsiParser) escDispatch() error { func (ap *AnsiParser) csiDispatch() error { cmd, _ := parseCmd(*ap.context) params, _ := parseParams(ap.context.paramBuffer) + ap.logf("Parsed params: %v with length: %d", params, len(params)) - logger.Infof("csiDispatch: %v(%v)", cmd, params) + ap.logf("csiDispatch: %v(%v)", cmd, params) switch cmd { case "@": @@ -102,7 +99,7 @@ func (ap *AnsiParser) csiDispatch() error { top, bottom := ints[0], ints[1] return ap.eventHandler.DECSTBM(top, bottom) default: - logger.Errorf(fmt.Sprintf("Unsupported CSI command: '%s', with full context: %v", cmd, ap.context)) + ap.logf("ERROR: Unsupported CSI command: '%s', with full context: %v", cmd, ap.context) return nil } diff --git a/winterm/cursor_helpers.go b/winterm/cursor_helpers.go index f015723..3ee06ea 100644 --- a/winterm/cursor_helpers.go +++ b/winterm/cursor_helpers.go @@ -34,7 +34,7 @@ func (h *windowsAnsiEventHandler) setCursorPosition(position COORD, window SMALL if err != nil { return err } - logger.Infof("Cursor position set: (%d, %d)", position.X, position.Y) + h.logf("Cursor position set: (%d, %d)", position.X, position.Y) return err } diff --git a/winterm/scroll_helper.go b/winterm/scroll_helper.go index 706d270..2d27fa1 100644 --- a/winterm/scroll_helper.go +++ b/winterm/scroll_helper.go @@ -50,8 +50,8 @@ func (h *windowsAnsiEventHandler) insertLines(param int) error { // scroll scrolls the provided scroll region by param lines. The scroll region is in buffer coordinates. func (h *windowsAnsiEventHandler) scroll(param int, sr scrollRegion, info *CONSOLE_SCREEN_BUFFER_INFO) error { - logger.Infof("scroll: scrollTop: %d, scrollBottom: %d", sr.top, sr.bottom) - logger.Infof("scroll: windowTop: %d, windowBottom: %d", info.Window.Top, info.Window.Bottom) + h.logf("scroll: scrollTop: %d, scrollBottom: %d", sr.top, sr.bottom) + h.logf("scroll: windowTop: %d, windowBottom: %d", info.Window.Top, info.Window.Bottom) // Copy from and clip to the scroll region (full buffer width) scrollRect := SMALL_RECT{ diff --git a/winterm/win_event_handler.go b/winterm/win_event_handler.go index 48998bb..2d40fb7 100644 --- a/winterm/win_event_handler.go +++ b/winterm/win_event_handler.go @@ -4,16 +4,13 @@ package winterm import ( "bytes" - "io/ioutil" + "log" "os" "strconv" "github.com/Azure/go-ansiterm" - "github.com/sirupsen/logrus" ) -var logger *logrus.Logger - type windowsAnsiEventHandler struct { fd uintptr file *os.File @@ -28,32 +25,52 @@ type windowsAnsiEventHandler struct { marginByte byte curInfo *CONSOLE_SCREEN_BUFFER_INFO curPos COORD + logf func(string, ...interface{}) } -func CreateWinEventHandler(fd uintptr, file *os.File) ansiterm.AnsiEventHandler { - logFile := ioutil.Discard - - if isDebugEnv := os.Getenv(ansiterm.LogEnv); isDebugEnv == "1" { - logFile, _ = os.Create("winEventHandler.log") - } +type Option func(*windowsAnsiEventHandler) - logger = &logrus.Logger{ - Out: logFile, - Formatter: new(logrus.TextFormatter), - Level: logrus.DebugLevel, +func WithLogf(f func(string, ...interface{})) Option { + return func(w *windowsAnsiEventHandler) { + w.logf = f } +} +func CreateWinEventHandler(fd uintptr, file *os.File, opts ...Option) ansiterm.AnsiEventHandler { infoReset, err := GetConsoleScreenBufferInfo(fd) if err != nil { return nil } - return &windowsAnsiEventHandler{ + h := &windowsAnsiEventHandler{ fd: fd, file: file, infoReset: infoReset, attributes: infoReset.Attributes, } + for _, o := range opts { + o(h) + } + + if isDebugEnv := os.Getenv(ansiterm.LogEnv); isDebugEnv == "1" { + logFile, _ := os.Create("winEventHandler.log") + logger := log.New(logFile, "", log.LstdFlags) + if h.logf != nil { + l := h.logf + h.logf = func(s string, v ...interface{}) { + l(s, v...) + logger.Printf(s, v...) + } + } else { + h.logf = logger.Printf + } + } + + if h.logf == nil { + h.logf = func(string, ...interface{}) {} + } + + return h } type scrollRegion struct { @@ -96,7 +113,7 @@ func (h *windowsAnsiEventHandler) simulateLF(includeCR bool) (bool, error) { if err := h.Flush(); err != nil { return false, err } - logger.Info("Simulating LF inside scroll region") + h.logf("Simulating LF inside scroll region") if err := h.scrollUp(1); err != nil { return false, err } @@ -119,7 +136,7 @@ func (h *windowsAnsiEventHandler) simulateLF(includeCR bool) (bool, error) { } else { // The cursor is at the bottom of the screen but outside the scroll // region. Skip the LF. - logger.Info("Simulating LF outside scroll region") + h.logf("Simulating LF outside scroll region") if includeCR { if err := h.Flush(); err != nil { return false, err @@ -151,7 +168,7 @@ func (h *windowsAnsiEventHandler) executeLF() error { if err := h.Flush(); err != nil { return err } - logger.Info("Resetting cursor position for LF without CR") + h.logf("Resetting cursor position for LF without CR") if err := SetConsoleCursorPosition(h.fd, pos); err != nil { return err } @@ -186,7 +203,7 @@ func (h *windowsAnsiEventHandler) Print(b byte) error { func (h *windowsAnsiEventHandler) Execute(b byte) error { switch b { case ansiterm.ANSI_TAB: - logger.Info("Execute(TAB)") + h.logf("Execute(TAB)") // Move to the next tab stop, but preserve auto-wrap if already set. if !h.wrapNext { pos, info, err := h.getCurrentInfo() @@ -269,7 +286,7 @@ func (h *windowsAnsiEventHandler) CUU(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("CUU: [%v]", []string{strconv.Itoa(param)}) + h.logf("CUU: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.moveCursorVertical(-param) } @@ -278,7 +295,7 @@ func (h *windowsAnsiEventHandler) CUD(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("CUD: [%v]", []string{strconv.Itoa(param)}) + h.logf("CUD: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.moveCursorVertical(param) } @@ -287,7 +304,7 @@ func (h *windowsAnsiEventHandler) CUF(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("CUF: [%v]", []string{strconv.Itoa(param)}) + h.logf("CUF: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.moveCursorHorizontal(param) } @@ -296,7 +313,7 @@ func (h *windowsAnsiEventHandler) CUB(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("CUB: [%v]", []string{strconv.Itoa(param)}) + h.logf("CUB: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.moveCursorHorizontal(-param) } @@ -305,7 +322,7 @@ func (h *windowsAnsiEventHandler) CNL(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("CNL: [%v]", []string{strconv.Itoa(param)}) + h.logf("CNL: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.moveCursorLine(param) } @@ -314,7 +331,7 @@ func (h *windowsAnsiEventHandler) CPL(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("CPL: [%v]", []string{strconv.Itoa(param)}) + h.logf("CPL: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.moveCursorLine(-param) } @@ -323,7 +340,7 @@ func (h *windowsAnsiEventHandler) CHA(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("CHA: [%v]", []string{strconv.Itoa(param)}) + h.logf("CHA: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.moveCursorColumn(param) } @@ -332,7 +349,7 @@ func (h *windowsAnsiEventHandler) VPA(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("VPA: [[%d]]", param) + h.logf("VPA: [[%d]]", param) h.clearWrap() info, err := GetConsoleScreenBufferInfo(h.fd) if err != nil { @@ -348,7 +365,7 @@ func (h *windowsAnsiEventHandler) CUP(row int, col int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("CUP: [[%d %d]]", row, col) + h.logf("CUP: [[%d %d]]", row, col) h.clearWrap() info, err := GetConsoleScreenBufferInfo(h.fd) if err != nil { @@ -364,7 +381,7 @@ func (h *windowsAnsiEventHandler) HVP(row int, col int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("HVP: [[%d %d]]", row, col) + h.logf("HVP: [[%d %d]]", row, col) h.clearWrap() return h.CUP(row, col) } @@ -373,7 +390,7 @@ func (h *windowsAnsiEventHandler) DECTCEM(visible bool) error { if err := h.Flush(); err != nil { return err } - logger.Infof("DECTCEM: [%v]", []string{strconv.FormatBool(visible)}) + h.logf("DECTCEM: [%v]", []string{strconv.FormatBool(visible)}) h.clearWrap() return nil } @@ -382,7 +399,7 @@ func (h *windowsAnsiEventHandler) DECOM(enable bool) error { if err := h.Flush(); err != nil { return err } - logger.Infof("DECOM: [%v]", []string{strconv.FormatBool(enable)}) + h.logf("DECOM: [%v]", []string{strconv.FormatBool(enable)}) h.clearWrap() h.originMode = enable return h.CUP(1, 1) @@ -392,7 +409,7 @@ func (h *windowsAnsiEventHandler) DECCOLM(use132 bool) error { if err := h.Flush(); err != nil { return err } - logger.Infof("DECCOLM: [%v]", []string{strconv.FormatBool(use132)}) + h.logf("DECCOLM: [%v]", []string{strconv.FormatBool(use132)}) h.clearWrap() if err := h.ED(2); err != nil { return err @@ -407,7 +424,7 @@ func (h *windowsAnsiEventHandler) DECCOLM(use132 bool) error { } if info.Size.X < targetWidth { if err := SetConsoleScreenBufferSize(h.fd, COORD{targetWidth, info.Size.Y}); err != nil { - logger.Info("set buffer failed:", err) + h.logf("set buffer failed: %v", err) return err } } @@ -415,12 +432,12 @@ func (h *windowsAnsiEventHandler) DECCOLM(use132 bool) error { window.Left = 0 window.Right = targetWidth - 1 if err := SetConsoleWindowInfo(h.fd, true, window); err != nil { - logger.Info("set window failed:", err) + h.logf("set window failed: %v", err) return err } if info.Size.X > targetWidth { if err := SetConsoleScreenBufferSize(h.fd, COORD{targetWidth, info.Size.Y}); err != nil { - logger.Info("set buffer failed:", err) + h.logf("set buffer failed: %v", err) return err } } @@ -431,7 +448,7 @@ func (h *windowsAnsiEventHandler) ED(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("ED: [%v]", []string{strconv.Itoa(param)}) + h.logf("ED: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() // [J -- Erases from the cursor to the end of the screen, including the cursor position. @@ -490,7 +507,7 @@ func (h *windowsAnsiEventHandler) EL(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("EL: [%v]", strconv.Itoa(param)) + h.logf("EL: [%v]", strconv.Itoa(param)) h.clearWrap() // [K -- Erases from the cursor to the end of the line, including the cursor position. @@ -531,7 +548,7 @@ func (h *windowsAnsiEventHandler) IL(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("IL: [%v]", strconv.Itoa(param)) + h.logf("IL: [%v]", strconv.Itoa(param)) h.clearWrap() return h.insertLines(param) } @@ -540,7 +557,7 @@ func (h *windowsAnsiEventHandler) DL(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("DL: [%v]", strconv.Itoa(param)) + h.logf("DL: [%v]", strconv.Itoa(param)) h.clearWrap() return h.deleteLines(param) } @@ -549,7 +566,7 @@ func (h *windowsAnsiEventHandler) ICH(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("ICH: [%v]", strconv.Itoa(param)) + h.logf("ICH: [%v]", strconv.Itoa(param)) h.clearWrap() return h.insertCharacters(param) } @@ -558,7 +575,7 @@ func (h *windowsAnsiEventHandler) DCH(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("DCH: [%v]", strconv.Itoa(param)) + h.logf("DCH: [%v]", strconv.Itoa(param)) h.clearWrap() return h.deleteCharacters(param) } @@ -572,7 +589,7 @@ func (h *windowsAnsiEventHandler) SGR(params []int) error { strings = append(strings, strconv.Itoa(v)) } - logger.Infof("SGR: [%v]", strings) + h.logf("SGR: [%v]", strings) if len(params) <= 0 { h.attributes = h.infoReset.Attributes @@ -606,7 +623,7 @@ func (h *windowsAnsiEventHandler) SU(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("SU: [%v]", []string{strconv.Itoa(param)}) + h.logf("SU: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.scrollUp(param) } @@ -615,13 +632,13 @@ func (h *windowsAnsiEventHandler) SD(param int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("SD: [%v]", []string{strconv.Itoa(param)}) + h.logf("SD: [%v]", []string{strconv.Itoa(param)}) h.clearWrap() return h.scrollDown(param) } func (h *windowsAnsiEventHandler) DA(params []string) error { - logger.Infof("DA: [%v]", params) + h.logf("DA: [%v]", params) // DA cannot be implemented because it must send data on the VT100 input stream, // which is not available to go-ansiterm. return nil @@ -631,7 +648,7 @@ func (h *windowsAnsiEventHandler) DECSTBM(top int, bottom int) error { if err := h.Flush(); err != nil { return err } - logger.Infof("DECSTBM: [%d, %d]", top, bottom) + h.logf("DECSTBM: [%d, %d]", top, bottom) // Windows is 0 indexed, Linux is 1 indexed h.sr.top = int16(top - 1) @@ -646,7 +663,7 @@ func (h *windowsAnsiEventHandler) RI() error { if err := h.Flush(); err != nil { return err } - logger.Info("RI: []") + h.logf("RI: []") h.clearWrap() info, err := GetConsoleScreenBufferInfo(h.fd) @@ -663,21 +680,21 @@ func (h *windowsAnsiEventHandler) RI() error { } func (h *windowsAnsiEventHandler) IND() error { - logger.Info("IND: []") + h.logf("IND: []") return h.executeLF() } func (h *windowsAnsiEventHandler) Flush() error { h.curInfo = nil if h.buffer.Len() > 0 { - logger.Infof("Flush: [%s]", h.buffer.Bytes()) + h.logf("Flush: [%s]", h.buffer.Bytes()) if _, err := h.buffer.WriteTo(h.file); err != nil { return err } } if h.wrapNext && !h.drewMarginByte { - logger.Infof("Flush: drawing margin byte '%c'", h.marginByte) + h.logf("Flush: drawing margin byte '%c'", h.marginByte) info, err := GetConsoleScreenBufferInfo(h.fd) if err != nil {