Hack tablestyle for edition columns
authorMichael Andres <ma@suse.de>
Wed, 12 Sep 2012 11:45:59 +0000 (13:45 +0200)
committerMichael Andres <ma@suse.de>
Wed, 12 Sep 2012 12:03:14 +0000 (14:03 +0200)
This usually highlights the '-' separating version and release. If
the table contains exactly 2 edition columns we highlight trailing
parts that differ.

src/Table.cc
src/Table.h
zypper.spec.cmake

index 0f39156..0c557a5 100644 (file)
@@ -2,11 +2,15 @@
 #include <cstring>
 #include <cstdlib>
 
-#include <zypp/base/Logger.h>
+#include <zypp/base/LogTools.h>
+#include <zypp/base/String.h>
+#include <zypp/base/DtorReset.h>
 
+#include "utils/colors.h"
 #include "utils/console.h"
 #include "utils/text.h"
 
+#include "Zypper.h"
 #include "Table.h"
 
 // libzypp logger settings
@@ -70,6 +74,10 @@ void TableRow::dumpTo (ostream &stream, const Table & parent) const
   int curpos = parent._margin;
   // whether to break the line now in order to wrap it to screen width
   bool do_wrap = false;
+  // On a table with 2 edition columns highlight the editions
+  // except for the common prefix.
+  std::string::size_type editionSep( std::string::npos );
+
   for (unsigned c = 0; i != e ; ++i, ++c)
   {
     if (seen_first)
@@ -111,7 +119,53 @@ void TableRow::dumpTo (ostream &stream, const Table & parent) const
     }
     else
     {
-      stream << s;
+      string s(*i);
+      if ( !parent._inHeader && parent.editionStyle( c ) && Zypper::instance()->config().do_colors )
+      {
+       // Edition column
+       if ( parent._editionStyle.size() == 2 )
+       {
+         // 2 Edition columns - highlight difference
+         if ( editionSep == std::string::npos )
+         {
+           editionSep = zypp::str::commonPrefix( _columns[*parent._editionStyle.begin()],
+                                                 _columns[*(++parent._editionStyle.begin())] );
+         }
+
+         if ( editionSep == 0 )
+         {
+           fprint_color( stream, s, COLOR_CONTEXT_HIGHLIGHT );
+         }
+         else if ( editionSep == s.size() )
+         {
+           stream << s;
+         }
+         else
+         {
+           stream << s.substr( 0, editionSep );
+           fprint_color( stream, s.substr( editionSep ), COLOR_CONTEXT_HIGHLIGHT );
+         }
+       }
+       else
+       {
+         // highlight edition-release separator
+         editionSep = s.find( '-' );
+         if ( editionSep != std::string::npos )
+         {
+           stream << s.substr( 0, editionSep );
+           fprint_color( stream, "-", COLOR_CONTEXT_HIGHLIGHT );
+           stream << s.substr( editionSep+1 );
+         }
+         else  // no release part
+         {
+           stream << s;
+         }
+       }
+      }
+      else     // no special style
+      {
+       stream << s;
+      }
       stream.width (parent._max_width[c] - ssize);
     }
     stream << "";
@@ -132,6 +186,7 @@ Table::Table()
   , _margin(0)
   , _force_break_after(-1)
   , _do_wrap(false)
+  , _inHeader( false )
 {}
 
 void Table::add (const TableRow& tr) {
@@ -223,6 +278,8 @@ void Table::dumpTo (ostream &stream) const {
   }
 
   if (_has_header) {
+    zypp::DtorReset inHeader( _inHeader, false );
+    _inHeader = true;
     _header.dumpTo (stream, *this);
     dumpRule (stream);
   }
index 22ec8dd..ae60f3f 100644 (file)
@@ -14,6 +14,7 @@
 #include <iosfwd>
 #include <list>
 #include <vector>
+
 using std::string;
 using std::ostream;
 using std::list;
@@ -75,14 +76,22 @@ private:
   friend class Table;
 };
 
+/** \relates TableRow */
+inline TableRow & operator<<( TableRow & tr, const string & s )
+{
+  tr.add (s);
+  return tr;
+}
+
 class TableHeader : public TableRow {
 public:
   //! Constructor. Reserve place for c columns.
   TableHeader (unsigned c = 0): TableRow (c) {}
 };
 
-inline
-TableRow& operator << (TableRow& tr, const string& s) {
+/** \relates TableHeader */
+inline TableHeader & operator<<( TableHeader & tr, const string & s )
+{
   tr.add (s);
   return tr;
 }
@@ -112,6 +121,10 @@ public:
 
   Table ();
 
+  // poor workaroud missing column styles and table entry objects
+  void setEditionStyle( unsigned column )
+  { _editionStyle.insert( column ); }
+
 private:
   void dumpRule (ostream &stream) const;
   void updateColWidths (const TableRow& tr);
@@ -140,9 +153,37 @@ private:
   //! Whether to wrap the table if it exceeds _screen_width
   bool _do_wrap;
 
+  mutable bool _inHeader;
+  std::set<unsigned> _editionStyle;
+  bool editionStyle( unsigned column ) const
+  { return _editionStyle.find( column ) != _editionStyle.end(); }
+
   friend class TableRow;
 };
 
+namespace table
+{
+  /** TableHeader manipulator */
+  struct EditionStyleSetter
+  {
+    EditionStyleSetter( Table & table_r, const std::string & header_r )
+    : _table( table_r )
+    , _header( header_r )
+    {}
+    Table & _table;
+    std::string _header;
+  };
+
+  /** \relates table::EditionStyleSetter */
+  inline TableHeader & operator<< ( TableHeader & th, const EditionStyleSetter & obj )
+  {
+    obj._table.setEditionStyle( th.cols() );
+    th.add( obj._header );
+    return th;
+  }
+}
+
+
 inline
 Table& operator << (Table& table, const TableRow& tr) {
   table.add (tr);
index bd5c0ac..64d8d69 100644 (file)
@@ -17,7 +17,7 @@
 # norootforbuild
 
 Name:           @PACKAGE@
-BuildRequires:  libzypp-devel >= 12.1.0
+BuildRequires:  libzypp-devel >= 12.2.0
 BuildRequires:  boost-devel >= 1.33.1
 BuildRequires:  gettext-devel >= 0.15
 BuildRequires:  readline-devel >= 5.1