CmdShorten: Cleanup

This commit is contained in:
Paul Beckingham 2016-07-24 10:55:02 -04:00
parent 0ba4b40a6a
commit beafe79b20

View file

@ -26,6 +26,7 @@
#include <cmake.h> #include <cmake.h>
#include <Duration.h> #include <Duration.h>
#include <format.h>
#include <commands.h> #include <commands.h>
#include <timew.h> #include <timew.h>
#include <iostream> #include <iostream>
@ -53,8 +54,6 @@ int CmdShorten (
if (! ids.size ()) if (! ids.size ())
throw std::string ("IDs must be specified. See 'timew help shorten'."); throw std::string ("IDs must be specified. See 'timew help shorten'.");
// TODO Support :adjust
// Load the data. // Load the data.
// Note: There is no filter. // Note: There is no filter.
Interval filter; Interval filter;
@ -63,38 +62,27 @@ int CmdShorten (
// Apply tags to ids. // Apply tags to ids.
for (auto& id : ids) for (auto& id : ids)
{ {
if (id <= static_cast <int> (tracked.size ())) if (id > static_cast <int> (tracked.size ()))
{ throw format ("ID '@{1}' does not correspond to any tracking.", id);
// Note: It's okay to subtract a one-based number from a zero-based index. // Note: It's okay to subtract a one-based number from a zero-based index.
Interval i = tracked[tracked.size () - id]; Interval i = tracked[tracked.size () - id];
if (! i.range.is_open ()) if (i.range.is_open ())
{ throw format ("Cannot shorten open interval @{1}", id);
Duration dur (delta); Duration dur (delta);
if (dur < (i.range.end - i.range.start)) if (dur >= (i.range.end - i.range.start))
{ throw format ("Cannot shorten interval @{1} by {2} because it is only {3} in length.", id, dur.formatHours (), Duration (i.range.end - i.range.start).formatHours ());
database.deleteInterval (tracked[tracked.size () - id]); database.deleteInterval (tracked[tracked.size () - id]);
i.range.end -= dur.toTime_t (); i.range.end -= dur.toTime_t ();
validate (cli, rules, database, i); validate (cli, rules, database, i);
database.addInterval (i); database.addInterval (i);
// Feedback.
if (rules.getBoolean ("verbose")) if (rules.getBoolean ("verbose"))
std::cout << "Shortened @" << id << " by " << dur.formatHours () << '\n'; std::cout << "Shortened @" << id << " by " << dur.formatHours () << '\n';
} }
else
std::cout << "Cannot shorten interval @"
<< id
<< " by "
<< dur.formatHours ()
<< " because it is only "
<< Duration (i.range.end - i.range.start).formatHours ()
<< " in length.\n";
}
else
std::cout << "Cannot shorten open interval @" << id << '\n';
}
}
return 0; return 0;
} }