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

Adding support for multiple decorations per chunk and proper colors inverse #56

Merged
merged 2 commits into from
Sep 6, 2020

Conversation

ktunkiewicz
Copy link

@ktunkiewicz ktunkiewicz commented Aug 31, 2020

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:

  • adds support for multiple decoration stacking until reset - this makes things like "red underlined text on yellow background and flashing" possible
  • adds proper color reverse - ad the moment color reverse in Answer is done either by adding ansi-reverse class, or a filter:invert(100%) style. While the ansi-reverse can be somehow handled correctly with CSS, the filter: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:

  • the existing 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.
  • a new decorations property is added to JSON object, this is an array of decorations found in the chunk, in the order of finding

Doing it like this it will not break any existing usage of the library.

How inverting of colors is implemented:

  • if the "reverse/invert" ANSI code is found in the chunk, the foreground and background colors are swapped. This includes truecolor as well
  • that operation is done before the moment where JSON is outputted, so it works in all modes of operation
  • if ansiToJson is used the returned JSON contains new property isInverted: boolean which is just informational, in case that information is useful
  • if ansiToHtml is used, the colors are obviously also swapped in the output, plus the data-is-inversed="true" is added t signal this
  • the class ansi-reverse is no loger outputted
  • if foreground or background color is missing in the chunk and inverting takes place, these missing colors are defaulted to either black (background) or white (foreground).

The 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 and data-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):
ezgif com-video-to-gif (1)

The same page with telnet in MacOS iTerm:
Screenshot 2020-09-01 at 00 42 21

The same page in zMud on Windows:
ezgif com-video-to-gif (2)

@ktunkiewicz
Copy link
Author

I guess with this change the version of library could be bumped to 1.5.0, assuming you think that this change is backwards-compatible, or 2.0.0 if you think it is not backwards compatible.

@IonicaBizau
Copy link
Owner

I am reviewing and merging this now. Thank you for the amazing work! ✨ MUD games are the best. 😄

@IonicaBizau IonicaBizau mentioned this pull request Sep 6, 2020
@IonicaBizau IonicaBizau merged commit 48c0cbe into IonicaBizau:master Sep 6, 2020
@Hypnosphi
Copy link

Hypnosphi commented Sep 7, 2020

Hi, can you please summarize what's the breaking change here? Am I safe to upgrade?

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.

3 participants