refact: Extract ProgressBar::redraw(newOutput)

This commit is contained in:
Robert Hensing 2024-09-08 01:09:51 +02:00
parent 9df5236c46
commit 047d9643b5

View file

@ -362,23 +362,28 @@ public:
updateCV.notify_one(); updateCV.notify_one();
} }
std::chrono::milliseconds draw(State & state) /**
{ * Redraw, if the output has changed.
// Call draw() and render if the output has changed. *
* Excessive redrawing is noticable on slow terminals, and it interferes
// Excessive redrawing is noticable on slow terminals, and it interferes * with text selection in some terminals, including libvte-based terminal
// with text selection in some terminals, including libvte-based terminal * emulators.
// emulators. */
void redraw(std::string newOutput)
std::optional<std::string> newOutput;
auto nextWakeup = draw(state, newOutput);
{ {
auto state(state_.lock()); auto state(state_.lock());
if (newOutput && *newOutput != state->lastOutput) { if (newOutput != state->lastOutput) {
writeToStderr(*newOutput); writeToStderr(newOutput);
state->lastOutput = std::move(*newOutput); state->lastOutput = std::move(newOutput);
} }
} }
std::chrono::milliseconds draw(State & state)
{
std::optional<std::string> newOutput;
auto nextWakeup = draw(state, newOutput);
if (newOutput)
redraw(*newOutput);
return nextWakeup; return nextWakeup;
} }