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 "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);

View file

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

View file

@ -95,6 +95,8 @@ private:
uint64_t filesLinked = 0, bytesLinked = 0;
uint64_t corruptedPaths = 0, untrustedPaths = 0;
bool active = true;
};
Sync<State> 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();
}
}
StartProgressBar::~StartProgressBar()
void stopProgressBar()
{
if (prev) {
auto bar = logger;
logger = prev;
delete bar;
}
auto progressBar = dynamic_cast<ProgressBar *>(logger);
if (progressBar) progressBar->stop();
}
}

View file

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

View file

@ -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