Adding support for multiple decorations per chunk and proper colors inverse #56
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm using Anser to build a client for the MUD game I play (yes, there are still people playing MUDs! 😄 ).
The problem is that Anser has very basic support for decorations and color reverse, and MUD games often heavily use these.
This PR makes two things:
ansi-reverse
class, or afilter:invert(100%)
style. While theansi-reverse
can be somehow handled correctly with CSS, thefilter:invert(100%)
is totally wrong. As far as I know no Telnet client actually does a "negative" effect when using the "reverse/invert" ANSI code.What client do is they swap background/foreground colors, not invert them.
How multiple decorations are implemented:
decoration
in JSON output stays the same, and outputs the same value as before (in fact, last decoration found in the chunk) - this is to keep the changes backwards-compatible.decorations
property is added to JSON object, this is an array of decorations found in the chunk, in the order of findingDoing it like this it will not break any existing usage of the library.
How inverting of colors is implemented:
ansiToJson
is used the returned JSON contains new propertyisInverted: boolean
which is just informational, in case that information is usefulansiToHtml
is used, the colors are obviously also swapped in the output, plus thedata-is-inversed="true"
is added t signal thisansi-reverse
is no loger outputtedThe telnet clients normally do it like that, so if Anser does not do it, it would lead to weird outputs. For example if you take a bright yellow text and invert it, the telnet client will show black text on bright yellow background. But the browser does not default the background to black, therefore after swapping colors, the foreground is not styled and displays as default font color (probably white).
By doing it like this it, in my opinion, it should not break existing usage of the library.
I think so because we are not sending
ansi-reverse
anymore, so will not trigger any "reversing" done in the app that uses Answer. Instead that app will simply get the colors inverted already, so wouldn't even know the reversing happened.That's what the
isInverted
anddata-is-inverted
informations are for, so if that's important for the app to know, it will get that information. But the projects already using Anser, will likely work all fine by simply displaying colors as instructed by Answer output.I think the only non-backward-compatible thing in this case is not outputting
filter:invert(100%)
, so if there's some app that's using Answer, and displays some inverted text, it will now appear differently (actually, it will now appear as it should look like in the real telnet client).Here are some proofs that it works all fine.
This is a help page about using colors in the game I play, rendered with Answer (using classes, and my own CSS for colors):
The same page with
telnet
in MacOS iTerm:The same page in
zMud
on Windows: