diff --git a/src/Context.cpp b/src/Context.cpp index 37969785f..e534f6b08 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -53,6 +53,7 @@ Context::Context () , tdb () , tdb2 () , program ("") +, commandLine ("") , file_override ("") , var_overrides ("") , cmd () @@ -73,27 +74,8 @@ Context::~Context () //////////////////////////////////////////////////////////////////////////////// void Context::initialize2 (int argc, char** argv) { - // TODO Capture the args. - // TODO Capture any stdin args. - // TODO Scan for rc: overrides --> apply. - // TODO Combine command line into one string. - // TODO Load relevant rc file. + Timer t ("Context::initialize2"); - // TODO Instantiate built-in command objects. - commands.push_back (Command::factory ("install")); - - // TODO Instantiate extension command objects. - // TODO Instantiate default command object. - // TODO Instantiate column objects. - // TODO Instantiate UDA objects. - // TODO Instantiate format objects. - // TODO Instantiate extension format objects. - // TODO Hook: on-launch -} - -//////////////////////////////////////////////////////////////////////////////// -void Context::initialize (int argc, char** argv) -{ // Capture the args. for (int i = 0; i < argc; ++i) { @@ -129,6 +111,34 @@ void Context::initialize (int argc, char** argv) } } + // TODO Scan for rc: overrides --> apply. + + // Combine command line into one string. + join (commandLine, " ", args); + + // TODO Load relevant rc file. + + // Instantiate built-in command objects. + commands.push_back (Command::factory ("exec")); + commands.push_back (Command::factory ("install")); + commands.push_back (Command::factory ("_logo")); + + // TODO Instantiate extension command objects. + // TODO Instantiate default command object. + // TODO Instantiate extension UDA objects. + // TODO Instantiate extension format objects. + // TODO Hook: on-launch +} + +//////////////////////////////////////////////////////////////////////////////// +void Context::initialize (int argc, char** argv) +{ + // Capture the args. + // ... + + // Capture any stdin args. + // ... + initialize (); // Hook system init, plus post-start event occurring at the first possible @@ -182,8 +192,10 @@ int Context::run () std::string output; try { - parse (); // Parse command line. - rc = dispatch (output); // Dispatch to command handlers. + parse (); // Parse command line. + rc = dispatch2 (output); // Dispatch to new command handlers. + if (rc) + rc = dispatch (output); // Dispatch to old command handlers. } catch (const std::string& error) @@ -229,6 +241,28 @@ int Context::run () return rc; } +//////////////////////////////////////////////////////////////////////////////// +int Context::dispatch2 (std::string &out) +{ + Timer t ("Context::dispatch2"); + + updateXtermTitle (); + + std::vector ::iterator c; + for (c = commands.begin (); c != commands.end (); ++c) + { + if ((*c)->implements (commandLine)) + { + if (! (*c)->read_only ()) + tdb.gc (); + + return (*c)->execute (commandLine, out); + } + } + + return 1; +} + //////////////////////////////////////////////////////////////////////////////// int Context::dispatch (std::string &out) { @@ -236,16 +270,7 @@ int Context::dispatch (std::string &out) Timer t ("Context::dispatch"); - // For read-only commands, optionally update the xterm window title. - // Why just the read-only commands? Because this capability is to answer the - // question of 'what did I just do to generate this outout?'. - if (config.getBoolean ("xterm.title") && - cmd.isReadOnlyCommand ()) - { - std::string title; - join (title, " ", args); - std::cout << "]0;task " << title << "" << std::endl; - } + updateXtermTitle (); // TODO Chain-of-command pattern dispatch. @@ -881,6 +906,7 @@ void Context::clear () tdb.clear (); // TODO Obsolete // tdb2.clear (); program = ""; + commandLine = ""; args.clear (); file_override = ""; var_overrides = ""; @@ -998,6 +1024,19 @@ void Context::autoFilter (Filter& f) } } +//////////////////////////////////////////////////////////////////////////////// +// This capability is to answer the question of 'what did I just do to generate +// this output?'. +void Context::updateXtermTitle () +{ + if (config.getBoolean ("xterm.title")) + { + std::string title; + join (title, " ", args); + std::cout << "]0;task " << title << "" << std::endl; + } +} + //////////////////////////////////////////////////////////////////////////////// void Context::header (const std::string& input) { diff --git a/src/Context.h b/src/Context.h index 86d2a854a..cd316987c 100644 --- a/src/Context.h +++ b/src/Context.h @@ -53,6 +53,7 @@ public: void initialize2 (int, char**); // all startup void initialize (); // for reinitializing int run (); // task classic + int dispatch2 (std::string&); // command handler dispatch int dispatch (std::string&); // command handler dispatch void shadow (); // shadow file update @@ -81,6 +82,7 @@ private: void loadAliases (); void autoFilter (Att&, Filter&); void autoFilter (Filter&); + void updateXtermTitle (); public: Config config; @@ -92,6 +94,7 @@ public: TDB2 tdb2; std::string program; std::vector args; + std::string commandLine; std::string file_override; std::string var_overrides; Cmd cmd; // TODO Obsolete @@ -109,7 +112,6 @@ public: std::vector debugMessages; bool inShadow; - std::vector columns; std::vector commands; int terminal_width; diff --git a/src/commands/CMakeLists.txt b/src/commands/CMakeLists.txt index 12d688740..03c6a3188 100644 --- a/src/commands/CMakeLists.txt +++ b/src/commands/CMakeLists.txt @@ -5,6 +5,7 @@ include_directories (${CMAKE_SOURCE_DIR}/src ${TASK_INCLUDE_DIRS}) set (commands_SRCS Command.cpp Command.h + CmdExec.cpp CmdExec.h CmdInstall.cpp CmdInstall.h CmdLogo.cpp CmdLogo.h) diff --git a/src/commands/CmdExec.cpp b/src/commands/CmdExec.cpp new file mode 100644 index 000000000..cb3db5f2e --- /dev/null +++ b/src/commands/CmdExec.cpp @@ -0,0 +1,58 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// 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 + +extern Context context; + +//////////////////////////////////////////////////////////////////////////////// +CmdExec::CmdExec () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +CmdExec::~CmdExec () +{ +} + +//////////////////////////////////////////////////////////////////////////////// +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) +{ + std::cout << "# CmdExec::execute\n"; + return 1; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdExec.h b/src/commands/CmdExec.h new file mode 100644 index 000000000..424b0f165 --- /dev/null +++ b/src/commands/CmdExec.h @@ -0,0 +1,46 @@ +//////////////////////////////////////////////////////////////////////////////// +// taskwarrior - a command line task list manager. +// +// Copyright 2006 - 2011, Paul Beckingham, Federico Hernandez. +// 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 +// +//////////////////////////////////////////////////////////////////////////////// +#ifndef INCLUDED_CMDEXEC +#define INCLUDED_CMDEXEC + +#include +#include + +class CmdExec : public Command +{ +public: + CmdExec (); + ~CmdExec (); + + bool implements (const std::string&) const; + int execute (const std::string&, std::string&); + +private: +}; + +#endif +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdInstall.cpp b/src/commands/CmdInstall.cpp index 432273176..4ae232675 100644 --- a/src/commands/CmdInstall.cpp +++ b/src/commands/CmdInstall.cpp @@ -33,61 +33,18 @@ extern Context context; //////////////////////////////////////////////////////////////////////////////// CmdInstall::CmdInstall () -/* -: _name ("") -*/ { } -//////////////////////////////////////////////////////////////////////////////// -CmdInstall::CmdInstall (const CmdInstall& other) -{ -/* - _minimum = other._minimum; -*/ -} - -//////////////////////////////////////////////////////////////////////////////// -CmdInstall& CmdInstall::operator= (const CmdInstall& other) -{ - if (this != &other) - { -/* - _name = other._name; -*/ - } - - return *this; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CmdInstall::operator== (const CmdInstall& other) const -{ - return false; -/* - return _name == other._name && - _minimum == other._minimum && - _maximum == other._maximum && - _wrap == other._wrap && - _just == other._just && - _sizing == other._sizing; -*/ -} - //////////////////////////////////////////////////////////////////////////////// CmdInstall::~CmdInstall () { } -//////////////////////////////////////////////////////////////////////////////// -bool CmdInstall::read_only () const -{ - return false; -} - //////////////////////////////////////////////////////////////////////////////// bool CmdInstall::implements (const std::string& command_line) const { + std::cout << "# CmdInstall::implements\n"; return false; } @@ -97,9 +54,10 @@ bool CmdInstall::implements (const std::string& command_line) const // Generate UUID // Call the "install" function once, store results in rc: // extension.= -std::string CmdInstall::execute (const std::string&) +int CmdInstall::execute (const std::string& commandLine, std::string& output) { - return "output"; + std::cout << "# CmdInstall::execute\n"; + return 1; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdInstall.h b/src/commands/CmdInstall.h index 14c0eb574..37c6fc759 100644 --- a/src/commands/CmdInstall.h +++ b/src/commands/CmdInstall.h @@ -34,14 +34,10 @@ class CmdInstall : public Command { public: CmdInstall (); - CmdInstall (const CmdInstall&); - CmdInstall& operator= (const CmdInstall&); - bool operator== (const CmdInstall&) const; // TODO Is this necessary? ~CmdInstall (); - bool read_only () const; bool implements (const std::string&) const; - std::string execute (const std::string&); + int execute (const std::string&, std::string&); private: }; diff --git a/src/commands/CmdLogo.cpp b/src/commands/CmdLogo.cpp index e8536de8a..b03909afd 100644 --- a/src/commands/CmdLogo.cpp +++ b/src/commands/CmdLogo.cpp @@ -28,52 +28,15 @@ #include #include #include +#include extern Context context; //////////////////////////////////////////////////////////////////////////////// CmdLogo::CmdLogo () -/* -: _name ("") -*/ { } -//////////////////////////////////////////////////////////////////////////////// -CmdLogo::CmdLogo (const CmdLogo& other) -{ -/* - _minimum = other._minimum; -*/ -} - -//////////////////////////////////////////////////////////////////////////////// -CmdLogo& CmdLogo::operator= (const CmdLogo& other) -{ - if (this != &other) - { -/* - _name = other._name; -*/ - } - - return *this; -} - -//////////////////////////////////////////////////////////////////////////////// -bool CmdLogo::operator== (const CmdLogo& other) const -{ - return false; -/* - return _name == other._name && - _minimum == other._minimum && - _maximum == other._maximum && - _wrap == other._wrap && - _just == other._just && - _sizing == other._sizing; -*/ -} - //////////////////////////////////////////////////////////////////////////////// CmdLogo::~CmdLogo () { @@ -82,12 +45,16 @@ CmdLogo::~CmdLogo () //////////////////////////////////////////////////////////////////////////////// bool CmdLogo::read_only () const { - return false; + return true; } //////////////////////////////////////////////////////////////////////////////// bool CmdLogo::implements (const std::string& command_line) const { + // TODO Upgrade to a parsed value. + if (command_line.find ("_logo") != std::string::npos) + return true; + return false; } @@ -97,7 +64,7 @@ bool CmdLogo::implements (const std::string& command_line) const // Generate UUID // Call the "install" function once, store results in rc: // extension.= -std::string CmdLogo::execute (const std::string&) +int CmdLogo::execute (const std::string& commandLine, std::string& output) { static const char* data[] = { @@ -132,10 +99,10 @@ std::string CmdLogo::execute (const std::string&) "" }; - std::string output; + output += optionalBlankLine (); + for (int line = 0; data[line][0]; ++line) { - for (int c = 0; c < 14; ++c) { int value = (int) data[line][c]; @@ -163,9 +130,13 @@ std::string CmdLogo::execute (const std::string&) output += block; } } + + output += "\n"; } - return output; + output += optionalBlankLine (); + + return 0; } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/CmdLogo.h b/src/commands/CmdLogo.h index 0f742cb20..75e044326 100644 --- a/src/commands/CmdLogo.h +++ b/src/commands/CmdLogo.h @@ -34,14 +34,11 @@ class CmdLogo : public Command { public: CmdLogo (); - CmdLogo (const CmdLogo&); - CmdLogo& operator= (const CmdLogo&); - bool operator== (const CmdLogo&) const; // TODO Is this necessary? ~CmdLogo (); bool read_only () const; bool implements (const std::string&) const; - std::string execute (const std::string&); + int execute (const std::string&, std::string&); private: }; diff --git a/src/commands/Command.cpp b/src/commands/Command.cpp index 0b900e2c4..9764cd2a6 100644 --- a/src/commands/Command.cpp +++ b/src/commands/Command.cpp @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -37,10 +38,13 @@ extern Context context; Command* Command::factory (const std::string& name) { Command* command; - if (name == "install") command = new CmdInstall (); + if (name == "exec") command = new CmdExec (); + else if (name == "install") command = new CmdInstall (); else if (name == "_logo") command = new CmdLogo (); else - throw std::string ("Unrecognized command '") + name + "'"; + throw std::string ("Unrecognized command object '") + name + "'"; + + // TODO Initialize command object. return command; } @@ -71,12 +75,6 @@ Command& Command::operator= (const Command& other) return *this; } -//////////////////////////////////////////////////////////////////////////////// -bool Command::read_only () const -{ - return false; -} - //////////////////////////////////////////////////////////////////////////////// bool Command::operator== (const Command& other) const { @@ -97,3 +95,10 @@ Command::~Command () } //////////////////////////////////////////////////////////////////////////////// +bool Command::read_only () const +{ + std::cout << "# Command::read_only\n"; + return false; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/commands/Command.h b/src/commands/Command.h index d45a6f255..8a0178b10 100644 --- a/src/commands/Command.h +++ b/src/commands/Command.h @@ -42,7 +42,7 @@ public: virtual bool read_only () const; virtual bool implements (const std::string&) const = 0; - virtual std::string execute (const std::string&) = 0; + virtual int execute (const std::string&, std::string&) = 0; private: }; diff --git a/src/interactive.cpp b/src/interactive.cpp index 8c5d8c8fe..c3d0294bd 100644 --- a/src/interactive.cpp +++ b/src/interactive.cpp @@ -55,7 +55,7 @@ int Context::getWidth () terminal_width = buff[1]; std::stringstream out; - out << "Context::getWidth: determined width of " << width << " characters"; + out << "Context::getWidth: determined width of " << terminal_width << " characters"; debug (out.str ()); } } diff --git a/src/main.cpp b/src/main.cpp index d4db3f5dd..41c315445 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,6 +63,7 @@ int main (int argc, char** argv) try { + context.initialize2 (argc, argv); context.initialize (argc, argv); status = context.run (); }