CmdEdit: Code cleanup

This commit is contained in:
Paul Beckingham 2017-01-29 00:26:43 -05:00
parent 5bbb232f35
commit 0122b72118

View file

@ -154,7 +154,7 @@ std::vector <std::string> CmdEdit::findValues (
auto eol = text.find ('\n', found + 1); auto eol = text.find ('\n', found + 1);
if (eol != std::string::npos) if (eol != std::string::npos)
{ {
std::string value = text.substr ( auto value = text.substr (
found + name.length (), found + name.length (),
eol - (found + name.length ())); eol - (found + name.length ()));
@ -173,7 +173,7 @@ std::string CmdEdit::formatDate (
const std::string& attribute, const std::string& attribute,
const std::string& dateformat) const std::string& dateformat)
{ {
std::string value = task.get (attribute); auto value = task.get (attribute);
if (value.length ()) if (value.length ())
value = Datetime (value).toString (dateformat); value = Datetime (value).toString (dateformat);
@ -185,7 +185,7 @@ std::string CmdEdit::formatDuration (
Task& task, Task& task,
const std::string& attribute) const std::string& attribute)
{ {
std::string value = task.get (attribute); auto value = task.get (attribute);
if (value.length ()) if (value.length ())
value = Duration (value).formatISO (); value = Duration (value).formatISO ();
@ -196,7 +196,7 @@ std::string CmdEdit::formatDuration (
std::string CmdEdit::formatTask (Task task, const std::string& dateformat) std::string CmdEdit::formatTask (Task task, const std::string& dateformat)
{ {
std::stringstream before; std::stringstream before;
bool verbose = context.verbose ("edit"); auto verbose = context.verbose ("edit");
if (verbose) if (verbose)
before << "# " << STRING_EDIT_HEADER_1 << '\n' before << "# " << STRING_EDIT_HEADER_1 << '\n'
@ -329,7 +329,7 @@ std::string CmdEdit::formatTask (Task task, const std::string& dateformat)
void CmdEdit::parseTask (Task& task, const std::string& after, const std::string& dateformat) void CmdEdit::parseTask (Task& task, const std::string& after, const std::string& dateformat)
{ {
// project // project
std::string value = findValue (after, "\n Project:"); auto value = findValue (after, "\n Project:");
if (task.get ("project") != value) if (task.get ("project") != value)
{ {
if (value != "") if (value != "")
@ -366,9 +366,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
value = findValue (after, "\n Created:"); value = findValue (after, "\n Created:");
if (value != "") if (value != "")
{ {
std::string formatted = formatDate (task, "entry", dateformat); if (value != formatDate (task, "entry", dateformat))
if (formatted != value)
{ {
context.footnote (STRING_EDIT_ENTRY_MOD); context.footnote (STRING_EDIT_ENTRY_MOD);
task.set ("entry", Datetime (value, dateformat).toEpochString ()); task.set ("entry", Datetime (value, dateformat).toEpochString ());
@ -383,9 +381,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
{ {
if (task.get ("start") != "") if (task.get ("start") != "")
{ {
std::string formatted = formatDate (task, "start", dateformat); if (value != formatDate (task, "start", dateformat))
if (formatted != value)
{ {
context.footnote (STRING_EDIT_START_MOD); context.footnote (STRING_EDIT_START_MOD);
task.set ("start", Datetime (value, dateformat).toEpochString ()); task.set ("start", Datetime (value, dateformat).toEpochString ());
@ -412,9 +408,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
{ {
if (task.get ("end") != "") if (task.get ("end") != "")
{ {
std::string formatted = formatDate (task, "end", dateformat); if (value != formatDate (task, "end", dateformat))
if (formatted != value)
{ {
context.footnote (STRING_EDIT_END_MOD); context.footnote (STRING_EDIT_END_MOD);
task.set ("end", Datetime (value, dateformat).toEpochString ()); task.set ("end", Datetime (value, dateformat).toEpochString ());
@ -439,9 +433,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
{ {
if (task.get ("scheduled") != "") if (task.get ("scheduled") != "")
{ {
std::string formatted = formatDate (task, "scheduled", dateformat); if (value != formatDate (task, "scheduled", dateformat))
if (formatted != value)
{ {
context.footnote (STRING_EDIT_SCHED_MOD); context.footnote (STRING_EDIT_SCHED_MOD);
task.set ("scheduled", Datetime (value, dateformat).toEpochString ()); task.set ("scheduled", Datetime (value, dateformat).toEpochString ());
@ -468,9 +460,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
{ {
if (task.get ("due") != "") if (task.get ("due") != "")
{ {
std::string formatted = formatDate (task, "due", dateformat); if (value != formatDate (task, "due", dateformat))
if (formatted != value)
{ {
context.footnote (STRING_EDIT_DUE_MOD); context.footnote (STRING_EDIT_DUE_MOD);
task.set ("due", Datetime (value, dateformat).toEpochString ()); task.set ("due", Datetime (value, dateformat).toEpochString ());
@ -505,9 +495,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
{ {
if (task.get ("until") != "") if (task.get ("until") != "")
{ {
std::string formatted = formatDate (task, "until", dateformat); if (value != formatDate (task, "until", dateformat))
if (formatted != value)
{ {
context.footnote (STRING_EDIT_UNTIL_MOD); context.footnote (STRING_EDIT_UNTIL_MOD);
task.set ("until", Datetime (value, dateformat).toEpochString ()); task.set ("until", Datetime (value, dateformat).toEpochString ());
@ -567,9 +555,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
{ {
if (task.get ("wait") != "") if (task.get ("wait") != "")
{ {
std::string formatted = formatDate (task, "wait", dateformat); if (value != formatDate (task, "wait", dateformat))
if (formatted != value)
{ {
context.footnote (STRING_EDIT_WAIT_MOD); context.footnote (STRING_EDIT_WAIT_MOD);
task.set ("wait", Datetime (value, dateformat).toEpochString ()); task.set ("wait", Datetime (value, dateformat).toEpochString ());
@ -619,10 +605,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
auto eol = after.find ('\n', found + 1); auto eol = after.find ('\n', found + 1);
if (eol != std::string::npos) if (eol != std::string::npos)
{ {
std::string value = Lexer::trim (after.substr ( auto value = Lexer::trim (after.substr (found, eol - found), "\t ");
found,
eol - found), "\t ");
auto gap = value.find (" -- "); auto gap = value.find (" -- ");
if (gap != std::string::npos) if (gap != std::string::npos)
{ {
@ -649,7 +632,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
} }
while (annotations.find (name.str ()) != annotations.end ()); while (annotations.find (name.str ()) != annotations.end ());
std::string text = Lexer::trim (value.substr (gap + 4), "\t "); auto text = Lexer::trim (value.substr (gap + 4), "\t ");
annotations.insert (std::make_pair (name.str (), json::decode (text))); annotations.insert (std::make_pair (name.str (), json::decode (text)));
} }
} }
@ -673,10 +656,10 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
// UDAs // UDAs
for (auto& col : context.columns) for (auto& col : context.columns)
{ {
std::string type = context.config.get ("uda." + col.first + ".type"); auto type = context.config.get ("uda." + col.first + ".type");
if (type != "") if (type != "")
{ {
std::string value = findValue (after, "\n UDA " + col.first + ":"); auto value = findValue (after, "\n UDA " + col.first + ":");
if ((task.get (col.first) != value) && (type != "date" || if ((task.get (col.first) != value) && (type != "date" ||
(task.get (col.first) != Datetime (value, dateformat).toEpochString ())) && (task.get (col.first) != Datetime (value, dateformat).toEpochString ())) &&
(type != "duration" || (type != "duration" ||
@ -719,8 +702,7 @@ void CmdEdit::parseTask (Task& task, const std::string& after, const std::string
} }
// UDA orphans // UDA orphans
std::vector <std::string> orphanValues = findValues (after, "\n UDA Orphan "); for (auto& orphan : findValues (after, "\n UDA Orphan "))
for (auto& orphan : orphanValues)
{ {
auto colon = orphan.find (':'); auto colon = orphan.find (':');
if (colon != std::string::npos) if (colon != std::string::npos)
@ -751,12 +733,12 @@ CmdEdit::editResult CmdEdit::editFile (Task& task)
// Determine the output date format, which uses a hierarchy of definitions. // Determine the output date format, which uses a hierarchy of definitions.
// rc.dateformat.edit // rc.dateformat.edit
// rc.dateformat // rc.dateformat
std::string dateformat = context.config.get ("dateformat.edit"); auto dateformat = context.config.get ("dateformat.edit");
if (dateformat == "") if (dateformat == "")
dateformat = context.config.get ("dateformat"); dateformat = context.config.get ("dateformat");
// Change directory for the editor // Change directory for the editor
std::string current_dir = Directory::cwd (); auto current_dir = Directory::cwd ();
int ignored = chdir (location._data.c_str ()); int ignored = chdir (location._data.c_str ());
++ignored; // Keep compiler quiet. ++ignored; // Keep compiler quiet.
@ -766,12 +748,12 @@ CmdEdit::editResult CmdEdit::editFile (Task& task)
throw std::string (STRING_EDIT_IN_PROGRESS); throw std::string (STRING_EDIT_IN_PROGRESS);
// Format the contents, T -> text, write to a file. // Format the contents, T -> text, write to a file.
std::string before = formatTask (task, dateformat); auto before = formatTask (task, dateformat);
std::string before_orig = before; auto before_orig = before;
File::write (file.str (), before); File::write (file.str (), before);
// Determine correct editor: .taskrc:editor > $VISUAL > $EDITOR > vi // Determine correct editor: .taskrc:editor > $VISUAL > $EDITOR > vi
std::string editor = context.config.get ("editor"); auto editor = context.config.get ("editor");
char* peditor = getenv ("VISUAL"); char* peditor = getenv ("VISUAL");
if (editor == "" && peditor) editor = std::string (peditor); if (editor == "" && peditor) editor = std::string (peditor);
peditor = getenv ("EDITOR"); peditor = getenv ("EDITOR");
@ -808,7 +790,7 @@ ARE_THESE_REALLY_HARMFUL:
{ {
std::cout << STRING_EDIT_CHANGES << '\n'; std::cout << STRING_EDIT_CHANGES << '\n';
std::string problem = ""; std::string problem = "";
bool oops = false; auto oops = false;
try try
{ {