]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
pretty-print: show progress bar only when we are running on a TTY
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 13 Aug 2025 05:17:10 +0000 (14:17 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 13 Aug 2025 14:44:58 +0000 (23:44 +0900)
Otherwise, when a command is running with e.g. StandardError=journal+console,
journal contains [xxxB blob data]:
```
[    5.628796] TEST-13-NSPAWN.sh[299]: + importctl import-raw --class=confext /var/tmp/importtest
[    5.632350] systemd-importd[302]: Successfully forked off '(sd-transfer)' as PID 319.
[    5.633671] TEST-13-NSPAWN.sh[318]: [83B blob data]
[    5.632598] (sd-transfer)[319]: Calling: /usr/lib/systemd/systemd-import raw --class confext - importtest
[    5.637769] systemd-importd[302]: (transfer1) Importing '/var/tmp/importtest', saving as 'importtest'.
[    5.637947] TEST-13-NSPAWN.sh[318]: [82B blob data]
[    5.638313] TEST-13-NSPAWN.sh[318]: [75B blob data]
[    5.638151] systemd-importd[302]: (transfer1) Operating on image directory '/var/lib/confexts'.
[    5.638863] systemd-importd[302]: (transfer1) Imported 40%.
[    5.638882] systemd-importd[302]: (transfer1) Wrote 40K.
[    5.639653] TEST-13-NSPAWN.sh[318]: [39B blob data]
[    5.639653] TEST-13-NSPAWN.sh[318]: [36B blob data]
[    5.639653] TEST-13-NSPAWN.sh[318]: [59B blob data]
[    5.639653] TEST-13-NSPAWN.sh[318]: [34B blob data]
[    5.638894] systemd-importd[302]: (transfer1) Operation completed successfully.
[    5.640760] TEST-13-NSPAWN.sh[318]: [25B blob data]
[    5.638902] systemd-importd[302]: (transfer1) Exiting.
```
The blob data entries are something like the following:
```
[    5.628796] TEST-13-NSPAWN.sh[299]: + importctl import-raw --class=confext /var/tmp/importtest
[    5.632350] systemd-importd[302]: Successfully forked off '(sd-transfer)' as PID 319.
[    5.633671] TEST-13-NSPAWN.sh[318]: ^M           ^MEnqueued transfer job 1. Press C-c to continue download in background.
[    5.632598] (sd-transfer)[319]: Calling: /usr/lib/systemd/systemd-import raw --class confext - importtest
[    5.637769] systemd-importd[302]: (transfer1) Importing '/var/tmp/importtest', saving as 'importtest'.
[    5.637947] TEST-13-NSPAWN.sh[318]: ^MTotal:   0%^M           ^MImporting '/var/tmp/importtest', saving as 'importtest'.
[    5.638313] TEST-13-NSPAWN.sh[318]: ^MTotal:   0%^M           ^MOperating on image directory '/var/lib/confexts'.
[    5.638151] systemd-importd[302]: (transfer1) Operating on image directory '/var/lib/confexts'.
[    5.638863] systemd-importd[302]: (transfer1) Imported 40%.
[    5.638882] systemd-importd[302]: (transfer1) Wrote 40K.
[    5.639653] TEST-13-NSPAWN.sh[318]: ^MTotal:   0%^M           ^MImported 40%.
[    5.639653] TEST-13-NSPAWN.sh[318]: ^MTotal:   0%^M           ^MWrote 40K.
[    5.639653] TEST-13-NSPAWN.sh[318]: ^MTotal:   0%^M           ^MOperation completed successfully.
[    5.639653] TEST-13-NSPAWN.sh[318]: ^MTotal:   0%^M           ^MExiting.
[    5.638894] systemd-importd[302]: (transfer1) Operation completed successfully.
[    5.640760] TEST-13-NSPAWN.sh[318]: ^MTotal:   0%^MTotal:  40%
[    5.638902] systemd-importd[302]: (transfer1) Exiting.
```

Fixes #38552.

src/shared/pretty-print.c

index cbcf2a4be21c92cfdcba0129af0cecc3b056107d..c7e77fd8e201d48fdc8ba4a82c7b20aa12536da6 100644 (file)
@@ -530,6 +530,9 @@ bool shall_tint_background(void) {
 }
 
 void draw_progress_bar_unbuffered(const char *prefix, double percentage) {
+        if (!on_tty())
+                return;
+
         fputc('\r', stderr);
         if (prefix) {
                 fputs(prefix, stderr);
@@ -593,6 +596,9 @@ void draw_progress_bar_unbuffered(const char *prefix, double percentage) {
 }
 
 void clear_progress_bar_unbuffered(const char *prefix) {
+        if (!on_tty())
+                return;
+
         fputc('\r', stderr);
 
         if (terminal_is_dumb())
@@ -609,6 +615,9 @@ void clear_progress_bar_unbuffered(const char *prefix) {
 }
 
 void draw_progress_bar(const char *prefix, double percentage) {
+        if (!on_tty())
+                return;
+
         /* We are going output a bunch of small strings that shall appear as a single line to STDERR which is
          * unbuffered by default. Let's temporarily turn on full buffering, so that this is passed to the tty
          * as a single buffer, to make things more efficient. */
@@ -621,6 +630,9 @@ int draw_progress_barf(double percentage, const char *prefixf, ...) {
         va_list ap;
         int r;
 
+        if (!on_tty())
+                return 0;
+
         va_start(ap, prefixf);
         r = vasprintf(&s, prefixf, ap);
         va_end(ap);
@@ -633,6 +645,9 @@ int draw_progress_barf(double percentage, const char *prefixf, ...) {
 }
 
 void clear_progress_bar(const char *prefix) {
+        if (!on_tty())
+                return;
+
         WITH_BUFFERED_STDERR;
         clear_progress_bar_unbuffered(prefix);
 }