Author: jkupec
Date: Wed Apr 30 18:48:12 2008
New Revision: 9915
URL: http://svn.opensuse.org/viewcvs/zypp?rev=9915&view=rev
Log:
- abbreviate package summary in the search to fit screen width
Modified:
trunk/zypper/src/zypper-tabulator.cc
trunk/zypper/src/zypper-tabulator.h
trunk/zypper/src/zypper.cc
Modified: trunk/zypper/src/zypper-tabulator.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/zypper-tabulator.cc?rev=9915&r1=9914&r2=9915&view=diff
==============================================================================
--- trunk/zypper/src/zypper-tabulator.cc (original)
+++ trunk/zypper/src/zypper-tabulator.cc Wed Apr 30 18:48:12 2008
@@ -1,5 +1,7 @@
#include <iostream>
#include <cstring>
+#include <cstdlib>
+#include
#include "zypp/base/Logger.h"
@@ -97,7 +99,7 @@
container::const_iterator
i = _columns.begin (),
e = _columns.end ();
-
+
stream.setf (ios::left, ios::adjustfield);
for (unsigned c = 0; i != e ; ++i, ++c) {
if (seen_first) {
@@ -107,14 +109,48 @@
}
seen_first = true;
-// stream.width (widths[c]);// that does not work with multibyte chars
- stream << *i;
- stream.width (widths[c] - string_to_columns (*i));
+ // stream.width (widths[c]); // that does not work with multibyte chars
+ const string & s = *i;
+ if (s.size() > widths[c])
+ stream << (s.substr(0, widths[c] - 2) + "->");
+ else
+ {
+ stream << s;
+ stream.width (widths[c] - string_to_columns (s));
+ }
stream << "";
}
stream << endl;
}
+// ----------------------( Table )---------------------------------------------
+
+Table::Table() :
+ _has_header (false),
+ _max_col (0),
+ _width(0),
+ _style (defaultStyle)
+{
+ //! \todo move this to utils
+
+ const char *cols_env = getenv("COLUMNS");
+ if (cols_env)
+ _screen_width = ::atoi (cols_env);
+ else
+ {
+ ::rl_initialize();
+ //::rl_reset_screen_size();
+ ::rl_get_screen_size (NULL, &_screen_width);
+ DBG << "readline says we have " << _screen_width << " char wide console screen" << endl;
+ }
+
+ // safe default
+ if (!_screen_width)
+ _screen_width = 80;
+
+ DBG << "got screen width of " << _screen_width << endl;
+}
+
void Table::add (const TableRow& tr) {
_rows.push_back (tr);
updateColWidths (tr);
@@ -126,7 +162,17 @@
updateColWidths (tr);
}
+void Table::allowAbbrev(unsigned column) {
+ if (column >= _abbrev_col.size()) {
+ _abbrev_col.reserve(column + 1);
+ _abbrev_col.insert(_abbrev_col.end(), column - _abbrev_col.size() + 1, false);
+ }
+ _abbrev_col[column] = true;
+}
+
void Table::updateColWidths (const TableRow& tr) {
+ _width = -3;
+
TableRow::container::const_iterator
i = tr._columns.begin (),
e = tr._columns.end ();
@@ -141,6 +187,8 @@
if (max < cur)
max = cur;
+
+ _width += max + 3;
}
}
@@ -165,6 +213,18 @@
}
void Table::dumpTo (ostream &stream) const {
+
+ // reset column widths for columns that can be abbreviated
+ //! \todo allow abbrev of multiple columns?
+ unsigned c = 0;
+ for (vector<bool>::const_iterator it = _abbrev_col.begin();
+ it != _abbrev_col.end() && c <= _max_col; ++it, ++c) {
+ if (*it && _width > _screen_width) {
+ _max_width[c] -= _width - _screen_width;
+ break;
+ }
+ }
+
if (_has_header) {
_header.dumpTo (stream, _max_width, _style);
dumpRule (stream);
Modified: trunk/zypper/src/zypper-tabulator.h
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/zypper-tabulator.h?rev=9915&r1=9914&r2=9915&view=diff
==============================================================================
--- trunk/zypper/src/zypper-tabulator.h (original)
+++ trunk/zypper/src/zypper-tabulator.h Wed Apr 30 18:48:12 2008
@@ -93,12 +93,10 @@
void style (TableStyle st);
void sort (unsigned by_column); // columns start with 0...
+ void allowAbbrev(unsigned column);
+
+ Table ();
- Table () :
- _has_header (false),
- _max_col (0),
- _style (defaultStyle)
- {}
private:
void dumpRule (ostream &stream) const;
void updateColWidths (const TableRow& tr);
@@ -109,9 +107,15 @@
//! maximum column index seen in this table
unsigned _max_col;
//! maximum width of respective columns
- vector<unsigned> _max_width;
+ mutable vector<unsigned> _max_width;
+ //! table width (columns)
+ int _width;
//! table drawing style
TableStyle _style;
+ //! console screen width as retrieved by readline
+ int _screen_width;
+ //! whether to abbreviate the column if needed
+ vector<bool> _abbrev_col;
};
inline
Modified: trunk/zypper/src/zypper.cc
URL: http://svn.opensuse.org/viewcvs/zypp/trunk/zypper/src/zypper.cc?rev=9915&r1=9914&r2=9915&view=diff
==============================================================================
--- trunk/zypper/src/zypper.cc (original)
+++ trunk/zypper/src/zypper.cc Wed Apr 30 18:48:12 2008
@@ -2578,6 +2578,8 @@
{
// sort by name (can't sort by repo)
t.sort(1);
+ if (!globalOpts().no_abbrev)
+ t.allowAbbrev(2);
}
cout << t; //! \todo out().table()?
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org