From 2eb7b13ae0e21006c5fdb1d5262bb945794fa293 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 16 May 2024 13:57:15 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=AE=8C=E5=96=84=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/proxy/parser.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/proxy/parser.go b/pkg/proxy/parser.go index 5d24146d..55259670 100644 --- a/pkg/proxy/parser.go +++ b/pkg/proxy/parser.go @@ -20,6 +20,7 @@ import ( var ( charEnter = []byte("\r") + charLF = []byte("\n") enterMarks = [][]byte{ []byte("\x1b[?1049h"), @@ -189,6 +190,16 @@ func (p *Parser) ParseStream(userInChan chan *exchange.RoomMessage, srvInChan <- return p.userOutputChan, p.srvOutputChan } +func (p *Parser) isEnterKeyPress(b []byte) bool { + if bytes.LastIndex(b, charEnter) == 0 { + return true + } + if len(b) > 1 && bytes.HasSuffix(b, charLF) && isLinux(p.platform) { + return true + } + return false +} + // parseInputState 切换用户输入状态, 并结算命令和结果 func (p *Parser) parseInputState(b []byte) []byte { lang := i18n.NewLang(p.i18nLang) @@ -298,13 +309,16 @@ func (p *Parser) parseInputState(b []byte) []byte { } return nil } - - if bytes.LastIndex(b, charEnter) == 0 { + p.writeInputBuffer(b) + if p.isEnterKeyPress(b) { // 连续输入enter key, 结算上一条可能存在的命令结果 p.sendCommandRecord() p.inputState = false // 用户输入了Enter,开始结算命令 p.parseCmdInput() + if p.command == "" { + p.command = strings.TrimSpace(p.readInputBuffer()) + } if rule, cmd, ok := p.IsMatchCommandRule(p.command); ok { switch rule.Acl.Action { case model.ActionReject: @@ -330,7 +344,6 @@ func (p *Parser) parseInputState(b []byte) []byte { } p.clearInputBuffer() } else { - p.writeInputBuffer(b) if p.supportMultiCmd() && bytes.Contains(b, charEnter) { p.isMultipleCmd = true p.command = p.readInputBuffer()