1414
1515#include < algorithm>
1616#include < chrono>
17+ #include < cstdio>
1718#include < cstring>
1819#include < cstdlib>
1920#include < sstream>
2627#include < vix/engine/BuildParallelism.hpp>
2728#include < vix/utils/Env.hpp>
2829
30+ #ifdef _WIN32
31+ #include < io.h>
32+ #else
33+ #include < unistd.h>
34+ #endif
35+
2936#ifndef _WIN32
3037#include < fcntl.h>
3138#include < poll.h>
3239#include < signal.h>
3340#include < sys/wait.h>
34- #include < unistd.h>
3541#endif
3642
3743#ifndef _WIN32
@@ -65,6 +71,18 @@ namespace
6571} // namespace
6672#endif
6773
74+ namespace
75+ {
76+ bool stdout_is_tty () noexcept
77+ {
78+ #ifdef _WIN32
79+ return ::_isatty (::_fileno (stdout)) != 0 ;
80+ #else
81+ return ::isatty (STDOUT_FILENO ) != 0 ;
82+ #endif
83+ }
84+ } // namespace
85+
6886#ifndef _WIN32
6987namespace
7088{
@@ -257,7 +275,7 @@ namespace vix::cli::build
257275 if (!opt.status )
258276 return env;
259277
260- if (plan.preset .generator == " Ninja" )
278+ if (plan.preset .generator == " Ninja" && stdout_is_tty () )
261279 env.emplace_back (" NINJA_STATUS" , " [%f/%t] " );
262280
263281 return env;
@@ -307,13 +325,15 @@ namespace vix::cli::build
307325 process::ExecResult r;
308326 r.displayCommand = util::join_display_cmd (argv);
309327
328+ const bool interactiveOutput = stdout_is_tty ();
329+
310330 const bool filterCMakeSummary = is_configure_cmd (argv) && !cmakeVerbose;
311331
312332 const bool isConfigure = is_configure_cmd (argv);
313333
314334 const bool heartbeatEnabled = [&]() -> bool
315335 {
316- if (quiet)
336+ if (quiet || !interactiveOutput )
317337 return false ;
318338
319339 if (heartbeat.has_value ())
@@ -364,6 +384,12 @@ namespace vix::cli::build
364384 ::close (pipefd[0 ]);
365385 ::close (pipefd[1 ]);
366386
387+ // The child inherits the parent's environment. Omitting an override
388+ // from ninja_env() therefore does not remove a caller-provided status
389+ // format, whose progress renderer uses carriage returns.
390+ if (!interactiveOutput)
391+ ::unsetenv (" NINJA_STATUS" );
392+
367393 for (const auto &kv : extraEnv)
368394 ::setenv (kv.first.c_str(), kv.second.c_str(), 1);
369395
@@ -718,7 +744,27 @@ namespace vix::cli::build
718744
719745 if (!quiet)
720746 {
721- consoleBuf.append (buf.data (), static_cast <std::size_t >(n));
747+ if (interactiveOutput)
748+ {
749+ consoleBuf.append (buf.data (), static_cast <std::size_t >(n));
750+ }
751+ else
752+ {
753+ for (ssize_t i = 0 ; i < n; ++i)
754+ {
755+ const char c = buf[static_cast <std::size_t >(i)];
756+ if (c == ' \r ' )
757+ {
758+ if (i + 1 < n && buf[static_cast <std::size_t >(i + 1 )] == ' \n ' )
759+ continue ;
760+ consoleBuf.push_back (' \n ' );
761+ }
762+ else
763+ {
764+ consoleBuf.push_back (c);
765+ }
766+ }
767+ }
722768
723769 std::size_t start = 0 ;
724770 while (true )
@@ -739,9 +785,13 @@ namespace vix::cli::build
739785 }
740786 else if (should_echo_line (line))
741787 {
788+ // Only a rendered heartbeat owns a transient terminal line.
789+ // Ordinary child output must not mark one as visible, otherwise
790+ // the unconditional completion cleanup emits a stray CR outside
791+ // an interactive terminal.
792+ finish_heartbeat_line ();
742793 line.push_back (' \n ' );
743794 write_all_fd (STDOUT_FILENO , line.data (), line.size ());
744- heartbeatVisible = true ;
745795 }
746796
747797 start = nl + 1 ;
0 commit comments