More progress bar fixes
authorMichael Andres <ma@suse.de>
Wed, 18 Apr 2012 09:55:08 +0000 (11:55 +0200)
committerMichael Andres <ma@suse.de>
Wed, 18 Apr 2012 09:55:08 +0000 (11:55 +0200)
src/callbacks/rpm.h
src/output/Out.cc
src/output/Out.h
src/output/OutNormal.cc

index 1966607..dd1edff 100644 (file)
 #include "output/prompt.h"
 
 
-static bool report_again(timespec * last)
+static bool report_again( timespec & lastTime_r, int & lastValue_r, int currentValue_r )
 {
-  // don't report more often than 5 times per sec
+  // Don't report more often than 5 times per sec,
+  // but also leaving 0%, more than 20% step and reaching 100%
   timespec now;
-  clock_gettime(CLOCK_REALTIME, &now);
-  if (now.tv_sec > last->tv_sec ||
-      (now.tv_sec == last->tv_sec && now.tv_nsec > last->tv_nsec + 200000000L))
+  clock_gettime( CLOCK_REALTIME, &now );
+  if ( now.tv_sec > lastTime_r.tv_sec
+    || ( now.tv_sec == lastTime_r.tv_sec && now.tv_nsec > lastTime_r.tv_nsec + 200000000L )
+    || ( lastValue_r != currentValue_r && ( lastValue_r + 20 <= currentValue_r || lastValue_r == 0 || currentValue_r == 100 ) ) )
   {
-    *last = now;
+    lastTime_r = now;
+    lastValue_r = currentValue_r;
     return true;
   }
-  else
-    return false;
+  return false;
 }
 
 ///////////////////////////////////////////////////////////////////
@@ -127,23 +129,25 @@ struct RemoveResolvableReportReceiver : public zypp::callback::ReceiveReport<zyp
 {
   std::string _label;
   timespec _last_reported;
+  zypp::DefaultIntegral<int,0> _last_percent;
 
   virtual void start( zypp::Resolvable::constPtr resolvable )
   {
     ::clock_gettime(CLOCK_REALTIME, &_last_reported);
+    _last_percent.reset();
     // translators: This text is a progress display label e.g. "Removing packagename-x.x.x [42%]"
     _label = boost::str(boost::format(_("Removing %s-%s"))
         % resolvable->name() % resolvable->edition());
     Zypper::instance()->out().progressStart("remove-resolvable", _label);
   }
 
-  virtual bool progress(int value, zypp::Resolvable::constPtr resolvable)
+  virtual bool progress( int value, zypp::Resolvable::constPtr resolvable )
   {
     // don't report too often
-    if (!report_again(&_last_reported))
-      return true;
-
-    Zypper::instance()->out().progress("remove-resolvable", _label, value);
+    if ( report_again( _last_reported, _last_percent, value ) )
+    {
+      Zypper::instance()->out().progress( "remove-resolvable", _label, value );
+    }
     return true;
   }
 
@@ -192,14 +196,12 @@ struct InstallResolvableReportReceiver : public zypp::callback::ReceiveReport<zy
   zypp::Resolvable::constPtr _resolvable;
   std::string _label;
   timespec _last_reported;
-
-  void display_step( zypp::Resolvable::constPtr resolvable, int value )
-  {
-  }
+  zypp::DefaultIntegral<int,0> _last_percent;
 
   virtual void start( zypp::Resolvable::constPtr resolvable )
   {
     clock_gettime(CLOCK_REALTIME, &_last_reported);
+    _last_percent.reset();
     _resolvable = resolvable;
     // TranslatorExplanation This text is a progress display label e.g. "Installing foo-1.1.2 [42%]"
     _label = boost::str(boost::format(_("Installing: %s-%s"))
@@ -207,13 +209,13 @@ struct InstallResolvableReportReceiver : public zypp::callback::ReceiveReport<zy
     Zypper::instance()->out().progressStart("install-resolvable", _label);
   }
 
-  virtual bool progress(int value, zypp::Resolvable::constPtr resolvable)
+  virtual bool progress( int value, zypp::Resolvable::constPtr resolvable )
   {
     // don't report too often
-    if (!report_again(&_last_reported))
-      return true;
-
-    Zypper::instance()->out().progress("install-resolvable", _label, value);
+    if ( report_again( _last_reported, _last_percent, value ) )
+    {
+      Zypper::instance()->out().progress( "install-resolvable", _label, value );
+    }
     return true;
   }
 
index 306a702..1cb519b 100644 (file)
@@ -12,9 +12,6 @@ std::string TermLine::get( unsigned width_r, SplitFlags flags_r, char exp_r ) co
   if ( width_r == 0 )
     return l + r;      // plain sring if zero width
 
-  if ( ! flags_r && flagsHint )
-    flags_r = flagsHint;
-
   unsigned llen( l.length() - lhidden );
   unsigned rlen( r.length() - rhidden );
   int diff = width_r - llen - rlen;
index 99bf803..515d77a 100644 (file)
@@ -28,14 +28,19 @@ struct TermLine
   };
   ZYPP_DECLARE_FLAGS( SplitFlags, SplitFlag );
 
-  TermLine() {}
+  TermLine( SplitFlags flags_r, char exp_r ) : flagsHint( flags_r ), expHint( exp_r ) {}
   TermLine( SplitFlags flags_r ) : flagsHint( flags_r ) {}
+  TermLine( char exp_r ) : expHint( exp_r ) {}
+  TermLine() {}
+
+  SplitFlags flagsHint;                                //< flags to use if not passed to \ref get
+  zypp::DefaultIntegral<char,' '> expHint;     //< expand char to use if not passed to \ref get
 
   zypp::str::Str lhs;                          //< left side
   zypp::str::Str rhs;                          //< right side
   zypp::DefaultIntegral<unsigned,0> lhidden;   //< size of embedded esc sequences
   zypp::DefaultIntegral<unsigned,0> rhidden;   //< size of embedded esc sequences
-  SplitFlags flagsHint;                                //< flags to use if no flags passed to \ref get
+
 
   /** Return plain line made of lhs + rhs */
   std::string get() const
@@ -44,7 +49,16 @@ struct TermLine
   /** Return line optionally formated according to \a width_r and \a flags_r.
    * If \a width_r or \a flags_r is zero a plain line made of lhs + rhs is returned.
    */
-  std::string get( unsigned width_r, SplitFlags flags_r = SplitFlag(), char exp_r = ' ' ) const;
+  std::string get( unsigned width_r, SplitFlags flags_r, char exp_r ) const;
+  /** \overload */
+  std::string get( unsigned width_r, SplitFlags flags_r ) const
+  { return get( width_r, flags_r, expHint ); }
+  /** \overload */
+  std::string get( unsigned width_r, char exp_r ) const
+  { return get( width_r, flagsHint, exp_r ); }
+  /** \overload */
+  std::string get( unsigned width_r ) const
+  { return get( width_r, flagsHint, expHint ); }
 };
 ZYPP_DECLARE_OPERATORS_FOR_FLAGS( TermLine::SplitFlags );
 
