Merge pull request #10131 from intelfx/work/notty

`TERM=dumb` fixes
This commit is contained in:
Théophane Hufschmitt 2024-03-29 13:38:37 +01:00 committed by GitHub
commit 90f5189451
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 28 additions and 18 deletions

View file

@ -50,7 +50,7 @@ std::string renderMarkdownToTerminal(std::string_view markdown)
if (!rndr_res) if (!rndr_res)
throw Error("allocation error while rendering Markdown"); throw Error("allocation error while rendering Markdown");
return filterANSIEscapes(std::string(buf->data, buf->size), !shouldANSI()); return filterANSIEscapes(std::string(buf->data, buf->size), !isTTY());
#else #else
return std::string(markdown); return std::string(markdown);
#endif #endif

View file

@ -123,14 +123,18 @@ public:
} }
void pause() override { void pause() override {
state_.lock()->paused = true; auto state (state_.lock());
writeToStderr("\r\e[K"); state->paused = true;
if (state->active)
writeToStderr("\r\e[K");
} }
void resume() override { void resume() override {
state_.lock()->paused = false; auto state (state_.lock());
writeToStderr("\r\e[K"); state->paused = false;
state_.lock()->haveUpdate = true; if (state->active)
writeToStderr("\r\e[K");
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 {};
@ -535,7 +537,7 @@ public:
Logger * makeProgressBar() Logger * makeProgressBar()
{ {
return new ProgressBar(shouldANSI()); return new ProgressBar(isTTY());
} }
void startProgressBar() void startProgressBar()

View file

@ -52,7 +52,7 @@ public:
: printBuildLogs(printBuildLogs) : printBuildLogs(printBuildLogs)
{ {
systemd = getEnv("IN_SYSTEMD") == "1"; systemd = getEnv("IN_SYSTEMD") == "1";
tty = shouldANSI(); tty = isTTY();
} }
bool isVerbose() override { bool isVerbose() override {

View file

@ -7,11 +7,14 @@
namespace nix { namespace nix {
bool shouldANSI() bool isTTY()
{ {
return isatty(STDERR_FILENO) static const bool tty =
isatty(STDERR_FILENO)
&& getEnv("TERM").value_or("dumb") != "dumb" && getEnv("TERM").value_or("dumb") != "dumb"
&& !(getEnv("NO_COLOR").has_value() || getEnv("NOCOLOR").has_value()); && !(getEnv("NO_COLOR").has_value() || getEnv("NOCOLOR").has_value());
return tty;
} }
std::string filterANSIEscapes(std::string_view s, bool filterAll, unsigned int width) std::string filterANSIEscapes(std::string_view s, bool filterAll, unsigned int width)

View file

@ -8,7 +8,7 @@ namespace nix {
* Determine whether ANSI escape sequences are appropriate for the * Determine whether ANSI escape sequences are appropriate for the
* present output. * present output.
*/ */
bool shouldANSI(); bool isTTY();
/** /**
* Truncate a string to 'width' printable characters. If 'filterAll' * Truncate a string to 'width' printable characters. If 'filterAll'

View file

@ -16,6 +16,7 @@
#include "xml-writer.hh" #include "xml-writer.hh"
#include "legacy.hh" #include "legacy.hh"
#include "eval-settings.hh" // for defexpr #include "eval-settings.hh" // for defexpr
#include "terminal.hh"
#include <cerrno> #include <cerrno>
#include <ctime> #include <ctime>
@ -1089,7 +1090,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs)
return; return;
} }
bool tty = isatty(STDOUT_FILENO); bool tty = isTTY();
RunPager pager; RunPager pager;
Table table; Table table;

View file

@ -16,6 +16,7 @@
#include "loggers.hh" #include "loggers.hh"
#include "markdown.hh" #include "markdown.hh"
#include "memory-input-accessor.hh" #include "memory-input-accessor.hh"
#include "terminal.hh"
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -375,7 +376,9 @@ void mainWrapped(int argc, char * * argv)
setLogFormat("bar"); setLogFormat("bar");
settings.verboseBuild = false; settings.verboseBuild = false;
if (isatty(STDERR_FILENO)) {
// If on a terminal, progress will be displayed via progress bars etc. (thus verbosity=notice)
if (nix::isTTY()) {
verbosity = lvlNotice; verbosity = lvlNotice;
} else { } else {
verbosity = lvlInfo; verbosity = lvlInfo;

View file

@ -11,6 +11,7 @@
#include "legacy.hh" #include "legacy.hh"
#include "posix-source-accessor.hh" #include "posix-source-accessor.hh"
#include "misc-store-flags.hh" #include "misc-store-flags.hh"
#include "terminal.hh"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
@ -188,7 +189,7 @@ static int main_nix_prefetch_url(int argc, char * * argv)
Finally f([]() { stopProgressBar(); }); Finally f([]() { stopProgressBar(); });
if (isatty(STDERR_FILENO)) if (isTTY())
startProgressBar(); startProgressBar();
auto store = openStore(); auto store = openStore();