From 75ff47760b92b09ff3178f92fefb4b39125d98d3 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 5 Jun 2016 14:18:12 -0400 Subject: [PATCH] CmdShorten: Added 'shorten' command --- src/commands/CmdShorten.cpp | 40 ++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/commands/CmdShorten.cpp b/src/commands/CmdShorten.cpp index 81b81e0e..0e09bdfb 100644 --- a/src/commands/CmdShorten.cpp +++ b/src/commands/CmdShorten.cpp @@ -25,6 +25,7 @@ //////////////////////////////////////////////////////////////////////////////// #include +#include #include #include #include @@ -36,7 +37,44 @@ int CmdShorten ( Rules& rules, Database& database) { - std::cout << "# CmdShorten\n"; + // Gather IDs and TAGs. + std::vector ids; + std::string delta; + for (auto& arg : cli._args) + { + if (arg.hasTag ("ID")) + ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10)); + + if (arg.hasTag ("FILTER") && + arg._lextype == Lexer::Type::duration) + delta = arg.attribute ("raw"); + } + + // TODO Support :adjust + + // Load the data. + // Note: There is no filter. + Interval filter; + auto tracked = getTracked (database, rules, filter); + + // Apply tags to ids. + for (auto& id : ids) + { + if (id <= static_cast (tracked.size ())) + { + // Note: It's okay to subtract a one-based number from a zero-based index. + Interval i = tracked[tracked.size () - id]; + + Duration dur (delta); + i.range.end -= dur.toTime_t (); + + database.modifyInterval (tracked[tracked.size () - id], i); + + // Feedback. + std::cout << "Shortened @" << id << " by " << dur.formatHours () << '\n'; + } + } + return 0; }