- Implemented UDAs in the 'edit' command for all four UDA data types.
This commit is contained in:
Paul Beckingham 2012-07-16 00:16:36 -04:00
parent 1d593c39e0
commit 57aa2de98c
5 changed files with 109 additions and 5 deletions

View file

@ -303,6 +303,15 @@ std::string Duration::formatPrecise () const
return std::string (formatted); return std::string (formatted);
} }
////////////////////////////////////////////////////////////////////////////////
std::string Duration::formatSeconds () const
{
char formatted[24];
sprintf (formatted, "%s%ldsec", (_negative ? "-" : ""), _secs);
return std::string (formatted);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
bool Duration::operator< (const Duration& other) bool Duration::operator< (const Duration& other)
{ {

View file

@ -57,6 +57,7 @@ public:
std::string format () const; std::string format () const;
std::string formatCompact () const; std::string formatCompact () const;
std::string formatPrecise () const; std::string formatPrecise () const;
std::string formatSeconds () const;
bool negative () const; bool negative () const;
static bool valid (const std::string&); static bool valid (const std::string&);

View file

@ -97,6 +97,35 @@ std::string CmdEdit::findValue (
return ""; return "";
} }
////////////////////////////////////////////////////////////////////////////////
std::vector <std::string> CmdEdit::findValues (
const std::string& text,
const std::string& name)
{
std::vector <std::string> results;
std::string::size_type found = 0;
while (found != std::string::npos)
{
found = text.find (name, found + 1);
if (found != std::string::npos)
{
std::string::size_type eol = text.find ("\n", found + 1);
if (eol != std::string::npos)
{
std::string value = text.substr (
found + name.length (),
eol - (found + name.length ()));
found = eol - 1;
results.push_back (trim (value, "\t "));
}
}
}
return results;
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string CmdEdit::formatDate ( std::string CmdEdit::formatDate (
Task& task, Task& task,
@ -106,7 +135,7 @@ std::string CmdEdit::formatDate (
std::string value = task.get (attribute); std::string value = task.get (attribute);
if (value.length ()) if (value.length ())
{ {
Date dt (strtol (value.c_str (), NULL, 10)); Date dt (value);
value = dt.toString (dateformat); value = dt.toString (dateformat);
} }
@ -121,8 +150,8 @@ std::string CmdEdit::formatDuration (
std::string value = task.get (attribute); std::string value = task.get (attribute);
if (value.length ()) if (value.length ())
{ {
Duration dur (strtol (value.c_str (), NULL, 10)); Duration dur (value);
value = dur.formatPrecise (); value = dur.formatSeconds ();
} }
return value; return value;
@ -681,9 +710,71 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
task.addDependency (*id); task.addDependency (*id);
} }
// TODO UDAs // UDAs
std::map <std::string, Column*>::iterator col;
for (col = context.columns.begin (); col != context.columns.end (); ++col)
{
std::string type = context.config.get ("uda." + col->first + ".type");
if (type != "")
{
std::string value = findValue (after, "\n UDA " + col->first + ":");
if (task.get (col->first) != value)
{
if (value != "")
{
context.footnote (format (STRING_EDIT_UDA_MOD, col->first));
// TODO UDA orphans if (type == "string")
{
task.set (col->first, value);
}
else if (type == "numeric")
{
Nibbler n (value);
double d;
if (n.getNumber (d) &&
n.depleted ())
task.set (col->first, value);
else
throw format (STRING_UDA_NUMERIC, value);
}
else if (type == "date")
{
Date d (value, dateformat);
task.set (col->first, d.toEpochString ());
}
else if (type == "duration")
{
Duration d (value);
task.set (col->first, (time_t) d);
}
}
else
{
context.footnote (format (STRING_EDIT_UDA_DEL, col->first));
task.remove (col->first);
}
}
}
}
// UDA orphans
std::vector <std::string> orphanValues = findValues (after, "\n UDA Orphan ");
std::vector <std::string>::iterator orphan;
for (orphan = orphanValues.begin (); orphan != orphanValues.end (); ++orphan)
{
std::string::size_type colon = orphan->find (':');
if (colon != std::string::npos)
{
std::string name = trim (orphan->substr (0, colon), "\t ");
std::string value = trim (orphan->substr (colon + 1), "\t ");
if (value != "")
task.set (name, value);
else
task.remove (name);
}
}
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////

View file

@ -41,6 +41,7 @@ public:
private: private:
std::string findValue (const std::string&, const std::string&); std::string findValue (const std::string&, const std::string&);
std::vector <std::string> findValues (const std::string&, const std::string&);
std::string formatDate (Task&, const std::string&, const std::string&); std::string formatDate (Task&, const std::string&, const std::string&);
std::string formatDuration (Task&, const std::string&); std::string formatDuration (Task&, const std::string&);
std::string formatTask (Task, const std::string&); std::string formatTask (Task, const std::string&);

View file

@ -654,6 +654,8 @@
#define STRING_EDIT_FG_DEL "Foreground color removed." #define STRING_EDIT_FG_DEL "Foreground color removed."
#define STRING_EDIT_BG_MOD "Background color modified." #define STRING_EDIT_BG_MOD "Background color modified."
#define STRING_EDIT_BG_DEL "Background color removed." #define STRING_EDIT_BG_DEL "Background color removed."
#define STRING_EDIT_UDA_MOD "UDA {1} modified."
#define STRING_EDIT_UDA_DEL "UDA {1} deleted."
// These four blocks can be replaced, but the number of lines must not change. // These four blocks can be replaced, but the number of lines must not change.
#define STRING_EDIT_HEADER_1 "The 'task <id> edit' command allows you to modify all aspects of a task" #define STRING_EDIT_HEADER_1 "The 'task <id> edit' command allows you to modify all aspects of a task"