Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support zero parameteter 'CSI n m' in parseOne #98

Closed
wants to merge 1 commit into from

Conversation

nwidger
Copy link
Contributor

@nwidger nwidger commented Feb 10, 2017

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.

nwidger added a commit to nwidger/wuzz that referenced this pull request Feb 10, 2017
This PR uses the jsoncolor package to colorize JSON bodies.  It
changes the imports to use my forked version of
github.com/jroimartin/gocui which includes a small patch to support
zero-parameter CSI escape sequences.  I have submitted a PR upstream:

jroimartin/gocui#98

Once that PR is merged the import can be switched back to
github.com/jroimartin/gocui if desired.
@nwidger
Copy link
Contributor Author

nwidger commented Feb 10, 2017

I'll add that the reason I ran into this was because github.com/fatih/color outputs a zero-parameter CSI escape sequence [m which was not getting scrubbed when printing to a gocui View.

escape.go Outdated
@@ -99,6 +99,11 @@ func (ei *escapeInterpreter) parseOne(ch rune) (isEscape bool, err error) {
}
return false, errNotCSI
case stateCSI:
if ch == 'm' {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about changing the code to:

switchState:
        switch ei.state {

...

        case stateCSI:
                switch {
                case ch >= '0' && ch <= '9':
                        ei.csiParam = append(ei.csiParam, "")
                case ch == 'm':
                        ei.csiParam = append(ei.csiParam, "0")
                default:
                        return false, errCSIParseError
                }
                ei.state = stateParams
                goto switchState
        case stateParams:

...

And also remove errCSINotANumber, which is not needed anymore.

@nwidger
Copy link
Contributor Author

nwidger commented Feb 11, 2017

How's the new commit look? Let me know if there's anything else I can change.

escape.go Outdated
}
return false, errCSINotANumber
ei.state = stateParams
goto switchState
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can get rid of goto if we use fallthrough instead.

escape.go Outdated
@@ -85,6 +84,7 @@ func (ei *escapeInterpreter) parseOne(ch rune) (isEscape bool, err error) {
return false, errCSITooLong
}
ei.curch = ch
switchState:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed anymore. See next comment.

@jroimartin
Copy link
Owner

jroimartin commented Feb 12, 2017

Could you also rebase and squash the commits?

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.
@nwidger
Copy link
Contributor Author

nwidger commented Feb 12, 2017

The ei.state = stateParams before the fallthrough needed to remain since ei.state is not assigned to in every code path in the stateParams case. Otherwise, I believe I made the changes you talked about and squashed the commits.

@jroimartin
Copy link
Owner

Merged (fast-forward) in e27f247, so I'm closing this PR manually.

Thanks!

@jroimartin jroimartin closed this Feb 12, 2017
@jroimartin
Copy link
Owner

Sorry, I had not read your last message before merging. Yes, keeping the ei.state = stateParams before the fallthrough was needed for the next calls to parseOne().

@nwidger
Copy link
Contributor Author

nwidger commented Feb 12, 2017

No problem, thanks for the quick merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants