libmain/progress-bar: try harder to avoid escape sequences if !isTTY

This commit is contained in:
Ivan Shapovalov 2024-03-01 22:12:44 +01:00
parent c3e9e3d0c3
commit 950b6401f9

View file

@ -123,14 +123,18 @@ public:
} }
void pause() override { void pause() override {
state_.lock()->paused = true; auto state (state_.lock());
state->paused = true;
if (state->active)
writeToStderr("\r\e[K"); writeToStderr("\r\e[K");
} }
void resume() override { void resume() override {
state_.lock()->paused = false; auto state (state_.lock());
state->paused = false;
if (state->active)
writeToStderr("\r\e[K"); writeToStderr("\r\e[K");
state_.lock()->haveUpdate = true; state->haveUpdate = true;
updateCV.notify_one(); updateCV.notify_one();
} }
@ -162,9 +166,7 @@ public:
writeToStderr("\r\e[K" + filterANSIEscapes(s, !isTTY) + ANSI_NORMAL "\n"); writeToStderr("\r\e[K" + filterANSIEscapes(s, !isTTY) + ANSI_NORMAL "\n");
draw(state); draw(state);
} else { } else {
auto s2 = s + ANSI_NORMAL "\n"; writeToStderr(filterANSIEscapes(s, !isTTY) + "\n");
if (!isTTY) s2 = filterANSIEscapes(s2, true);
writeToStderr(s2);
} }
} }
@ -519,7 +521,7 @@ public:
std::optional<char> ask(std::string_view msg) override std::optional<char> ask(std::string_view msg) override
{ {
auto state(state_.lock()); auto state(state_.lock());
if (!state->active || !isatty(STDIN_FILENO)) return {}; if (!state->active) return {};
std::cerr << fmt("\r\e[K%s ", msg); std::cerr << fmt("\r\e[K%s ", msg);
auto s = trim(readLine(STDIN_FILENO)); auto s = trim(readLine(STDIN_FILENO));
if (s.size() != 1) return {}; if (s.size() != 1) return {};