about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-08-29T13·13+0200
committerEelco Dolstra <edolstra@gmail.com>2017-08-29T13·13+0200
commitc8235c531355d612d48f633f8a6ace5fcd97f5fe (patch)
treec54d398380ef786e6895b5427774bbd750566b31
parent05d68a6e23127e490193599f75489006830b302c (diff)
nix run: Flush the progress bar before starting the command
-rw-r--r--src/nix/edit.cc3
-rw-r--r--src/nix/main.cc3
-rw-r--r--src/nix/progress-bar.cc28
-rw-r--r--src/nix/progress-bar.hh10
-rw-r--r--src/nix/run.cc3
5 files changed, 28 insertions, 19 deletions
diff --git a/src/nix/edit.cc b/src/nix/edit.cc
index 5cf9c7200d03..fd07913bcfa2 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 <unistd.h>
 
@@ -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 0990549aeecd..9781b58544fe 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 341866679186..f48cfa13b4c4 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> state_;
@@ -107,7 +109,14 @@ 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<ProgressBar *>(logger);
+    if (progressBar) progressBar->stop();
+
 }
 
 }
diff --git a/src/nix/progress-bar.hh b/src/nix/progress-bar.hh
index d2e44f7c4fd9..af8eda5a84fd 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 8175d38e8ca6..2fc93a05c512 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 <sys/mount.h>
@@ -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