nix run: Flush the progress bar before starting the command

This commit is contained in:
Eelco Dolstra 2017-08-29 15:13:30 +02:00
parent 05d68a6e23
commit c8235c5313
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
5 changed files with 28 additions and 19 deletions

View file

@ -2,6 +2,7 @@
#include "shared.hh" #include "shared.hh"
#include "eval.hh" #include "eval.hh"
#include "attr-path.hh" #include "attr-path.hh"
#include "progress-bar.hh"
#include <unistd.h> #include <unistd.h>
@ -65,6 +66,8 @@ struct CmdEdit : InstallablesCommand
args.push_back(filename); args.push_back(filename);
stopProgressBar();
execvp(editor.c_str(), stringsToCharPtrs(args).data()); execvp(editor.c_str(), stringsToCharPtrs(args).data());
throw SysError("cannot run editor '%s'", editor); throw SysError("cannot run editor '%s'", editor);

View file

@ -84,7 +84,8 @@ void mainWrapped(int argc, char * * argv)
if (!args.command) args.showHelpAndExit(); if (!args.command) args.showHelpAndExit();
StartProgressBar bar; if (isatty(STDERR_FILENO))
startProgressBar();
args.command->prepare(); args.command->prepare();
args.command->run(); args.command->run();

View file

@ -95,6 +95,8 @@ private:
uint64_t filesLinked = 0, bytesLinked = 0; uint64_t filesLinked = 0, bytesLinked = 0;
uint64_t corruptedPaths = 0, untrustedPaths = 0; uint64_t corruptedPaths = 0, untrustedPaths = 0;
bool active = true;
}; };
Sync<State> state_; Sync<State> state_;
@ -106,8 +108,15 @@ public:
} }
~ProgressBar() ~ProgressBar()
{
stop();
}
void stop()
{ {
auto state(state_.lock()); auto state(state_.lock());
if (!state->active) return;
state->active = true;
std::string status = getStatus(*state); std::string status = getStatus(*state);
writeToStderr("\r\e[K"); writeToStderr("\r\e[K");
if (status != "") if (status != "")
@ -268,6 +277,8 @@ public:
void update(State & state) void update(State & state)
{ {
if (!state.active) return;
std::string line; std::string line;
std::string status = getStatus(state); std::string status = getStatus(state);
@ -385,21 +396,16 @@ public:
} }
}; };
StartProgressBar::StartProgressBar() void startProgressBar()
{ {
if (isatty(STDERR_FILENO)) { logger = new ProgressBar();
prev = logger;
logger = new ProgressBar();
}
} }
StartProgressBar::~StartProgressBar() void stopProgressBar()
{ {
if (prev) { auto progressBar = dynamic_cast<ProgressBar *>(logger);
auto bar = logger; if (progressBar) progressBar->stop();
logger = prev;
delete bar;
}
} }
} }

View file

@ -4,12 +4,8 @@
namespace nix { namespace nix {
class StartProgressBar void startProgressBar();
{
Logger * prev = 0; void stopProgressBar();
public:
StartProgressBar();
~StartProgressBar();
};
} }

View file

@ -6,6 +6,7 @@
#include "local-store.hh" #include "local-store.hh"
#include "finally.hh" #include "finally.hh"
#include "fs-accessor.hh" #include "fs-accessor.hh"
#include "progress-bar.hh"
#if __linux__ #if __linux__
#include <sys/mount.h> #include <sys/mount.h>
@ -107,6 +108,8 @@ struct CmdRun : InstallablesCommand
std::string cmd = *command.begin(); std::string cmd = *command.begin();
Strings args = command; Strings args = command;
stopProgressBar();
/* If this is a diverted store (i.e. its "logical" location /* If this is a diverted store (i.e. its "logical" location
(typically /nix/store) differs from its "physical" location (typically /nix/store) differs from its "physical" location
(e.g. /home/eelco/nix/store), then run the command in a (e.g. /home/eelco/nix/store), then run the command in a