From afc97d566caed7d6b62bb312388234292c4a328d Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 15 May 2011 00:18:24 -0400 Subject: [PATCH] Commands - New 'exec' command that runs external programs, implemented by CmdExec. - Restructured code so that only Commands implements ::read_only and ::displays_id methods, while derived objects simply set flags. - Corrected column alignment in summary report. --- ChangeLog | 1 + NEWS | 1 + src/commands/CmdExec.cpp | 33 +++++++++++++++++++++++---------- src/commands/CmdExec.h | 4 ++-- src/commands/CmdInstall.cpp | 13 +++++-------- src/commands/CmdInstall.h | 3 +-- src/commands/CmdLogo.cpp | 17 ++++------------- src/commands/CmdLogo.h | 4 +--- src/commands/Command.cpp | 21 +++++++++++++++------ src/commands/Command.h | 9 +++++++-- src/report.cpp | 6 +++--- 11 files changed, 63 insertions(+), 49 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3bf46f424..e28469f60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -32,6 +32,7 @@ 'blanklines'. + The 'verbose' configuration variable now accepts a specific list of items to be verbose about. See taskrc(5). + + New 'exec' command that runs external programs. # Tracked Features, sorted by ID. + Added feature #330, which supports the 'inverse' color attribute. diff --git a/NEWS b/NEWS index 2159e7eb2..fa1117f06 100644 --- a/NEWS +++ b/NEWS @@ -16,6 +16,7 @@ New Features in taskwarrior 2.0.0 - Performance enhancements. - New 'next' report, that gauges urgency and reports the most urgent tasks. - Fine control of verbosity through the 'verbose=' configuration variable. + - New 'exec' command that runs external scripts/programs. Please refer to the ChangeLog file for full details. There are too many to list here. diff --git a/src/commands/CmdExec.cpp b/src/commands/CmdExec.cpp index cb3db5f2e..055e5e1db 100644 --- a/src/commands/CmdExec.cpp +++ b/src/commands/CmdExec.cpp @@ -25,7 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// -#include +#include #include #include @@ -33,26 +33,39 @@ extern Context context; //////////////////////////////////////////////////////////////////////////////// CmdExec::CmdExec () +: _external_command ("") { + _read_only = false; + _displays_id = true; } //////////////////////////////////////////////////////////////////////////////// -CmdExec::~CmdExec () +bool CmdExec::implements (const std::string& command_line) { -} + if (context.args.size () > 1 && + (context.args[0] == "exec" || + context.args[0] == "exe" || + context.args[0] == "ex")) + { + for (int i = 1; i < context.args.size (); ++i) + { + if (i > 1) + _external_command += " "; + + _external_command += context.args[i]; + } + + return true; + } -//////////////////////////////////////////////////////////////////////////////// -bool CmdExec::implements (const std::string& command_line) const -{ - std::cout << "# CmdExec::implements\n"; return false; } //////////////////////////////////////////////////////////////////////////////// -int CmdExec::execute (const std::string& commandLine, std::string& output) +int CmdExec::execute (const std::string&, std::string&) { - std::cout << "# CmdExec::execute\n"; - return 1; + system (_external_command.c_str ()); + return 0; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdExec.h b/src/commands/CmdExec.h index 424b0f165..3e19ce298 100644 --- a/src/commands/CmdExec.h +++ b/src/commands/CmdExec.h @@ -34,12 +34,12 @@ class CmdExec : public Command { public: CmdExec (); - ~CmdExec (); - bool implements (const std::string&) const; + bool implements (const std::string&); int execute (const std::string&, std::string&); private: + std::string _external_command; }; #endif diff --git a/src/commands/CmdInstall.cpp b/src/commands/CmdInstall.cpp index 4ae232675..6cf1cfc90 100644 --- a/src/commands/CmdInstall.cpp +++ b/src/commands/CmdInstall.cpp @@ -25,7 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// -#include +#include // TODO Remove. #include #include @@ -34,17 +34,14 @@ extern Context context; //////////////////////////////////////////////////////////////////////////////// CmdInstall::CmdInstall () { + _read_only = true; + _displays_id = false; } //////////////////////////////////////////////////////////////////////////////// -CmdInstall::~CmdInstall () +bool CmdInstall::implements (const std::string& command_line) { -} - -//////////////////////////////////////////////////////////////////////////////// -bool CmdInstall::implements (const std::string& command_line) const -{ - std::cout << "# CmdInstall::implements\n"; + std::cout << "# CmdInstall::implements '" << command_line << "'\n"; return false; } diff --git a/src/commands/CmdInstall.h b/src/commands/CmdInstall.h index 37c6fc759..c22c206ef 100644 --- a/src/commands/CmdInstall.h +++ b/src/commands/CmdInstall.h @@ -34,9 +34,8 @@ class CmdInstall : public Command { public: CmdInstall (); - ~CmdInstall (); - bool implements (const std::string&) const; + bool implements (const std::string&); int execute (const std::string&, std::string&); private: diff --git a/src/commands/CmdLogo.cpp b/src/commands/CmdLogo.cpp index bd7dd6cd9..33591d58b 100644 --- a/src/commands/CmdLogo.cpp +++ b/src/commands/CmdLogo.cpp @@ -25,7 +25,7 @@ // //////////////////////////////////////////////////////////////////////////////// -#include +#include // TODO Remove. #include #include #include @@ -35,21 +35,12 @@ extern Context context; //////////////////////////////////////////////////////////////////////////////// CmdLogo::CmdLogo () { + _read_only = true; + _displays_id = false; } //////////////////////////////////////////////////////////////////////////////// -CmdLogo::~CmdLogo () -{ -} - -//////////////////////////////////////////////////////////////////////////////// -bool CmdLogo::read_only () const -{ - return true; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CmdLogo::implements (const std::string& command_line) const +bool CmdLogo::implements (const std::string& command_line) { // TODO Upgrade to a parsed value. if (command_line.find ("_logo") != std::string::npos) diff --git a/src/commands/CmdLogo.h b/src/commands/CmdLogo.h index 75e044326..bd9ca4c25 100644 --- a/src/commands/CmdLogo.h +++ b/src/commands/CmdLogo.h @@ -34,10 +34,8 @@ class CmdLogo : public Command { public: CmdLogo (); - ~CmdLogo (); - bool read_only () const; - bool implements (const std::string&) const; + bool implements (const std::string&); int execute (const std::string&, std::string&); private: diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index 752d0ffcd..333c671db 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -51,13 +51,16 @@ Command* Command::factory (const std::string& name) //////////////////////////////////////////////////////////////////////////////// Command::Command () +: _read_only (true) +, _displays_id (true) { } //////////////////////////////////////////////////////////////////////////////// Command::Command (const Command& other) { -// _all = other._all; + _read_only = other._read_only; + _displays_id = other._displays_id; } //////////////////////////////////////////////////////////////////////////////// @@ -65,7 +68,8 @@ Command& Command::operator= (const Command& other) { if (this != &other) { -// _all = other._all; + _read_only = other._read_only; + _displays_id = other._displays_id; } return *this; @@ -74,8 +78,8 @@ Command& Command::operator= (const Command& other) //////////////////////////////////////////////////////////////////////////////// bool Command::operator== (const Command& other) const { -// return _all == other._all; - return false; + return _read_only == other._read_only && + _displays_id == other._displays_id; } //////////////////////////////////////////////////////////////////////////////// @@ -86,8 +90,13 @@ Command::~Command () //////////////////////////////////////////////////////////////////////////////// bool Command::read_only () const { - std::cout << "# Command::read_only\n"; - return false; + return _read_only; +} + +//////////////////////////////////////////////////////////////////////////////// +bool Command::displays_id () const +{ + return _displays_id; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/Command.h b/src/commands/Command.h index 88068221d..f07ceb2a2 100644 --- a/src/commands/Command.h +++ b/src/commands/Command.h @@ -40,9 +40,14 @@ public: static Command* factory (const std::string&); - virtual bool read_only () const; - virtual bool implements (const std::string&) const = 0; + bool read_only () const; + bool displays_id () const; + virtual bool implements (const std::string&) = 0; virtual int execute (const std::string&, std::string&) = 0; + +protected: + bool _read_only; + bool _displays_id; }; #endif diff --git a/src/report.cpp b/src/report.cpp index ba02f2a22..f05c335ce 100644 --- a/src/report.cpp +++ b/src/report.cpp @@ -788,9 +788,9 @@ int handleReportSummary (std::string& outs) ViewText view; view.width (context.getWidth ()); view.add (Column::factory ("string", "Project")); - view.add (Column::factory ("string", "Remaining")); - view.add (Column::factory ("string", "Avg age")); - view.add (Column::factory ("string", "Complete")); + view.add (Column::factory ("string.right", "Remaining")); + view.add (Column::factory ("string.right", "Avg age")); + view.add (Column::factory ("string.right", "Complete")); view.add (Column::factory ("string", "0% 100%")); Color bar_color (context.config.get ("color.summary.bar"));