- Fixed bug #1218 so that description columns minimum width is calculated
  correctly.
This commit is contained in:
Paul Beckingham 2013-04-01 18:07:25 -04:00
parent d14a9cacec
commit d895c4a249
6 changed files with 81 additions and 63 deletions

View file

@ -109,6 +109,8 @@ Bugs
Heusler).
+ Fixed bug #1210 so that widths are taken from localized strings instead of
hardcoded.
+ Fixed bug #1218 so that description columns minimum width is calculated
correctly.
+ Improved hyphenation by splitting on commas (even if no whitespace after).
Leads to better output of, for example, 'task show', where comma-separated
lists are common.

View file

@ -27,11 +27,13 @@
#define L10N // Localization complete.
#include <numeric>
#include <ViewTask.h>
#include <Context.h>
#include <Timer.h>
#include <text.h>
#include <utf8.h>
#include <i18n.h>
#include <main.h>
extern Context context;
@ -162,32 +164,47 @@ std::string ViewTask::render (std::vector <Task>& data, std::vector <int>& seque
if (! print_empty_columns)
_columns = nonempty_columns;
// Sum the minimal widths.
int sum_minimal = 0;
std::vector <int>::iterator c;
for (c = minimal.begin (); c != minimal.end (); ++c)
sum_minimal += *c;
int all_extra = _left_margin
+ (2 * _extra_padding)
+ ((_columns.size () - 1) * _intra_padding);
// Sum the widths.
int sum_minimal = std::accumulate (minimal.begin (), minimal.end (), 0);
int sum_ideal = std::accumulate (ideal.begin (), ideal.end (), 0);
// 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);
int overage = _width - sum_minimal - all_extra;
context.debug (format ("ViewTask::render min={1} ideal={2} overage={3}",
sum_minimal + all_extra,
sum_ideal + all_extra,
overage));
std::vector <int> widths;
if (_width == 0 || sum_ideal <= overage)
// Ideal case. Everything fits.
if (_width == 0 || sum_ideal + all_extra <= _width)
{
widths = ideal;
else if (sum_minimal > overage || overage < 0)
}
// Not enough for minimum.
else if (overage < 0)
{
context.error (format (STRING_VIEW_TOO_SMALL, sum_minimal + all_extra, _width));
widths = minimal;
}
// Perfect minimal width.
else if (overage == 0)
{
widths = minimal;
}
// Extra space to share.
else if (overage > 0)
{
widths = minimal;
overage -= sum_minimal;
// Spread 'overage' among columns where width[i] < ideal[i]
bool needed = true;

View file

@ -52,7 +52,11 @@ ColumnDescription::ColumnDescription ()
_styles.push_back ("truncated");
_styles.push_back ("count");
std::string t = Date ().toString (context.config.get ("dateformat"));
_dateformat = context.config.get ("dateformat.annotation");
if (_dateformat == "")
_dateformat = context.config.get ("dateformat");
std::string t = Date ().toString (_dateformat);
std::string d = STRING_COLUMN_EXAMPLES_DESC;
std::string a1 = STRING_COLUMN_EXAMPLES_ANNO1;
std::string a2 = STRING_COLUMN_EXAMPLES_ANNO2;
@ -74,6 +78,8 @@ ColumnDescription::ColumnDescription ()
_examples.push_back (d + " [4]");
_hyphenate = context.config.getBoolean ("hyphenate");
_indent = context.config.getInteger ("indent.annotation");
}
////////////////////////////////////////////////////////////////////////////////
@ -99,16 +105,15 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int
if (_style == "default" ||
_style == "combined")
{
int indent = context.config.getInteger ("indent.annotation");
std::string format = context.config.get ("dateformat.annotation");
if (format == "")
format = context.config.get ("dateformat");
int min_desc = longestWord (description);
int min_anno = indent + Date::length (format);
minimum = std::max (min_desc, min_anno);
minimum = longestWord (description);
maximum = utf8_width (description);
if (task.annotation_count)
{
int min_anno = _indent + Date::length (_dateformat);
if (min_anno > minimum)
minimum = min_anno;
std::map <std::string, std::string> annos;
task.getAnnotations (annos);
std::map <std::string, std::string>::iterator i;
@ -119,6 +124,7 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int
maximum = len;
}
}
}
// Just the text
else if (_style == "desc")
@ -130,20 +136,18 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int
// The text <date> <anno> ...
else if (_style == "oneline")
{
std::string format = context.config.get ("dateformat.annotation");
if (format == "")
format = context.config.get ("dateformat");
int min_desc = longestWord (description);
int min_anno = Date::length (format);
minimum = std::max (min_desc, min_anno);
minimum = longestWord (description);
maximum = utf8_width (description);
if (task.annotation_count)
{
int min_anno = Date::length (_dateformat);
std::map <std::string, std::string> annos;
task.getAnnotations (annos);
std::map <std::string, std::string>::iterator i;
for (i = annos.begin (); i != annos.end (); i++)
maximum += utf8_width (i->second) + minimum + 1;
maximum += min_anno + 1 + utf8_width (i->second);
}
}
// The te...
@ -156,11 +160,8 @@ void ColumnDescription::measure (Task& task, unsigned int& minimum, unsigned int
// The text [2]
else if (_style == "count")
{
std::map <std::string, std::string> annos;
task.getAnnotations (annos);
// <description> + ' ' + '[' + <count> + ']'
maximum = utf8_width (description) + 3 + utf8_width (format ((int)annos.size ()));
maximum = utf8_width (description) + 1 + 1 + format (task.annotation_count).length () + 1;
minimum = longestWord (description);
}
@ -183,21 +184,15 @@ void ColumnDescription::render (
if (_style == "default" ||
_style == "combined")
{
int indent = context.config.getInteger ("indent.annotation");
std::map <std::string, std::string> annos;
task.getAnnotations (annos);
if (annos.size ())
{
std::string format = context.config.get ("dateformat.annotation");
if (format == "")
format = context.config.get ("dateformat");
std::map <std::string, std::string>::iterator i;
for (i = annos.begin (); i != annos.end (); i++)
{
Date dt (strtol (i->first.substr (11).c_str (), NULL, 10));
description += "\n" + std::string (indent, ' ') + dt.toString (format) + " " + i->second;
description += "\n" + std::string (_indent, ' ') + dt.toString (_dateformat) + " " + i->second;
}
}
@ -227,15 +222,11 @@ void ColumnDescription::render (
task.getAnnotations (annos);
if (annos.size ())
{
std::string format = context.config.get ("dateformat.annotation");
if (format == "")
format = context.config.get ("dateformat");
std::map <std::string, std::string>::iterator i;
for (i = annos.begin (); i != annos.end (); i++)
{
Date dt (atoi (i->first.substr (11).c_str ()));
description += " " + dt.toString (format) + " " + i->second;
description += " " + dt.toString (_dateformat) + " " + i->second;
}
}

View file

@ -47,6 +47,8 @@ public:
private:
bool _hyphenate;
std::string _dateformat;
int _indent;
};
#endif

View file

@ -858,6 +858,9 @@
#define STRING_UTF8_INVALID_CP_REP "Invalid codepoint representation."
#define STRING_UTF8_INVALID_CP "Invalid Unicode codepoint."
// View
#define STRING_VIEW_TOO_SMALL "The report has a minimum width of {1} and does not fit in the available width of {2}."
// Usage text. This is an exception, and contains \n characters and formatting.
#define STRING_CMD_HELP_TEXT \
"Documentation for Taskwarrior can be found using 'man task', 'man taskrc', 'man " \

View file

@ -875,6 +875,9 @@
#define STRING_UTF8_INVALID_CP_REP "Representación de codepoint no válida."
#define STRING_UTF8_INVALID_CP "Codepoint Unicode no válido."
// View
#define STRING_VIEW_TOO_SMALL "The report has a minimum width of {1} and does not fit in the available width of {2}."
// ****************************************************************************************************
// Usage text. This is an exception, and contains \n characters and formatting.
/* FIXME más abajo