Added support for "default" colors in ScreenBuffer #31
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.
Since all but one of the 32
attr
bits were already in use, I expanded the attr size from 4 bytes to 6 and in the process reordered the 'colors' and 'flags' such that the colors are now in the upper 16 bits and the flags in the lower 16 (which ends up being more convenient for checking bitmasks since the bitwise operators all truncate Number values to 32 bits).After adding this extra space to the middle of the
attr
field I assigned two new flag bits (at 17 & 18) that indicate whether acolor
and/orbgColor
have been selected respectively. If they were left undefined (or passed a string value of"default"
), the flag bits are unset. The escape-generating methods now consult these flags to decide whether colors should be set based on the color bits inattr
or the terminal's 'default' fg/bg colors should be used.Some Caveats
I added 16 bits to the
attr
size but am only using two of them at the moment so it's a bit on the wasteful side. Unless you can imagine a use for the rest of that space, it might make more sense to setScreenBuffer.prototype.ATTR_SIZE
to5
rather than6
. I believe that the rest of the code should adapt without error if you just change this one value.Since the generateDeltaEscapeSequence() method may need to set the fg and bg colors back to the 'default' value independently I added the
defaultColor
andbgDefaultColor
escapes toterm.optimized
.I incremented the version number on
.sbuf
files to 3 but ended up modifying the load- and saveSyncV2() methods to branch based on the version number in the header rather than creating brand new (and nearly identical) V3 methods. If keeping the historical serialization formats in the source is important to you, you might want to paste the old versions back in.I used the
demo/spaceship.js
script as my main test-case and made some minor edits to it in places where it was relying on a blackbgColor
being present by default.I wasn't quite sure if the
object2attr
andattr2object
definitions were repeated intentionally or not (since the implementations appeared identical). For now I'm just assigning the same function to both the 'class' and 'instance' methods...