More fixes for multibyte string output.
authorMichael Andres <ma@suse.de>
Tue, 24 Apr 2012 16:49:16 +0000 (18:49 +0200)
committerMichael Andres <ma@suse.de>
Tue, 24 Apr 2012 16:49:16 +0000 (18:49 +0200)
src/output/Out.cc
src/output/OutNormal.cc

index 4dcef8f..36c9a66 100644 (file)
@@ -1,31 +1,38 @@
 #include <iostream>
 #include <sstream>
 
-#include <zypp/AutoDispose.h>
+//#include <zypp/AutoDispose.h>
 
 #include "Out.h"
 #include "Table.h"
+#include "Utf8.h"
+
+////////////////////////////////////////////////////////////////////////////////
+//     class TermLine
+////////////////////////////////////////////////////////////////////////////////
 
 std::string TermLine::get( unsigned width_r, SplitFlags flags_r, char exp_r ) const
 {
-  std::string l(lhs);
-  std::string r(rhs);
+  utf8::string l(lhs);
+  utf8::string r(rhs);
 
   if ( width_r == 0 )
-    return l + r;      // plain sring if zero width
+    return zypp::str::Str() << l << r; // plain string if zero width
 
-  unsigned llen( l.length() - lhidden );
-  unsigned rlen( r.length() - rhidden );
+  unsigned llen( l.size() - lhidden );
+  unsigned rlen( r.size() - rhidden );
   int diff = width_r - llen - rlen;
 
+  //zypp::AutoDispose<int> _delay( 1, ::sleep );
+
   if ( diff > 0 )
   {
     // expand...
     if ( ! flags_r.testFlag( SF_EXPAND ) )
-      return l + r;
+      return zypp::str::Str() << l << r;
 
     if ( percentHint < 0 || percentHint > 100 )
-      return l + std::string( diff, exp_r ) + r;
+      return zypp::str::Str() << l << std::string( diff, exp_r ) << r;
 
     // else:  draw % indicator
     // -------
@@ -37,7 +44,7 @@ std::string TermLine::get( unsigned width_r, SplitFlags flags_r, char exp_r ) co
 
     int pc = diff * percentHint / 100;
     if ( diff < 6 )    // not enough space for fancy stuff
-      return l + std::string( pc, '.' ) + std::string( diff-pc, '=' ) + r;
+      return zypp::str::Str() << l <<  std::string( pc, '.' ) << std::string( diff-pc, '=' ) << r;
 
     // else: less boring
     std::string tag( zypp::str::Str() << '<' << percentHint << "%>" );
@@ -50,22 +57,26 @@ std::string TermLine::get( unsigned width_r, SplitFlags flags_r, char exp_r ) co
     if ( flags_r.testFlag( SF_CRUSH ) )
     {
       if ( rlen > width_r )
-       return r.substr( 0, width_r );
-      return l.substr( 0, width_r - rlen ) + r;
+       return r.substr( 0, width_r ).str();
+      return zypp::str::Str() << l.substr( 0, width_r - rlen ) << r;
     }
     else if ( flags_r.testFlag( SF_SPLIT ) )
     {
-      return( ( llen > width_r ? l.substr( 0, width_r ) : l )
-            + "\n"
-            + ( rlen > width_r ? r.substr( 0, width_r ) : std::string( width_r - rlen, ' ' ) + r ) );
+      return zypp::str::Str() << ( llen > width_r ? l.substr( 0, width_r ) : l )
+                             << "\n"
+                             << ( rlen > width_r ? r.substr( 0, width_r ) : std::string( width_r - rlen, ' ' ) + r );
     }
     // else:
-    return l + r;
+    return zypp::str::Str() << l << r;
   }
   // else: fits exactly
-  return l + r;
+  return zypp::str::Str() << l << r;
 }
 
+////////////////////////////////////////////////////////////////////////////////
+//     class Out
+////////////////////////////////////////////////////////////////////////////////
+
 Out::~Out()
 {}
 
index 1f83bc1..773da6a 100644 (file)
@@ -24,6 +24,8 @@
 
 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)
@@ -127,7 +129,7 @@ void OutNormal::displayProgress (const string & s, int percent)
 
   if (_isatty)
   {
-    TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '-' );
+    TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '-' );
     outstr.lhs << s << ' ';
 
     // dont display percents if invalid
@@ -144,7 +146,7 @@ void OutNormal::displayProgress (const string & s, int percent)
 
     std::string outline( outstr.get( termwidth() ) );
     cout << outline << std::flush;
-    _oneup = ( outline.length() > termwidth() );
+    // no _oneup if CRUSHed // _oneup = ( outline.length() > termwidth() );
   }
   else
     cout << '.' << std::flush;
@@ -158,7 +160,7 @@ void OutNormal::displayTick (const string & s)
 
   if (_isatty)
   {
-    TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '-' );
+    TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '-' );
     ++cursor;
     outstr.lhs << s << ' ';
     outstr.rhs << '[' << cursor.current() << ']';
@@ -169,7 +171,7 @@ void OutNormal::displayTick (const string & s)
 
     std::string outline( outstr.get( termwidth() ) );
     cout << outline << std::flush;
-    _oneup = ( outline.length() > termwidth() );
+    // no _oneup if CRUSHed // _oneup = ( outline.length() > termwidth() );
   }
   else
     cout << '.' << std::flush;
@@ -216,7 +218,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 | TermLine::SF_EXPAND, '.' );
+  TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '.' );
   if (_isatty)
   {
     if(_oneup)
@@ -261,7 +263,7 @@ void OutNormal::dwnldProgressStart(const zypp::Url & uri)
   if (_isatty)
     cout << CLEARLN;
 
-  TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '-' );
+  TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '-' );
   outstr.lhs << _("Retrieving:");
   if (verbosity() == DEBUG)
     outstr.lhs << uri << ' ';
@@ -275,7 +277,7 @@ void OutNormal::dwnldProgressStart(const zypp::Url & uri)
 
   std::string outline( outstr.get( termwidth() ) );
   cout << outline << std::flush;
-  _oneup = (outline.length() > termwidth());
+  // no _oneup if CRUSHed // _oneup = (outline.length() > termwidth());
 
   _newline = false;
 }
@@ -297,7 +299,7 @@ void OutNormal::dwnldProgress(const zypp::Url & uri,
     cout << CLEARLN << CURSORUP(1);
   cout << CLEARLN;
 
-  TermLine outstr( TermLine::SF_CRUSH | TermLine::SF_EXPAND, '-' );
+  TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '-' );
   outstr.lhs << _("Retrieving:") << " ";
   if (verbosity() == DEBUG)
     outstr.lhs << uri;
@@ -316,10 +318,9 @@ void OutNormal::dwnldProgress(const zypp::Url & uri,
     outstr.rhs << " (" << zypp::ByteCount(rate) << "/s)";
   outstr.rhs << ']';
 
-
   std::string outline( outstr.get( termwidth() ) );
   cout << outline << std::flush;
-  _oneup = (outline.length() > termwidth());
+  // no _oneup if CRUSHed // _oneup = (outline.length() > termwidth());
   _newline = false;
 }
 
@@ -331,7 +332,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 | TermLine::SF_EXPAND, '.' );
+  TermLine outstr( TermLine::SF_CRUSH | SF_EXPAND_IF_TTY, '.' );
   if (_isatty)
   {
     if(_oneup)