Commands - shell

- Migrated handleShell to CmdShell.
- Note there is a segfault bug that will probably go away by itself when
  Context::parse is obsoleted.
This commit is contained in:
Paul Beckingham 2011-05-28 14:22:38 -04:00
parent 4857269d12
commit b075f1252c
9 changed files with 161 additions and 76 deletions

View file

@ -164,7 +164,6 @@ void Cmd::load ()
commands.push_back ("log"); commands.push_back ("log");
commands.push_back ("prepend"); commands.push_back ("prepend");
commands.push_back ("projects"); commands.push_back ("projects");
commands.push_back ("shell");
commands.push_back ("start"); commands.push_back ("start");
commands.push_back ("stats"); commands.push_back ("stats");
commands.push_back ("stop"); commands.push_back ("stop");
@ -256,7 +255,6 @@ bool Cmd::isReadOnlyCommand ()
command == "help" || command == "help" ||
command == "projects" || command == "projects" ||
command == "push" || command == "push" ||
command == "shell" ||
command == "stats" || command == "stats" ||
command == "summary" || command == "summary" ||
command == "timesheet" || command == "timesheet" ||

View file

@ -276,7 +276,6 @@ int Context::dispatch (std::string &out)
else if (cmd.command == "export.yaml") { rc = handleExportYAML (out); } else if (cmd.command == "export.yaml") { rc = handleExportYAML (out); }
else if (cmd.command == "import") { rc = handleImport (out); } else if (cmd.command == "import") { rc = handleImport (out); }
else if (cmd.command == "duplicate") { rc = handleDuplicate (out); } else if (cmd.command == "duplicate") { rc = handleDuplicate (out); }
else if (cmd.command == "shell") { handleShell ( ); }
else if (cmd.command == "undo") { handleUndo ( ); } else if (cmd.command == "undo") { handleUndo ( ); }
else if (cmd.command == "merge") { tdb.gc (); else if (cmd.command == "merge") { tdb.gc ();
handleMerge (out); } handleMerge (out); }

View file

