From ee06280808f12c09aa4cfd215ac865c280fe5434 Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 13 May 2009 12:35:54 -0400 Subject: [PATCH] Code Reorganization - Silver Bullet - Broke out Silver Bullet feature into separate edit.cpp file. Because it is big. --- src/Makefile.am | 2 +- src/Makefile.in | 7 ++- src/command.cpp | 119 ------------------------------------ src/edit.cpp | 157 ++++++++++++++++++++++++++++++++++++++++++++++++ src/task.h | 4 +- 5 files changed, 165 insertions(+), 124 deletions(-) create mode 100644 src/edit.cpp diff --git a/src/Makefile.am b/src/Makefile.am index c164f7999..1d898a521 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,2 +1,2 @@ bin_PROGRAMS = task -task_SOURCES = Config.cpp Date.cpp T.cpp TDB.cpp Table.cpp Grid.cpp Timer.cpp color.cpp parse.cpp task.cpp command.cpp report.cpp util.cpp text.cpp rules.cpp import.cpp Config.h Date.h T.h TDB.h Table.h Grid.h Timer.h color.h task.h +task_SOURCES = Config.cpp Date.cpp T.cpp TDB.cpp Table.cpp Grid.cpp Timer.cpp color.cpp parse.cpp task.cpp command.cpp edit.cpp report.cpp util.cpp text.cpp rules.cpp import.cpp Config.h Date.h T.h TDB.h Table.h Grid.h Timer.h color.h task.h diff --git a/src/Makefile.in b/src/Makefile.in index 0cdac5fab..ea3982dc4 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -46,8 +46,8 @@ PROGRAMS = $(bin_PROGRAMS) am_task_OBJECTS = Config.$(OBJEXT) Date.$(OBJEXT) T.$(OBJEXT) \ TDB.$(OBJEXT) Table.$(OBJEXT) Grid.$(OBJEXT) Timer.$(OBJEXT) \ color.$(OBJEXT) parse.$(OBJEXT) task.$(OBJEXT) \ - command.$(OBJEXT) report.$(OBJEXT) util.$(OBJEXT) \ - text.$(OBJEXT) rules.$(OBJEXT) import.$(OBJEXT) + command.$(OBJEXT) edit.$(OBJEXT) report.$(OBJEXT) \ + util.$(OBJEXT) text.$(OBJEXT) rules.$(OBJEXT) import.$(OBJEXT) task_OBJECTS = $(am_task_OBJECTS) task_LDADD = $(LDADD) DEFAULT_INCLUDES = -I. -I$(top_builddir)@am__isrc@ @@ -155,7 +155,7 @@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -task_SOURCES = Config.cpp Date.cpp T.cpp TDB.cpp Table.cpp Grid.cpp Timer.cpp color.cpp parse.cpp task.cpp command.cpp report.cpp util.cpp text.cpp rules.cpp import.cpp Config.h Date.h T.h TDB.h Table.h Grid.h Timer.h color.h task.h +task_SOURCES = Config.cpp Date.cpp T.cpp TDB.cpp Table.cpp Grid.cpp Timer.cpp color.cpp parse.cpp task.cpp command.cpp edit.cpp report.cpp util.cpp text.cpp rules.cpp import.cpp Config.h Date.h T.h TDB.h Table.h Grid.h Timer.h color.h task.h all: all-am .SUFFIXES: @@ -231,6 +231,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Timer.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/color.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/command.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/edit.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/import.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/parse.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/report.Po@am__quote@ diff --git a/src/command.cpp b/src/command.cpp index 19ec92fc6..4a89427e1 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -28,19 +28,12 @@ #include #include #include -#include #include #include #include #include #include -#include "Config.h" -#include "Date.h" -#include "Table.h" -#include "color.h" -#include "TDB.h" -#include "T.h" #include "task.h" #ifdef HAVE_LIBNCURSES @@ -860,118 +853,6 @@ std::string handleDuplicate (TDB& tdb, T& task, Config& conf) return out.str (); } -//////////////////////////////////////////////////////////////////////////////// -static const char* leftDelim = "<<"; -static const char* rightDelim = ">>"; -static std::string findValue (const std::string& text, const std::string& name) -{ - // Look for /^\s+name:\s+<<(.*)>>/ - // Extract - // Trim - // Join - return ""; -} - -//////////////////////////////////////////////////////////////////////////////// -// Introducing the Silver Bullet. This feature is the catch-all fixative for -// various other ills. This is like opening up the hood and going in with a -// wrench. To be used sparingly. -std::string handleEdit (TDB& tdb, T& task, Config& conf) -{ - std::stringstream out; - std::vector all; - tdb.pendingT (all); - - filterSequence (all, task); - foreach (seq, all) - { - // Check for file permissions. - std::string dataLocation = expandPath (conf.get ("data.location")); - if (access (dataLocation.c_str (), X_OK)) - throw std::string ("Your data.location directory is not writable."); - - // Create a temp file name in data.location. - std::stringstream pattern; - pattern << dataLocation << "/task." << seq->getId () << ".XXXXXX"; - char cpattern [PATH_MAX]; - strcpy (cpattern, pattern.str ().c_str ()); - char* file = mktemp (cpattern); - - // Format the contents, T -> text. - std::stringstream before; - before << "# The 'task edit ' command allows you to modify all aspects of a task" << std::endl - << "# using a text editor. What is shown below is a representation of the" << std::endl - << "# task in all it's detail. Modify what you wish, and if you save and" << std::endl - << "# quit your editor, task will read this file and try to make sense of" << std::endl - << "# what changed, and apply those changes. If you quit your editor without" << std::endl - << "# saving or making any modifications, task will do nothing." << std::endl - << "#" << std::endl - << "# Lines that begin with # are comments, and will be ignored by task." << std::endl - - << "# Edit only the items within the marks " - << leftDelim << " " << rightDelim << "." - << "All other edits will be ignored." << std::endl - - << "# Externally Visible" << std::endl - << " ID: " << seq->getId () << std::endl - << " Status: " << leftDelim << seq->getStatus () << rightDelim << std::endl - << " Project: " << leftDelim << seq->getAttribute ("project") << rightDelim << std::endl - << " Priority: " << leftDelim << seq->getAttribute ("priority") << rightDelim << std::endl; - - std::vector tags; - seq->getTags (tags); - std::string allTags; - join (allTags, " ", tags); - before << " Tags: " << leftDelim << allTags << rightDelim << std::endl; - - std::map annotations; - seq->getAnnotations (annotations); - foreach (anno, annotations) - before << " Annotation: " << leftDelim << anno->first << " " << anno->second << rightDelim << std::endl; - - before << " Description: " << leftDelim << seq->getDescription () << rightDelim << std::endl - - << "# Internals" << std::endl - << " Start: " << leftDelim << seq->getAttribute ("start") << rightDelim << std::endl - << " End: " << leftDelim << seq->getAttribute ("end") << rightDelim << std::endl - << " Due: " << leftDelim << seq->getAttribute ("due") << rightDelim << std::endl - << " Recur: " << leftDelim << seq->getAttribute ("recur") << rightDelim << std::endl - << " Mask: " << leftDelim << seq->getAttribute ("mask") << rightDelim << std::endl - << " iMask: " << leftDelim << seq->getAttribute ("imask") << rightDelim << std::endl; - - // Write to file. - spit (file, before.str ()); - - // Determine correct editor: .taskrc:editor > $VISUAL > $EDITOR > vi - std::string editor = conf.get ("editor", ""); - if (editor == "") editor = getenv ("VISUAL"); - if (editor == "") editor = getenv ("EDITOR"); - if (editor == "") editor = "vi"; - - // Launch the editor. - editor += " "; - editor += file; - system (editor.c_str ()); - - // Slurp file. - std::string after; - slurp (file, after, true); - - // Update seq based on what can be parsed back out of the file. - seq->setAttribute ("Project", findValue (after, "Project")); - seq->setAttribute ("Priority", findValue (after, "Priority")); - seq->setDescription ( findValue (after, "Description")); - - // Modify task. - tdb.modifyT (*seq); - - // Cleanup. - unlink (file); - } - - return out.str (); -} - //////////////////////////////////////////////////////////////////////////////// std::string handleColor (Config& conf) { diff --git a/src/edit.cpp b/src/edit.cpp new file mode 100644 index 000000000..0b5723596 --- /dev/null +++ b/src/edit.cpp @@ -0,0 +1,157 @@ +//////////////////////////////////////////////////////////////////////////////// +// task - a command line task list manager. +// +// Copyright 2006 - 2009, Paul Beckingham. +// All rights reserved. +// +// This program is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation; either version 2 of the License, or (at your option) any later +// version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +// details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the +// +// Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, +// Boston, MA +// 02110-1301 +// USA +// +//////////////////////////////////////////////////////////////////////////////// +#include +#include +#include +#include +#include +#include +#include +#include "task.h" + +//////////////////////////////////////////////////////////////////////////////// +static std::string findSimpleValue (const std::string& text, const std::string& name) +{ + // Look for /^\s+name:\s+(.*)$/ + // Extract + // Trim + // Join + return ""; +} + +//////////////////////////////////////////////////////////////////////////////// +static std::string formatTask (T task) +{ + std::stringstream before; + before << "# The 'task edit ' command allows you to modify all aspects of a task" << std::endl + << "# using a text editor. What is shown below is a representation of the" << std::endl + << "# task in all it's detail. Modify what you wish, and if you save and" << std::endl + << "# quit your editor, task will read this file and try to make sense of" << std::endl + << "# what changed, and apply those changes. If you quit your editor without" << std::endl + << "# saving or making any modifications, task will do nothing." << std::endl + << "#" << std::endl + << "# Lines that begin with # are comments, and will be ignored by task." << std::endl + << "# All other edits will be ignored." << std::endl + << "#" << std::endl + << "# Things you probably want to edit" << std::endl + << " ID: " << task.getId () << std::endl + << " Status: " << task.getStatus () << std::endl + << " Project: " << task.getAttribute ("project") << std::endl + << " Priority: " << task.getAttribute ("priority") << std::endl; + + std::vector tags; + task.getTags (tags); + std::string allTags; + join (allTags, " ", tags); + before << " Tags: " << allTags << std::endl; + + std::map annotations; + task.getAnnotations (annotations); + foreach (anno, annotations) + before << " Annotation: " << anno->first << " " << anno->second << std::endl; + + before << " Description: " << task.getDescription () << std::endl + << "#" << std::endl + << "# Things you should be very careful about editing" << std::endl + << " Start: " << task.getAttribute ("start") << std::endl + << " End: " << task.getAttribute ("end") << std::endl + << " Due: " << task.getAttribute ("due") << std::endl + << "#" << std::endl + << "# Things you should not edit, but can" << std::endl + << " Recur: " << task.getAttribute ("recur") << std::endl + << " Mask: " << task.getAttribute ("mask") << std::endl + << " iMask: " << task.getAttribute ("imask") << std::endl; + + return before.str (); +} + +//////////////////////////////////////////////////////////////////////////////// +static void parseTask (T& task, const std::string& before, const std::string& after) +{ + task.setAttribute ("Project", findSimpleValue (after, "Project")); + task.setAttribute ("Priority", findSimpleValue (after, "Priority")); + task.setDescription ( findSimpleValue (after, "Description")); +} + +//////////////////////////////////////////////////////////////////////////////// +// Introducing the Silver Bullet. This feature is the catch-all fixative for +// various other ills. This is like opening up the hood and going in with a +// wrench. To be used sparingly. +std::string handleEdit (TDB& tdb, T& task, Config& conf) +{ + std::stringstream out; + std::vector all; + tdb.pendingT (all); + + filterSequence (all, task); + foreach (seq, all) + { + // Check for file permissions. + std::string dataLocation = expandPath (conf.get ("data.location")); + if (access (dataLocation.c_str (), X_OK)) + throw std::string ("Your data.location directory is not writable."); + + // Create a temp file name in data.location. + std::stringstream pattern; + pattern << dataLocation << "/task." << seq->getId () << ".XXXXXX"; + char cpattern [PATH_MAX]; + strcpy (cpattern, pattern.str ().c_str ()); + char* file = mktemp (cpattern); + + // Format the contents, T -> text, write to a file. + std::string before = formatTask (*seq); + spit (file, before); + + // Determine correct editor: .taskrc:editor > $VISUAL > $EDITOR > vi + std::string editor = conf.get ("editor", ""); + if (editor == "") editor = getenv ("VISUAL"); + if (editor == "") editor = getenv ("EDITOR"); + if (editor == "") editor = "vi"; + + // Launch the editor. + editor += " "; + editor += file; + system (editor.c_str ()); + + // Slurp file. + std::string after; + slurp (file, after, true); + + // Update seq based on what can be parsed back out of the file. + parseTask (*seq, before, after); + + // Modify task. + tdb.modifyT (*seq); + + // Cleanup. + unlink (file); + } + + return out.str (); +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/task.h b/src/task.h index 803aac0e6..7b4a5e9a7 100644 --- a/src/task.h +++ b/src/task.h @@ -90,7 +90,6 @@ std::string handleUndo (TDB&, T&, Config&); std::string handleColor (Config&); std::string handleAnnotate (TDB&, T&, Config&); std::string handleDuplicate (TDB&, T&, Config&); -std::string handleEdit (TDB&, T&, Config&); T findT (int, const std::vector &); int deltaAppend (T&, T&); int deltaDescription (T&, T&); @@ -98,6 +97,9 @@ int deltaTags (T&, T&); int deltaAttributes (T&, T&); int deltaSubstitutions (T&, T&); +// edit.cpp +std::string handleEdit (TDB&, T&, Config&); + // report.cpp void filterSequence (std::vector&, T&); void filter (std::vector&, T&);