File tree Expand file tree Collapse file tree 5 files changed +28
-19
lines changed Expand file tree Collapse file tree 5 files changed +28
-19
lines changed Original file line number Diff line number Diff line change 22#include " shared.hh"
33#include " eval.hh"
44#include " attr-path.hh"
5+ #include " progress-bar.hh"
56
67#include < unistd.h>
78
@@ -65,6 +66,8 @@ struct CmdEdit : InstallablesCommand
6566
6667 args.push_back (filename);
6768
69+ stopProgressBar ();
70+
6871 execvp (editor.c_str (), stringsToCharPtrs (args).data ());
6972
7073 throw SysError (" cannot run editor '%s'" , editor);
Original file line number Diff line number Diff line change @@ -84,7 +84,8 @@ void mainWrapped(int argc, char * * argv)
8484
8585 if (!args.command ) args.showHelpAndExit ();
8686
87- StartProgressBar bar;
87+ if (isatty (STDERR_FILENO))
88+ startProgressBar ();
8889
8990 args.command ->prepare ();
9091 args.command ->run ();
Original file line number Diff line number Diff line change @@ -95,6 +95,8 @@ class ProgressBar : public Logger
9595 uint64_t filesLinked = 0 , bytesLinked = 0 ;
9696
9797 uint64_t corruptedPaths = 0 , untrustedPaths = 0 ;
98+
99+ bool active = true ;
98100 };
99101
100102 Sync<State> state_;
@@ -106,8 +108,15 @@ class ProgressBar : public Logger
106108 }
107109
108110 ~ProgressBar ()
111+ {
112+ stop ();
113+ }
114+
115+ void stop ()
109116 {
110117 auto state (state_.lock ());
118+ if (!state->active ) return ;
119+ state->active = true ;
111120 std::string status = getStatus (*state);
112121 writeToStderr (" \r \e[K" );
113122 if (status != " " )
@@ -268,6 +277,8 @@ class ProgressBar : public Logger
268277
269278 void update (State & state)
270279 {
280+ if (!state.active ) return ;
281+
271282 std::string line;
272283
273284 std::string status = getStatus (state);
@@ -385,21 +396,16 @@ class ProgressBar : public Logger
385396 }
386397};
387398
388- StartProgressBar::StartProgressBar ()
399+ void startProgressBar ()
389400{
390- if (isatty (STDERR_FILENO)) {
391- prev = logger;
392- logger = new ProgressBar ();
393- }
401+ logger = new ProgressBar ();
394402}
395403
396- StartProgressBar::~StartProgressBar ()
404+ void stopProgressBar ()
397405{
398- if (prev) {
399- auto bar = logger;
400- logger = prev;
401- delete bar;
402- }
406+ auto progressBar = dynamic_cast <ProgressBar *>(logger);
407+ if (progressBar) progressBar->stop ();
408+
403409}
404410
405411}
Original file line number Diff line number Diff line change 44
55namespace nix {
66
7- class StartProgressBar
8- {
9- Logger * prev = 0 ;
10- public:
11- StartProgressBar ();
12- ~StartProgressBar ();
13- };
7+ void startProgressBar ();
8+
9+ void stopProgressBar ();
1410
1511}
Original file line number Diff line number Diff line change 66#include " local-store.hh"
77#include " finally.hh"
88#include " fs-accessor.hh"
9+ #include " progress-bar.hh"
910
1011#if __linux__
1112#include < sys/mount.h>
@@ -107,6 +108,8 @@ struct CmdRun : InstallablesCommand
107108 std::string cmd = *command.begin ();
108109 Strings args = command;
109110
111+ stopProgressBar ();
112+
110113 /* If this is a diverted store (i.e. its "logical" location
111114 (typically /nix/store) differs from its "physical" location
112115 (e.g. /home/eelco/nix/store), then run the command in a
You can’t perform that action at this time.
0 commit comments