diff --git a/src/Task.cpp b/src/Task.cpp index 2cdb0a4c4..917ad1a59 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -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 names; + context.config.all (names); + + std::vector udas; + std::vector ::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 ::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 diff --git a/src/commands/CmdUDAs.cpp b/src/commands/CmdUDAs.cpp index 0d9f73f65..9d8fbb291 100644 --- a/src/commands/CmdUDAs.cpp +++ b/src/commands/CmdUDAs.cpp @@ -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 ::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 () diff --git a/src/en-US.h b/src/en-US.h index f19199c2e..0e1bad8ce 100644 --- a/src/en-US.h +++ b/src/en-US.h @@ -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" diff --git a/src/es-ES.h b/src/es-ES.h index fe7207283..d1a276b99 100644 --- a/src/es-ES.h +++ b/src/es-ES.h @@ -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" diff --git a/src/fr-FR.h b/src/fr-FR.h index 1f86778a6..b892d7c6c 100644 --- a/src/fr-FR.h +++ b/src/fr-FR.h @@ -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" diff --git a/src/it-IT.h b/src/it-IT.h index eefea24dc..36af326ff 100644 --- a/src/it-IT.h +++ b/src/it-IT.h @@ -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" diff --git a/test/uda_defaults.t b/test/uda_defaults.t new file mode 100755 index 000000000..ebf378d61 --- /dev/null +++ b/test/uda_defaults.t @@ -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; +