Fix zypper output if stdout is not a tty (bnc#779716)
authorMichael Andres <ma@suse.de>
Thu, 13 Sep 2012 11:38:02 +0000 (13:38 +0200)
committerMichael Andres <ma@suse.de>
Thu, 13 Sep 2012 11:38:02 +0000 (13:38 +0200)
src/output/Out.cc
src/output/Out.h
src/output/OutNormal.cc

index 36c9a66..0f6f2d6 100644 (file)
@@ -1,3 +1,5 @@
+#include <unistd.h>
+
 #include <iostream>
 #include <sstream>
 
@@ -28,7 +30,7 @@ std::string TermLine::get( unsigned width_r, SplitFlags flags_r, char exp_r ) co
   if ( diff > 0 )
   {
     // expand...
-    if ( ! flags_r.testFlag( SF_EXPAND ) )
+    if ( ! ( flags_r.testFlag( SF_EXPAND ) && ::isatty(STDOUT_FILENO) ) )
       return zypp::str::Str() << l << r;
 
     if ( percentHint < 0 || percentHint > 100 )
index 21eb770..052cf9f 100644 (file)
@@ -24,7 +24,7 @@ struct TermLine
   {
     SF_CRUSH   = 1<<0, //< truncate lhs, then rhs
     SF_SPLIT   = 1<<1, //< split line across two
-    SF_EXPAND  = 1<<2  //< expand short lines
+    SF_EXPAND  = 1<<2  //< expand short lines iff stdout is a tty
   };
   ZYPP_DECLARE_FLAGS( SplitFlags, SplitFlag );
 
index 72f3153..066c28c 100644 (file)
@@ -24,8 +24,6 @@
 
 using namespace std;
 
-#define SF_EXPAND_IF_TTY ( _isatty ? TermLine::SF_EXPAND : TermLine::SplitFlags() )
-
 OutNormal::OutNormal(Verbosity verbosity)
   : Out(TYPE_NORMAL, verbosity),
     _use_colors(false), _isatty(isatty(STDOUT_FILENO)), _newline(true), _oneup(false)
@@ -129,7 +127,7 @@ void OutNormal::displayProgress (const string & s, int percent)
 
   if (_isatty)
   {
-    TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '-' );
+    TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '-' );
     outstr.lhs << s << ' ';
 
     // dont display percents if invalid
@@ -160,7 +158,7 @@ void OutNormal::displayTick (const string & s)
 
   if (_isatty)
   {
-    TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '-' );
+    TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '-' );
     ++cursor;
     outstr.lhs << s << ' ';
     outstr.rhs << '[' << cursor.current() << ']';
@@ -218,7 +216,7 @@ void OutNormal::progressEnd(const std::string & id, const string & label, bool e
   if (!error && _use_colors)
     cout << get_color(COLOR_CONTEXT_MSG_STATUS);
 
-  TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '.' );
+  TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '.' );
   if (_isatty)
   {
     if(_oneup)
@@ -263,7 +261,7 @@ void OutNormal::dwnldProgressStart(const zypp::Url & uri)
   if (_isatty)
     cout << CLEARLN;
 
-  TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '-' );
+  TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '-' );
   outstr.lhs << _("Retrieving:") << ' ';
   if (verbosity() == DEBUG)
     outstr.lhs << uri;
@@ -299,7 +297,7 @@ void OutNormal::dwnldProgress(const zypp::Url & uri,
     cout << CLEARLN << CURSORUP(1);
   cout << CLEARLN;
 
-  TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '-' );
+  TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '-' );
   outstr.lhs << _("Retrieving:") << " ";
   if (verbosity() == DEBUG)
     outstr.lhs << uri;
@@ -332,7 +330,7 @@ void OutNormal::dwnldProgressEnd(const zypp::Url & uri, long rate, bool error)
   if (!error && _use_colors)
     cout << get_color(COLOR_CONTEXT_MSG_STATUS);
 
-  TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '.' );
+  TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '.' );
   if (_isatty)
   {
     if(_oneup)