From aac0753b079d392492cef6663b6c2ceb16d20470 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 9 Oct 2014 18:45:32 -0400 Subject: [PATCH] ViewTask - Supports the notion of different colors for sorted columns. --- src/ViewTask.cpp | 17 +++++++++++------ src/ViewTask.h | 37 ++++++++++++++++++++----------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/ViewTask.cpp b/src/ViewTask.cpp index 414ddd90a..5c6336a5b 100644 --- a/src/ViewTask.cpp +++ b/src/ViewTask.cpp @@ -41,6 +41,7 @@ ViewTask::ViewTask () : _width (0) , _left_margin (0) , _header (0) +, _sort_header (0) , _odd (0) , _even (0) , _intra_padding (1) @@ -115,13 +116,13 @@ std::string ViewTask::render (std::vector & data, std::vector & seque bool const print_empty_columns = context.config.getBoolean ("print.empty.columns"); std::vector nonempty_columns; + std::vector nonempty_sort; // Determine minimal, ideal column widths. std::vector minimal; std::vector ideal; - std::vector ::iterator i; - for (i = _columns.begin (); i != _columns.end (); ++i) + for (unsigned int i = 0; i < _columns.size (); ++i) { // Headers factor in to width calculations. unsigned int global_min = 0; @@ -138,7 +139,7 @@ std::string ViewTask::render (std::vector & data, std::vector & seque // Determine minimum and ideal width for this column. unsigned int min = 0; unsigned int ideal = 0; - (*i)->measure (data[sequence[s]], min, ideal); + _columns[i]->measure (data[sequence[s]], min, ideal); if (min > global_min) global_min = min; if (ideal > global_ideal) global_ideal = ideal; @@ -146,7 +147,7 @@ std::string ViewTask::render (std::vector & data, std::vector & seque if (print_empty_columns || global_min != 0) { - unsigned int label_length = utf8_width ((*i)->label ()); + unsigned int label_length = utf8_width (_columns[i]->label ()); if (label_length > global_min) global_min = label_length; if (label_length > global_ideal) global_ideal = label_length; minimal.push_back (global_min); @@ -155,12 +156,16 @@ std::string ViewTask::render (std::vector & data, std::vector & seque if (! print_empty_columns && global_min != 0) { - nonempty_columns.push_back (*i); + nonempty_columns.push_back (_columns[i]); + nonempty_sort.push_back (_sort[i]); } } if (! print_empty_columns) + { _columns = nonempty_columns; + _sort = nonempty_sort; + } int all_extra = _left_margin + (2 * _extra_padding) @@ -227,7 +232,7 @@ std::string ViewTask::render (std::vector & data, std::vector & seque for (unsigned int c = 0; c < _columns.size (); ++c) { headers.push_back (std::vector ()); - _columns[c]->renderHeader (headers[c], widths[c], _header); + _columns[c]->renderHeader (headers[c], widths[c], _sort[c] ? _sort_header : _header); if (headers[c].size () > max_lines) max_lines = headers[c].size (); diff --git a/src/ViewTask.h b/src/ViewTask.h index a0d646ded..761c0ab59 100644 --- a/src/ViewTask.h +++ b/src/ViewTask.h @@ -40,33 +40,36 @@ public: ~ViewTask (); // View specifications. - void add (Column* column) { _columns.push_back (column); } - void width (int width) { _width = width; } - void leftMargin (int margin) { _left_margin = margin; } - void colorHeader (Color& c) { _header = c; } - void colorOdd (Color& c) { _odd = c; } - void colorEven (Color& c) { _even = c; } - void intraPadding (int padding) { _intra_padding = padding; } - void intraColorOdd (Color& c) { _intra_odd = c; } - void intraColorEven (Color& c) { _intra_even = c; } - void extraPadding (int padding) { _extra_padding = padding; } - void extraColorOdd (Color& c) { _extra_odd = c; } - void extraColorEven (Color& c) { _extra_even = c; } - void truncateLines (int n) { _truncate_lines = n; } - void truncateRows (int n) { _truncate_rows = n; } - void addBreak (const std::string& attr) { _breaks.push_back (attr); } - int lines () { return _lines; } - int rows () { return _rows; } + void add (Column* column, bool sort = false) { _columns.push_back (column); _sort.push_back (sort); } + void width (int width) { _width = width; } + void leftMargin (int margin) { _left_margin = margin; } + void colorHeader (Color& c) { _header = c; if (!_sort_header) _sort_header = c; } + void colorSortHeader (Color& c) { _sort_header = c; } + void colorOdd (Color& c) { _odd = c; } + void colorEven (Color& c) { _even = c; } + void intraPadding (int padding) { _intra_padding = padding; } + void intraColorOdd (Color& c) { _intra_odd = c; } + void intraColorEven (Color& c) { _intra_even = c; } + void extraPadding (int padding) { _extra_padding = padding; } + void extraColorOdd (Color& c) { _extra_odd = c; } + void extraColorEven (Color& c) { _extra_even = c; } + void truncateLines (int n) { _truncate_lines = n; } + void truncateRows (int n) { _truncate_rows = n; } + void addBreak (const std::string& attr) { _breaks.push_back (attr); } + int lines () { return _lines; } + int rows () { return _rows; } // View rendering. std::string render (std::vector &, std::vector &); private: std::vector _columns; + std::vector _sort; std::vector _breaks; int _width; int _left_margin; Color _header; + Color _sort_header; Color _odd; Color _even; int _intra_padding;