From e27f247a3ec72dc2594d367db0e0ee31068c64eb Mon Sep 17 00:00:00 2001 From: Niels Widger Date: Fri, 10 Feb 2017 08:16:11 -0500 Subject: [PATCH] Support zero parameteter 'CSI n m' in parseOne According to https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes, a zero-parameter 'CSI n m' code (for setting SGR parameters) should be treated as the same as 'CSI 0 m'. This PR changes escapeInterpreter.parseOne to support this shorthand. --- escape.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/escape.go b/escape.go index 008330e..c3ea31a 100644 --- a/escape.go +++ b/escape.go @@ -28,7 +28,6 @@ const ( var ( errNotCSI = errors.New("Not a CSI escape sequence") - errCSINotANumber = errors.New("CSI escape sequence was expecting a number or a ;") errCSIParseError = errors.New("CSI escape sequence parsing error") errCSITooLong = errors.New("CSI escape sequence is too long") ) @@ -99,12 +98,16 @@ func (ei *escapeInterpreter) parseOne(ch rune) (isEscape bool, err error) { } return false, errNotCSI case stateCSI: - if ch >= '0' && ch <= '9' { - ei.state = stateParams - ei.csiParam = append(ei.csiParam, string(ch)) - return true, nil + switch { + case ch >= '0' && ch <= '9': + ei.csiParam = append(ei.csiParam, "") + case ch == 'm': + ei.csiParam = append(ei.csiParam, "0") + default: + return false, errCSIParseError } - return false, errCSINotANumber + ei.state = stateParams + fallthrough case stateParams: switch { case ch >= '0' && ch <= '9':