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)
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
return std::string(markdown);
#endif

View File

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

View File

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

View File

@ -7,11 +7,14 @@
namespace nix {
bool shouldANSI()
bool isTTY()
{
return isatty(STDERR_FILENO)
static const bool tty =
isatty(STDERR_FILENO)
&& getEnv("TERM").value_or("dumb") != "dumb"
&& !(getEnv("NO_COLOR").has_value() || getEnv("NOCOLOR").has_value());
return tty;
}
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
* present output.
*/
bool shouldANSI();
bool isTTY();
/**
* Truncate a string to 'width' printable characters. If 'filterAll'

View File

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

View File

@ -16,6 +16,7 @@
#include "loggers.hh"
#include "markdown.hh"
#include "memory-input-accessor.hh"
#include "terminal.hh"
#include <sys/types.h>
#include <sys/socket.h>
@ -375,7 +376,9 @@ void mainWrapped(int argc, char * * argv)
setLogFormat("bar");
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;
} else {
verbosity = lvlInfo;

View File

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