Added ability for UDAs to have a default value.

This commit is contained in:
Thomas 2013-05-07 13:46:10 -04:00 committed by Paul Beckingham
parent a1132f0028
commit 56eab7da0d
7 changed files with 123 additions and 2 deletions

View file

@ -1260,6 +1260,43 @@ void Task::validate (bool applyDefault /* = true */)
context.columns["due"]->validate (defaultDue))
set ("due", Date (defaultDue).toEpoch ());
}
// If a UDA has a default value in the configuration,
// override with uda.(uda).default, if not specified
if (applyDefault)
{ // Gather a list of all UDAs with a .default value
std::vector <std::string> names;
context.config.all (names);
std::vector <std::string> udas;
std::vector <std::string>::iterator name;
for (name = names.begin (); name != names.end (); ++name)
{
if (name->substr (0, 4) == "uda." &&
name->find (".default") != std::string::npos)
{
std::string::size_type period = name->find ('.', 4);
if (period != std::string::npos)
udas.push_back (name->substr (4, period - 4));
}
}
if (udas.size ())
{ // For each of those, setup the default value on the task now,
// of course only if we don't have one on the command line already
std::vector <std::string>::iterator uda;
for (uda = udas.begin (); uda != udas.end (); ++uda)
{
std::string type = context.config.get ("uda." + *uda + ".type");
std::string defVal = context.config.get ("uda." + *uda + ".default");
// If the default is empty, and we already have a value, skip it
if (defVal != "" && get (*uda) == "")
set(*uda,defVal);
}
}
}
// 2) To provide suitable warnings about odd states

View file

@ -77,13 +77,15 @@ int CmdUDAs::execute (std::string& output)
{
std::sort (udas.begin (), udas.end ());
// Render a list of UDA name, type and label.
// Render a list of UDA name, type, label, allowed values,
// possible default value, and finally the usage count.
ViewText view;
view.width (context.getWidth ());
view.add (Column::factory ("string", STRING_COLUMN_LABEL_UDA));
view.add (Column::factory ("string", STRING_COLUMN_LABEL_TYPE));
view.add (Column::factory ("string", STRING_COLUMN_LABEL_LABEL));
view.add (Column::factory ("string", STRING_COLUMN_LABEL_VALUES));
view.add (Column::factory ("string", STRING_COLUMN_LABEL_DEFAULT));
view.add (Column::factory ("string", STRING_COLUMN_LABEL_UDACOUNT));
std::vector <std::string>::iterator uda;
@ -92,6 +94,7 @@ int CmdUDAs::execute (std::string& output)
std::string type = context.config.get ("uda." + *uda + ".type");
std::string label = context.config.get ("uda." + *uda + ".label");
std::string values = context.config.get ("uda." + *uda + ".values");
std::string defval = context.config.get ("uda." + *uda + ".default");
if (label == "")
label = *uda;
@ -107,7 +110,8 @@ int CmdUDAs::execute (std::string& output)
view.set (row, 1, type);
view.set (row, 2, label);
view.set (row, 3, values);
view.set (row, 4, count);
view.set (row, 4, defval);
view.set (row, 5, count);
}
out << optionalBlankLine ()

View file

@ -176,6 +176,7 @@
#define STRING_COLUMN_LABEL_UDA "Name"
#define STRING_COLUMN_LABEL_TYPE "Type"
#define STRING_COLUMN_LABEL_LABEL "Label"
#define STRING_COLUMN_LABEL_DEFAULT "Default"
#define STRING_COLUMN_LABEL_VALUES "Allowed Values"
#define STRING_COLUMN_LABEL_UDACOUNT "Usage Count"
#define STRING_COLUMN_LABEL_ORPHAN "Orphan UDA"

View file

@ -178,6 +178,7 @@
#define STRING_COLUMN_LABEL_UDA "Nombre"
#define STRING_COLUMN_LABEL_TYPE "Tipo"
#define STRING_COLUMN_LABEL_LABEL "Etiqueta"
#define STRING_COLUMN_LABEL_DEFAULT "Defecto"
#define STRING_COLUMN_LABEL_VALUES "Valores permitidos"
#define STRING_COLUMN_LABEL_UDACOUNT "Recuento de uso"
#define STRING_COLUMN_LABEL_ORPHAN "UDA huérfano"

View file

@ -176,6 +176,7 @@
#define STRING_COLUMN_LABEL_UDA "Name"
#define STRING_COLUMN_LABEL_TYPE "Type"
#define STRING_COLUMN_LABEL_LABEL "Label"
#define STRING_COLUMN_LABEL_DEFAULT "Default"
#define STRING_COLUMN_LABEL_VALUES "Allowed Values"
#define STRING_COLUMN_LABEL_UDACOUNT "Usage Count"
#define STRING_COLUMN_LABEL_ORPHAN "Orphan UDA"

View file

@ -177,6 +177,7 @@
#define STRING_COLUMN_LABEL_UDA "Nome"
#define STRING_COLUMN_LABEL_TYPE "Tipo"
#define STRING_COLUMN_LABEL_LABEL "Etichetta"
#define STRING_COLUMN_LABEL_DEFAULT "Predefinito"
#define STRING_COLUMN_LABEL_VALUES "Valori consentiti"
#define STRING_COLUMN_LABEL_UDACOUNT "Conteggio Uso"
#define STRING_COLUMN_LABEL_ORPHAN "UDA Orfano"

76
test/uda_defaults.t Executable file
View file

@ -0,0 +1,76 @@
#! /usr/bin/env perl
################################################################################
## taskwarrior - a command line task list manager.
##
## Copyright 2006-2013, Paul Beckingham, Federico Hernandez.
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included
## in all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
## SOFTWARE.
##
## http://www.opensource.org/licenses/mit-license.php
##
################################################################################
use strict;
use warnings;
use Test::More tests => 5;
# Create the rc file.
if (open my $fh, '>', 'uda.rc')
{
print $fh "data.location=.\n",
"confirmation=off\n",
"uda.smell.type=string\n",
"uda.smell.label=Smell\n",
"uda.smell.values=weak,strong\n",
"uda.smell.default=weak\n",
"uda.size.type=numeric\n",
"uda.size.label=Size\n",
"report.uda.description=UDA Test\n",
"report.uda.columns=id,smell,size,description\n",
"report.uda.sort=id\n",
"report.uda.labels=ID,Smell,Size,Description\n";
close $fh;
ok (-r 'uda.rc', 'Created uda.rc');
}
# Add task with nondefault UDA
qx{../src/task rc:uda.rc add one smell:strong 2>&1};
# Add task without a UDA value, checking for usage of the default
qx{../src/task rc:uda.rc add two 2>&1};
# Add a task with a UDA that has no default, ensure it is entered fine
qx{../src/task rc:uda.rc add three size:10 2>&1};
my $output = qx{../src/task rc:uda.rc uda 2>&1};
like ($output, qr/1\s+strong\s+one/, 'UDA nondefault stored');
like ($output, qr/2\s+weak\s+two/, 'UDA default stored');
like ($output, qr/3\s+weak\s+10\s+three/, 'UDA without default stored');
# Cleanup.
unlink qw(pending.data completed.data undo.data backlog.data uda.rc);
ok (! -r 'pending.data' &&
! -r 'completed.data' &&
! -r 'undo.data' &&
! -r 'backlog.data' &&
! -r 'uda.rc', 'Cleanup');
exit 0;