Skip to content

Commit

Permalink
Merge pull request #10131 from intelfx/work/notty
Browse files Browse the repository at this point in the history
`TERM=dumb` fixes
  • Loading branch information
thufschmitt authored Mar 29, 2024
2 parents de10141 + c6f0407 commit 90f5189
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/libcmd/markdown.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 12 additions & 10 deletions src/libmain/progress-bar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,18 @@ class ProgressBar : public Logger
}

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();
}

Expand Down Expand Up @@ -162,9 +166,7 @@ class ProgressBar : public Logger
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");
}
}

Expand Down Expand Up @@ -519,7 +521,7 @@ class ProgressBar : public Logger
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 {};
Expand All @@ -535,7 +537,7 @@ class ProgressBar : public Logger

Logger * makeProgressBar()
{
return new ProgressBar(shouldANSI());
return new ProgressBar(isTTY());
}

void startProgressBar()
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SimpleLogger : public Logger
: printBuildLogs(printBuildLogs)
{
systemd = getEnv("IN_SYSTEMD") == "1";
tty = shouldANSI();
tty = isTTY();
}

bool isVerbose() override {
Expand Down
7 changes: 5 additions & 2 deletions src/libutil/terminal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/terminal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 2 additions & 1 deletion src/nix-env/nix-env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "xml-writer.hh"
#include "legacy.hh"
#include "eval-settings.hh" // for defexpr
#include "terminal.hh"

#include <cerrno>
#include <ctime>
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/nix/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/nix/prefetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "legacy.hh"
#include "posix-source-accessor.hh"
#include "misc-store-flags.hh"
#include "terminal.hh"

#include <nlohmann/json.hpp>

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 90f5189

Please sign in to comment.