@ -1792,74 +1792,6 @@ int handleIds (std::string& outs)
return rc; return rc;
} }
////////////////////////////////////////////////////////////////////////////////
// TODO Obsolete.
void handleShell ()
{
// Display some kind of welcome message.
Color bold (Color::nocolor, Color::nocolor, false, true, false);
std::cout << (context.color () ? bold.colorize (PACKAGE_STRING) : PACKAGE_STRING)
<< " shell\n\n"
<< "Enter any task command (such as 'list'), or hit 'Enter'.\n"
<< "There is no need to include the 'task' command itself.\n"
<< "Enter 'quit' (or 'bye', 'exit') to end the session.\n\n";
// Make a copy because context.clear will delete them.
std::string permanentOverrides = " " + context.file_override
+ " " + context.var_overrides;
std::vector <std::string> quit_commands;
quit_commands.push_back ("quit");
quit_commands.push_back ("exit");
quit_commands.push_back ("bye");
std::string command;
bool keepGoing = true;
do
{
std::cout << context.config.get ("shell.prompt") << " ";
command = "";
std::getline (std::cin, command);
std::string decoratedCommand = trim (command + permanentOverrides);
// When looking for the 'quit' command, use 'command', not
// 'decoratedCommand'.
if (std::find (quit_commands.begin (), quit_commands.end (), lowerCase (command)) != quit_commands.end ())
{
keepGoing = false;
}
else
{
try
{
context.clear ();
std::vector <std::string> args;
split (args, decoratedCommand, ' ');
foreach (arg, args) context.args.push_back (*arg);
context.initialize (0, NULL);
context.run ();
}
catch (std::string& error)
{
std::cout << error << "\n";
}
catch (...)
{
std::cerr << "Unknown error." << "\n";
}
}
}
while (keepGoing && !std::cin.eof ());
// No need to repeat any overrides after the shell quits.
context.clearMessages ();
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int handleColor (std::string& outs) int handleColor (std::string& outs)
{ {

View file

@ -14,6 +14,7 @@ set (commands_SRCS Command.cpp Command.h
CmdInfo.cpp CmdInfo.h CmdInfo.cpp CmdInfo.h
CmdInstall.cpp CmdInstall.h CmdInstall.cpp CmdInstall.h
CmdLogo.cpp CmdLogo.h CmdLogo.cpp CmdLogo.h
CmdShell.cpp CmdShell.h
CmdShow.cpp CmdShow.h CmdShow.cpp CmdShow.h
CmdTags.cpp CmdTags.h CmdTags.cpp CmdTags.h
CmdTip.cpp CmdTip.h CmdTip.cpp CmdTip.h

View file

@ -131,10 +131,6 @@ int CmdHelp::execute (const std::string& command_line, std::string& output)
view.set (row, 1, "task undo"); view.set (row, 1, "task undo");
view.set (row, 2, "Reverts the most recent action."); view.set (row, 2, "Reverts the most recent action.");
row = view.addRow ();
view.set (row, 1, "task shell");
view.set (row, 2, "Launches an interactive shell.");
row = view.addRow (); row = view.addRow ();
view.set (row, 1, "task duplicate ID [tags] [attrs] [desc...]"); view.set (row, 1, "task duplicate ID [tags] [attrs] [desc...]");
view.set (row, 2, "Duplicates the specified task, and allows modifications."); view.set (row, 2, "Duplicates the specified task, and allows modifications.");

116
src/commands/CmdShell.cpp Normal file
View file

@ -0,0 +1,116 @@
////////////////////////////////////////////////////////////////////////////////
// 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 <iostream>
#include <Color.h>
#include <Context.h>
#include <text.h>
#include <CmdShell.h>
extern Context context;
////////////////////////////////////////////////////////////////////////////////
CmdShell::CmdShell ()
{
_keyword = "shell";
_usage = "task shell";
_description = "Launches an interactive shell";
_read_only = false;
_displays_id = true;
}
////////////////////////////////////////////////////////////////////////////////
int CmdShell::execute (const std::string& command_line, std::string& output)
{
// Display some kind of welcome message.
Color bold (Color::nocolor, Color::nocolor, false, true, false);
std::cout << (context.color () ? bold.colorize (PACKAGE_STRING) : PACKAGE_STRING)
<< " shell\n\n"
<< "Enter any task command (such as 'list'), or hit 'Enter'.\n"
<< "There is no need to include the 'task' command itself.\n"
<< "Enter 'quit' (or 'bye', 'exit') to end the session.\n\n";
// Make a copy because context.clear will delete them.
std::string permanentOverrides = " " + context.file_override
+ " " + context.var_overrides;
std::vector <std::string> quit_commands;
quit_commands.push_back ("quit");
quit_commands.push_back ("exit");
quit_commands.push_back ("bye");
std::string command;
bool keepGoing = true;
do
{
std::cout << context.config.get ("shell.prompt") << " ";
command = "";
std::getline (std::cin, command);
std::string decoratedCommand = trim (command + permanentOverrides);
// When looking for the 'quit' command, use 'command', not
// 'decoratedCommand'.
if (std::find (quit_commands.begin (), quit_commands.end (), lowerCase (command)) != quit_commands.end ())
{
keepGoing = false;
}
else
{
try
{
context.clear ();
std::vector <std::string> args;
split (args, decoratedCommand, ' ');
std::vector <std::string>::iterator arg;
for (arg = args.begin (); arg != args.end (); ++arg)
context.args.push_back (*arg);
context.initialize (0, NULL);
context.run ();
}
catch (std::string& error)
{
std::cout << error << "\n";
}
catch (...)
{
std::cerr << "Unknown error." << "\n";
}
}
}
while (keepGoing && !std::cin.eof ());
// No need to repeat any overrides after the shell quits.
context.clearMessages ();
return 0;
}
////////////////////////////////////////////////////////////////////////////////

42
src/commands/CmdShell.h Normal file
View file

@ -0,0 +1,42 @@
////////////////////////////////////////////////////////////////////////////////
// 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_CMDSHELL
#define INCLUDED_CMDSHELL
#define L10N // Localization complete.
#include <string>
#include <Command.h>
class CmdShell : public Command
{
public:
CmdShell ();
int execute (const std::string&, std::string&);
};
#endif
////////////////////////////////////////////////////////////////////////////////

View file

@ -36,6 +36,7 @@
#include <CmdInfo.h> #include <CmdInfo.h>
#include <CmdInstall.h> #include <CmdInstall.h>
#include <CmdLogo.h> #include <CmdLogo.h>
#include <CmdShell.h>
#include <CmdShow.h> #include <CmdShow.h>
#include <CmdTags.h> #include <CmdTags.h>
#include <CmdTip.h> #include <CmdTip.h>
@ -58,6 +59,7 @@ void Command::factory (std::map <std::string, Command*>& all)
c = new CmdInfo (); all[c->keyword ()] = c; c = new CmdInfo (); all[c->keyword ()] = c;
c = new CmdInstall (); all[c->keyword ()] = c; c = new CmdInstall (); all[c->keyword ()] = c;
c = new CmdLogo (); all[c->keyword ()] = c; c = new CmdLogo (); all[c->keyword ()] = c;
c = new CmdShell (); all[c->keyword ()] = c;
c = new CmdShow (); all[c->keyword ()] = c; c = new CmdShow (); all[c->keyword ()] = c;
c = new CmdTags (); all[c->keyword ()] = c; c = new CmdTags (); all[c->keyword ()] = c;
c = new CmdTip (); all[c->keyword ()] = c; c = new CmdTip (); all[c->keyword ()] = c;

View file

@ -79,7 +79,6 @@ void handleUndo ();
void handleMerge (std::string&); void handleMerge (std::string&);
void handlePush (std::string&); void handlePush (std::string&);
void handlePull (std::string&); void handlePull (std::string&);
void handleShell ();
int deltaAppend (Task&); int deltaAppend (Task&);
int deltaPrepend (Task&); int deltaPrepend (Task&);
int deltaDescription (Task&); int deltaDescription (Task&);