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

Fix support for nested styles #335

Merged
merged 3 commits into from Jul 12, 2019
Merged

Conversation

farnabaz
Copy link
Contributor

@farnabaz farnabaz commented Mar 21, 2019

As I recovered both bold and dim style has same closing chars and this cause the problem

bold: { open: '\u001b[1m', close: '\u001b[22m' }
dim: { open: '\u001b[2m', close: '\u001b[22m' }

On line 165 all closing chars on current style replace by opening chars, so in this specific situation closing tag of dim replaced by bold opening tag and bar seen as dim

chalk/index.js

Line 165 in 7b9211b

string = code.open + string.replace(code.closeRe, code.open) + code.close;

Fixes #334

@Qix-
Copy link
Member

Qix- commented Mar 21, 2019

Unfortunately this doesn't solve it. This is intentional. The history of escape codes is annoying and unclear, but 22m is the only widely compatible way to close either bold or dim. 21m is not used as frequently since it has been confused as a double-underline, which isn't commonly supported in rendering libraries and thus is usually just silently ignored.

Also, "dim" is usually the analog of "bold", so having both modes enabled at once wouldn't make much sense under such an interpretation, hence most implementations keeping it simply 22m.

I wish it was this easy.

@farnabaz
Copy link
Contributor Author

farnabaz commented Mar 21, 2019

Thank you @Qix- for information. I didn't know much about escape code, I've checked ansi-colors and compared its result with chalk:

'\u001b[1mfoo \u001b[31m\u001b[2mbar\u001b[22m\u001b[1m\u001b[39m baz\u001b[22m' ansi-colors
'\u001b[1mfoo \u001b[31m\u001b[2mbar          \u001b[1m\u001b[39m baz\u001b[22m' chalk

As you can see the only difference between chalk and ansi-colors is 22m that closes dim, So I thought I found problem, I've change line 165 to prevent removing close char of dim and it works.

Also, all test passed except the one that compares the nesting style result.
Here is result after this change:
Screen Shot 2019-03-21 at 10 12 18 PM

@mirefly
Copy link

mirefly commented Mar 24, 2019

This is how ansi-colors solve the nested styling bug. To me, it looks like a good way to do it without introducing a global.

@Qix-
Copy link
Member

Qix- commented Mar 24, 2019

Yeah, this seems right. @sindresorhus at some point we need to condense codes down into a single CSI character and not repeat it because that output is ugly (not your problem @farnabaz).

@sindresorhus
Copy link
Member

@mirefly Can you fix the merge conflict?

@sindresorhus
Copy link
Member

Can you also try to write a more descriptive PR title of what this fixes?

@mirefly
Copy link

mirefly commented Jul 12, 2019

@sindresorhus I guess you mean the author @farnabaz ?

@sindresorhus
Copy link
Member

Yes, sorry.

@farnabaz farnabaz changed the title fix(nested-styling): reopen closed style Fix support for nested styling Jul 12, 2019
@farnabaz
Copy link
Contributor Author

@sindresorhus Done

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.

Nesting bug
4 participants