CmdChart: Default value handling

- Added default value ('15') for 'rules.<type>.cell'.
- Corrected EINVAL and ERANGE handling inside Rule::getInteger.
- Made the error message for the above more specific.
- Added the '> 0' constraint to the man page.
This commit is contained in:
Paul Beckingham 2017-02-25 13:00:20 -05:00
parent 1ed8d8e07c
commit 848c6e33fc
3 changed files with 19 additions and 18 deletions

View file

@ -696,6 +696,7 @@ Determines how many minutes are represented by a single character cell, for the
charts.
A value of '15' means that an hour is represented by 60/15, or 4 character cells.
Suitable values are the divisors of 60 (30, 20, 15, 12, ...).
The value must be greater than '0'.
Default value is '15'.
Type is one of 'month', 'week', 'day'.

View file

@ -157,9 +157,11 @@ int Rules::getInteger (const std::string& key, int defaultValue) const
{
int value = strtoimax (found->second.c_str (), nullptr, 10);
// On щuccess return the value.
if (errno != EINVAL &&
errno != ERANGE)
// Invalid values are handled. ERANGE errors are simply capped by
// strtoimax, which is desired behavior.
if (value == 0 && errno == EINVAL)
throw format ("Invalid integer value for '{1}': '{2}'", key, found->second);
return value;
}

View file

@ -157,10 +157,9 @@ int renderChart (
(rules.getBoolean ("reports." + type + ".day") ? 3 : 0) +
(rules.getBoolean ("reports." + type + ".weekday") ? 4 : 0);
auto cell = rules.getInteger ("reports." + type + ".cell");
auto cell = rules.getInteger ("reports." + type + ".cell", 15);
if (cell < 1)
throw std::string ("Invalid value for 'reports." + type + ".cell': '" + rules.get("reports." + type + ".cell") + "'");
throw format ("The value for 'reports.{1}.cell' must be at least 1.", type);
auto chars_per_hour = 60 / cell;
@ -174,7 +173,7 @@ int renderChart (
num_lines = rules.getInteger ("reports." + type + ".lines", num_lines);
if (num_lines < 1)
throw std::string ("Invalid value for 'reports." + type + ".lines': '" + rules.get("reports." + type + ".lines") + "'");
throw format ("Invalid value for 'reports.{1}.lines': '{2}'", type, rules.get ("reports." + type + ".lines"));
int spacing = 1;
if (rules.has ("reports." + type + ".spacing"))
@ -298,10 +297,9 @@ static void renderAxis (
int first_hour,
int last_hour)
{
auto cell = rules.getInteger ("reports." + type + ".cell");
auto cell = rules.getInteger ("reports." + type + ".cell", 15);
if (cell < 1)
throw std::string ("Invalid value for 'reports." + type + ".cell': '" + rules.get("reports." + type + ".cell") + "'");
throw format ("The value for 'reports.{1}.cell' must be at least 1.", type);
auto chars_per_hour = 60 / cell;
@ -418,10 +416,9 @@ static std::string renderSubTotal (
(rules.getBoolean ("reports." + type + ".weekday") ? 4 : 0);
int spacing = rules.getInteger ("reports." + type + ".spacing");
auto cell = rules.getInteger ("reports." + type + ".cell");
auto cell = rules.getInteger ("reports." + type + ".cell", 15);
if (cell < 1)
throw std::string ("Invalid value for 'reports." + type + ".cell': '" + rules.get("reports." + type + ".cell") + "'");
throw format ("The value for 'reports.{1}.cell' must be at least 1.", type);
auto chars_per_hour = 60 / cell;
@ -454,10 +451,9 @@ static void renderExclusionBlocks (
int last_hour,
const std::vector <Range>& excluded)
{
auto cell = rules.getInteger ("reports." + type + ".cell");
auto cell = rules.getInteger ("reports." + type + ".cell", 15);
if (cell < 1)
throw std::string ("Invalid value for 'reports." + type + ".cell': '" + rules.get("reports." + type + ".cell") + "'");
throw format ("The value for 'reports.{1}.cell' must be at least 1.", type);
auto chars_per_hour = 60 / cell;
@ -521,7 +517,9 @@ static void renderInterval (
bool ids)
{
Datetime now;
auto cell = rules.getInteger ("reports." + type + ".cell");
auto cell = rules.getInteger ("reports." + type + ".cell", 15);
if (cell < 1)
throw format ("The value for 'reports.{1}.cell' must be at least 1.", type);
auto spacing = rules.getInteger ("reports." + type + ".spacing");
// Ignore any track that doesn't overlap with day.