index 9307775..a36956d 100644 (file)
@@ -126,31 +126,31 @@ void OutNormal::displayProgress (const string & s, int percent)
 
   if (_isatty)
   {
-    string outline = s + " [";
+    TermLine outstr( TermLine::SF_CRUSH, '.' );
+    outstr.lhs << s;
+    outstr.rhs << " [";
     // dont display percents if invalid
     if (percent >= 0 && percent <= 100)
     {
-      std::ostringstream oss;
-      oss << percent << "%";
-      outline += oss.str();
+      outstr.rhs << percent << "%";
     }
     else
     {
       ++cursor;
-      outline += cursor.current();
+      outstr.rhs << cursor.current();
     }
-    outline += "]";
+    outstr.rhs << "]";
 
     if(_oneup)
-      cout << CLEARLN << CURSORUP(1) << CLEARLN << outline;
-    else
-      cout << CLEARLN << outline;
+      cout << CLEARLN << CURSORUP(1);
+    cout << CLEARLN;
 
+    std::string outline( outstr.get( termwidth() ) );
+    cout << outline << std::flush;
     _oneup = (outline.length() > termwidth());
   }
   else
-    cout << '.';
-  cout << std::flush;
+    cout << '.' << std::flush;
 }
 
 // ----------------------------------------------------------------------------
@@ -218,30 +218,40 @@ 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, '.' );
   if (_isatty)
   {
     if(_oneup)
     {
-      cout << CLEARLN << CURSORUP(1) << CLEARLN;
+      cout << CLEARLN << CURSORUP(1);
       _oneup = false;
     }
+    cout << CLEARLN;
+
+    outstr.lhs << label;
+    outstr.rhs << " [";
+    if (error)
+    {
+      // a bit clmupsy and not perfect: hidden char counting
+      unsigned tag( std::string(outstr.rhs).size() );
+      std::string errstr( _("error") );
+      fprint_color(outstr.rhs._str, errstr, COLOR_CONTEXT_NEGATIVE);
+      outstr.rhidden += unsigned(std::string(outstr.rhs).size() - errstr.size()) - tag ;
+    }
     else
-      cout << CLEARLN;
-    cout << label << " [";
+      outstr.rhs << _("done");
   }
-
-  if (error)
-    print_color(_("error"), COLOR_CONTEXT_NEGATIVE);
   else
-    cout << _("done");
+    outstr.rhs << (error ? _("error") : _("done"));
 
-  cout << "]";
+  outstr.rhs << "]";
+
+  std::string outline( outstr.get( termwidth() ) );
+  cout << outline << endl << std::flush;
+  _newline = true;
 
   if (!error && _use_colors)
     cout << COLOR_RESET;
-
-  cout << endl << std::flush;
-  _newline = true;
 }
 
 // progress with download rate
@@ -253,7 +263,7 @@ void OutNormal::dwnldProgressStart(const zypp::Url & uri)
   if (_isatty)
     cout << CLEARLN;
 
-  TermLine outstr( TermLine::SF_CRUSH );
+  TermLine outstr( TermLine::SF_CRUSH, '.' );
   outstr.lhs << _("Retrieving:") << " ";
   if (verbosity() == DEBUG)
     outstr.lhs << uri;
@@ -288,7 +298,7 @@ void OutNormal::dwnldProgress(const zypp::Url & uri,
     cout << CLEARLN << CURSORUP(1);
   cout << CLEARLN;
 
-  TermLine outstr( TermLine::SF_CRUSH );
+  TermLine outstr( TermLine::SF_CRUSH, '.' );
   outstr.lhs << _("Retrieving:") << " ";
   if (verbosity() == DEBUG)
     outstr.lhs << uri;
@@ -319,7 +329,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 outstr( TermLine::SF_CRUSH, '.' );
   if (_isatty)
   {
     if(_oneup)