Automerge feature. (#313)

Add the `automerge` attribute to the Pixel bit field. It controls
whether two pixels must be automerged. Defining this allows two
mergeable characters not to be merged.

This was requested by:
https://github.com/ArthurSonzogni/FTXUI/issues/285
This commit is contained in:
Arthur Sonzogni
2022-01-22 15:38:01 +01:00
committed by GitHub
parent 4267b40a68
commit 6039474a26
5 changed files with 56 additions and 22 deletions

View File

@@ -317,6 +317,10 @@ void UpgradeTopDown(std::string& top, std::string& down) {
}
}
bool ShouldAttemptAutoMerge(Pixel& pixel) {
return pixel.automerge && pixel.character.size() == 3;
}
} // namespace
/// A fixed dimension.
@@ -457,19 +461,17 @@ void Screen::ApplyShader() {
for (int y = 1; y < dimy_; ++y) {
for (int x = 1; x < dimx_; ++x) {
// Box drawing character uses exactly 3 byte.
std::string& cur = pixels_[y][x].character;
if (cur.size() != 3u)
Pixel& cur = pixels_[y][x];
if (!ShouldAttemptAutoMerge(cur))
continue;
// Left vs current.
std::string& left = pixels_[y][x-1].character;
if (left.size() == 3u)
UpgradeLeftRight(left, cur);
Pixel& left = pixels_[y][x-1];
Pixel& top = pixels_[y-1][x];
// Top vs current.
std::string& top = pixels_[y-1][x].character;
if (top.size() == 3u)
UpgradeTopDown(top, cur);
if (ShouldAttemptAutoMerge(left))
UpgradeLeftRight(left.character, cur.character);
if (ShouldAttemptAutoMerge(top))
UpgradeTopDown(top.character, cur.character);
}
}
}