View
- Broke out View into ViewTask and ViewText, where the former uses an external std::vector <Task> as storage, thus eliminating the additional copy, and the latter that duplicates data and color into 2D vectors for rendering non-task data.
This commit is contained in:
282
src/ViewText.cpp
Normal file
282
src/ViewText.cpp
Normal file
@@ -0,0 +1,282 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// task - a command line task list manager.
|
||||
//
|
||||
// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez.
|
||||
// All rights reserved.
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify it under
|
||||
// the terms of the GNU General Public License as published by the Free Software
|
||||
// Foundation; either version 2 of the License, or (at your option) any later
|
||||
// version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||
// details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program; if not, write to the
|
||||
//
|
||||
// Free Software Foundation, Inc.,
|
||||
// 51 Franklin Street, Fifth Floor,
|
||||
// Boston, MA
|
||||
// 02110-1301
|
||||
// USA
|
||||
//
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <ViewText.h>
|
||||
#include <Timer.h>
|
||||
#include <text.h>
|
||||
#include <utf8.h>
|
||||
#include <main.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
ViewText::ViewText ()
|
||||
: _width (0)
|
||||
, _left_margin (0)
|
||||
, _header (0)
|
||||
, _odd (0)
|
||||
, _even (0)
|
||||
, _intra_padding (1)
|
||||
, _intra_odd (0)
|
||||
, _intra_even (0)
|
||||
, _extra_padding (0)
|
||||
, _extra_odd (0)
|
||||
, _extra_even (0)
|
||||
, _truncate_lines (0)
|
||||
, _truncate_rows (0)
|
||||
, _lines (0)
|
||||
, _rows (0)
|
||||
{
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void ViewText::add (const std::string& label)
|
||||
{
|
||||
_columns.push_back (Column::factory ("string", label));
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
int ViewText::addRow ()
|
||||
{
|
||||
_data.push_back (std::vector <std::string> (_columns.size (), ""));
|
||||
_color.push_back (std::vector <Color> (_columns.size (), Color::nocolor));
|
||||
return _data.size () - 1;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void ViewText::set (int row, int col, const std::string& value, Color color)
|
||||
{
|
||||
_data[row][col] = value;
|
||||
|
||||
if (color.nontrivial ())
|
||||
_color[row][col] = color;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void ViewText::set (int row, int col, int value, Color color)
|
||||
{
|
||||
std::string string_value = format (value);
|
||||
_data[row][col] = value;
|
||||
|
||||
if (color.nontrivial ())
|
||||
_color[row][col] = color;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
void ViewText::set (int row, int col, float value, int width, int precision, Color color)
|
||||
{
|
||||
std::string string_value = format ((float)value, width, precision);
|
||||
_data[row][col] = value;
|
||||
|
||||
if (color.nontrivial ())
|
||||
_color[row][col] = color;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::string ViewText::render ()
|
||||
{
|
||||
Timer timer ("ViewText::render");
|
||||
|
||||
// Determine minimal, ideal column widths.
|
||||
std::vector <int> minimal;
|
||||
std::vector <int> ideal;
|
||||
|
||||
for (int col = 0; col < _columns.size (); ++col)
|
||||
{
|
||||
// Headers factor in to width calculations.
|
||||
int global_min = utf8_length (_columns[col]->getLabel ());
|
||||
int global_ideal = global_min;
|
||||
|
||||
for (int row = 0; row < _data.size (); ++row)
|
||||
{
|
||||
// Determine minimum and ideal width for this column.
|
||||
int min;
|
||||
int ideal;
|
||||
_columns[col]->measure (_data[row][col], min, ideal);
|
||||
|
||||
if (min > global_min) global_min = min;
|
||||
if (ideal > global_ideal) global_ideal = ideal;
|
||||
}
|
||||
|
||||
minimal.push_back (global_min);
|
||||
ideal.push_back (global_ideal);
|
||||
}
|
||||
|
||||
// Sum the minimal widths.
|
||||
int sum_minimal = 0;
|
||||
std::vector <int>::iterator c;
|
||||
for (c = minimal.begin (); c != minimal.end (); ++c)
|
||||
sum_minimal += *c;
|
||||
|
||||
// Sum the ideal widths.
|
||||
int sum_ideal = 0;
|
||||
for (c = ideal.begin (); c != ideal.end (); ++c)
|
||||
sum_ideal += *c;
|
||||
|
||||
// Calculate final column widths.
|
||||
int overage = _width
|
||||
- _left_margin
|
||||
- (2 * _extra_padding)
|
||||
- ((_columns.size () - 1) * _intra_padding);
|
||||
|
||||
std::vector <int> widths;
|
||||
if (sum_ideal <= overage)
|
||||
widths = ideal;
|
||||
else if (sum_minimal > overage)
|
||||
throw std::string ("There is not enough horizontal width to display the results.");
|
||||
else
|
||||
{
|
||||
widths = minimal;
|
||||
overage -= sum_minimal;
|
||||
|
||||
// Spread 'overage' among columns where width[i] < ideal[i]
|
||||
while (overage)
|
||||
{
|
||||
for (int i = 0; i < _columns.size () && overage; ++i)
|
||||
{
|
||||
if (widths[i] < ideal[i])
|
||||
{
|
||||
++widths[i];
|
||||
--overage;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Compose column headers.
|
||||
int max_lines = 0;
|
||||
std::vector <std::vector <std::string> > headers;
|
||||
for (int c = 0; c < _columns.size (); ++c)
|
||||
{
|
||||
headers.push_back (std::vector <std::string> ());
|
||||
_columns[c]->renderHeader (headers[c], widths[c], _header);
|
||||
|
||||
if (headers[c].size () > max_lines)
|
||||
max_lines = headers[c].size ();
|
||||
}
|
||||
|
||||
// Output string.
|
||||
std::string out;
|
||||
_lines = 0;
|
||||
|
||||
// Render column headers.
|
||||
std::string left_margin = std::string (_left_margin, ' ');
|
||||
std::string extra = std::string (_extra_padding, ' ');
|
||||
std::string intra = std::string (_intra_padding, ' ');
|
||||
|
||||
std::string extra_odd = _extra_odd.colorize (extra);
|
||||
std::string extra_even = _extra_even.colorize (extra);
|
||||
std::string intra_odd = _intra_odd.colorize (intra);
|
||||
std::string intra_even = _intra_even.colorize (intra);
|
||||
|
||||
for (int i = 0; i < max_lines; ++i)
|
||||
{
|
||||
out += left_margin + extra;
|
||||
|
||||
for (int c = 0; c < _columns.size (); ++c)
|
||||
{
|
||||
if (c)
|
||||
out += intra;
|
||||
|
||||
if (headers[i].size () < max_lines - i)
|
||||
out += _header.colorize (std::string (widths[c], ' '));
|
||||
else
|
||||
out += headers[c][i];
|
||||
}
|
||||
|
||||
out += extra + "\n";
|
||||
|
||||
// Stop if the line limit is exceeded.
|
||||
if (++_lines >= _truncate_lines && _truncate_lines != 0)
|
||||
return out;
|
||||
}
|
||||
|
||||
// Compose, render columns, in sequence.
|
||||
_rows = 0;
|
||||
std::vector <std::vector <std::string> > cells;
|
||||
for (int row = 0; row < _data.size (); ++row)
|
||||
{
|
||||
max_lines = 0;
|
||||
|
||||
// Alternate rows based on |s % 2|
|
||||
bool odd = (row % 2) ? true : false;
|
||||
Color row_color = odd ? _odd : _even;
|
||||
|
||||
Color cell_color;
|
||||
for (int col = 0; col < _columns.size (); ++col)
|
||||
{
|
||||
cell_color = row_color;
|
||||
cell_color.blend (_color[row][col]);
|
||||
|
||||
cells.push_back (std::vector <std::string> ());
|
||||
_columns[col]->render (cells[col], _data[row][col], widths[col], cell_color);
|
||||
|
||||
if (cells[col].size () > max_lines)
|
||||
max_lines = cells[col].size ();
|
||||
}
|
||||
|
||||
for (int i = 0; i < max_lines; ++i)
|
||||
{
|
||||
out += left_margin + (odd ? extra_odd : extra_even);
|
||||
|
||||
for (int col = 0; col < _columns.size (); ++col)
|
||||
{
|
||||
if (col)
|
||||
{
|
||||
if (row_color.nontrivial ())
|
||||
out += row_color.colorize (intra);
|
||||
else
|
||||
out += (odd ? intra_odd : intra_even);
|
||||
}
|
||||
|
||||
if (i < cells[col].size ())
|
||||
out += cells[col][i];
|
||||
else
|
||||
{
|
||||
cell_color = row_color;
|
||||
cell_color.blend (_color[row][col]);
|
||||
|
||||
out += cell_color.colorize (std::string (widths[col], ' '));
|
||||
}
|
||||
}
|
||||
|
||||
out += (odd ? extra_odd : extra_even) + "\n";
|
||||
|
||||
// Stop if the line limit is exceeded.
|
||||
if (++_lines >= _truncate_lines && _truncate_lines != 0)
|
||||
return out;
|
||||
}
|
||||
|
||||
cells.clear ();
|
||||
|
||||
// Stop if the row limit is exceeded.
|
||||
if (++_rows >= _truncate_rows && _truncate_rows != 0)
|
||||
return out;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
Reference in New Issue
Block a user