Reduce the number of added/removed lines when redrawing screen

This should reduce flickering.
This commit is contained in:
Maxime Coste 2020-03-21 16:07:30 +11:00
parent 0506de8443
commit 68f0bcce7c

View File

@ -189,37 +189,50 @@ void TerminalUI::Screen::output(bool force)
} }
}; };
struct Add { int pos; int len; }; struct Change { int pos; int add; int del; };
Vector<Add> adds; Vector<Change> changes{Change{}};
auto new_hashes = lines | transform([](auto& line) { return hash_value(line.atoms); }) | gather<Vector>(); auto new_hashes = lines | transform([](auto& line) { return hash_value(line.atoms); }) | gather<Vector>();
for_each_diff(hashes.begin(), hashes.size(), for_each_diff(hashes.begin(), hashes.size(),
new_hashes.begin(), new_hashes.size(), new_hashes.begin(), new_hashes.size(),
[&, line=0, posB=0](DiffOp op, int len) mutable { [&, pos=0](DiffOp op, int len) mutable {
switch (op) switch (op)
{ {
case DiffOp::Keep: case DiffOp::Keep:
line += len; pos += len;
posB += len; if (changes.back().add != 0 || changes.back().del != 0)
changes.push_back({pos, 0, 0});
break; break;
case DiffOp::Add: case DiffOp::Add:
adds.push_back({posB, len}); if (changes.back().add == 0)
posB += len; changes.back().pos = pos;
changes.back().add += len;
pos += len;
break; break;
case DiffOp::Remove: case DiffOp::Remove:
printf("\033[%dH\033[%dM", line+1, len); changes.back().del += len;
break; break;
} }
}); });
hashes = std::move(new_hashes); hashes = std::move(new_hashes);
for (auto& add : adds) int added = 0;
for (auto& change : changes)
{ {
printf("\033[%dH\033[%dL", add.pos + 1, add.len); if (change.del > change.add)
for (int i = 0; i < add.len; ++i) printf("\033[%dH\033[%dM", change.pos - added + 1, change.del - change.add);
added += change.add;
}
for (auto& change : changes)
{ {
if (i != 0) for (int i = 0; i < change.add; ++i)
printf("\033[%dH", add.pos + i + 1); {
for (auto& atom : lines[add.pos + i].atoms) if (i == 0 and change.add > change.del)
printf("\033[%dH\033[%dL", change.pos + 1, change.add - change.del);
else
printf("\033[%dH", change.pos + i + 1);
for (auto& atom : lines[change.pos + i].atoms)
{ {
fputs("\033[", stdout); fputs("\033[", stdout);
set_attributes(atom.face.attributes); set_attributes(atom.face.attributes);