From c8235c531355d612d48f633f8a6ace5fcd97f5fe Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 29 Aug 2017 15:13:30 +0200 Subject: [PATCH] nix run: Flush the progress bar before starting the command --- src/nix/edit.cc | 3 +++ src/nix/main.cc | 3 ++- src/nix/progress-bar.cc | 28 +++++++++++++++++----------- src/nix/progress-bar.hh | 10 +++------- src/nix/run.cc | 3 +++ 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/nix/edit.cc b/src/nix/edit.cc index 5cf9c7200..fd07913bc 100644 --- a/src/nix/edit.cc +++ b/src/nix/edit.cc @@ -2,6 +2,7 @@ #include "shared.hh" #include "eval.hh" #include "attr-path.hh" +#include "progress-bar.hh" #include @@ -65,6 +66,8 @@ struct CmdEdit : InstallablesCommand args.push_back(filename); + stopProgressBar(); + execvp(editor.c_str(), stringsToCharPtrs(args).data()); throw SysError("cannot run editor '%s'", editor); diff --git a/src/nix/main.cc b/src/nix/main.cc index 0990549ae..9781b5854 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -84,7 +84,8 @@ void mainWrapped(int argc, char * * argv) if (!args.command) args.showHelpAndExit(); - StartProgressBar bar; + if (isatty(STDERR_FILENO)) + startProgressBar(); args.command->prepare(); args.command->run(); diff --git a/src/nix/progress-bar.cc b/src/nix/progress-bar.cc index 341866679..f48cfa13b 100644 --- a/src/nix/progress-bar.cc +++ b/src/nix/progress-bar.cc @@ -95,6 +95,8 @@ private: uint64_t filesLinked = 0, bytesLinked = 0; uint64_t corruptedPaths = 0, untrustedPaths = 0; + + bool active = true; }; Sync state_; @@ -106,8 +108,15 @@ public: } ~ProgressBar() + { + stop(); + } + + void stop() { auto state(state_.lock()); + if (!state->active) return; + state->active = true; std::string status = getStatus(*state); writeToStderr("\r\e[K"); if (status != "") @@ -268,6 +277,8 @@ public: void update(State & state) { + if (!state.active) return; + std::string line; std::string status = getStatus(state); @@ -385,21 +396,16 @@ public: } }; -StartProgressBar::StartProgressBar() +void startProgressBar() { - if (isatty(STDERR_FILENO)) { - prev = logger; - logger = new ProgressBar(); - } + logger = new ProgressBar(); } -StartProgressBar::~StartProgressBar() +void stopProgressBar() { - if (prev) { - auto bar = logger; - logger = prev; - delete bar; - } + auto progressBar = dynamic_cast(logger); + if (progressBar) progressBar->stop(); + } } diff --git a/src/nix/progress-bar.hh b/src/nix/progress-bar.hh index d2e44f7c4..af8eda5a8 100644 --- a/src/nix/progress-bar.hh +++ b/src/nix/progress-bar.hh @@ -4,12 +4,8 @@ namespace nix { -class StartProgressBar -{ - Logger * prev = 0; -public: - StartProgressBar(); - ~StartProgressBar(); -}; +void startProgressBar(); + +void stopProgressBar(); } diff --git a/src/nix/run.cc b/src/nix/run.cc index 8175d38e8..2fc93a05c 100644 --- a/src/nix/run.cc +++ b/src/nix/run.cc @@ -6,6 +6,7 @@ #include "local-store.hh" #include "finally.hh" #include "fs-accessor.hh" +#include "progress-bar.hh" #if __linux__ #include @@ -107,6 +108,8 @@ struct CmdRun : InstallablesCommand std::string cmd = *command.begin(); Strings args = command; + stopProgressBar(); + /* If this is a diverted store (i.e. its "logical" location (typically /nix/store) differs from its "physical" location (e.g. /home/eelco/nix/store), then run the command in a