From 60a7d2128edd1683a3c54f0f526322f03dc9afe7 Mon Sep 17 00:00:00 2001 From: Haitham Gad Date: Sat, 9 Mar 2013 21:57:07 -0500 Subject: [PATCH] Fixing tests and updating documentation. --- doc/misc/tutorial/shell/run.200 | 33 ++--- doc/misc/tutorial/shell/script.200.txt | 2 +- src/en-US.h | 8 +- src/shell/Readline.cpp | 8 ++ src/shell/Readline.h | 1 + src/shell/main.cpp | 166 ++++++++++++++++--------- test/shell.t | 6 +- 7 files changed, 139 insertions(+), 85 deletions(-) diff --git a/doc/misc/tutorial/shell/run.200 b/doc/misc/tutorial/shell/run.200 index 5439eb75b..bfcfb2bec 100755 --- a/doc/misc/tutorial/shell/run.200 +++ b/doc/misc/tutorial/shell/run.200 @@ -306,20 +306,7 @@ printf "y\ny\n" | task 11 delete # y, y echo $ task list task list -# 17 Shell -echo Shell -------------------------------------------------------------- -echo $ task shell -#task shell -echo '> projects' -#> projects -echo '> tags' -#> tags -echo '> list' -#> list -echo '> quit' -#> quit - -# 18 Special tags +# 17 Special tags echo Special tags -------------------------------------------------------------- echo $ task 6 +nocolor task 6 +nocolor @@ -330,7 +317,7 @@ task tags echo $ task 6 -nocolor task 6 -nocolor -# 19 Waiting +# 18 Waiting echo Waiting -------------------------------------------------------------- echo $ task add Look for new apartment due:eoy task add Look for new apartment due:eoy @@ -354,7 +341,7 @@ task list echo $ task 11 rc.confirmation:no delete task 11 rc.confirmation:no delete -# 20 Dependencies +# 19 Dependencies echo Dependencies -------------------------------------------------------------- echo $ task list pro:party task list pro:party @@ -385,7 +372,7 @@ echo 'y' | task 2 done echo $ task 1 info task 1 info -# 21 Reports +# 20 Reports echo Reports -------------------------------------------------------------- echo $ task minimal task minimal @@ -414,7 +401,7 @@ task timesheet echo $ task next task next -# 22 Custom report +# 21 Custom report echo Custom report -------------------------------------------------------------- echo 'report.foo.description=My own report' >> x echo 'report.foo.columns=id,entry,description' >> x @@ -428,7 +415,7 @@ task show report.foo echo $ task foo task foo -# 23 Charts +# 22 Charts echo Charts -------------------------------------------------------------- echo $ task history task history @@ -441,7 +428,7 @@ task ghistory.annual echo $ task summary task summary -# 24 Advanced filters +# 23 Advanced filters echo Advanced filters -------------------------------------------------------------- echo $ task list task list @@ -470,7 +457,7 @@ task all status:pending echo $ task all status:waiting task all status:waiting -# 25 Import/export +# 24 Import/export echo Import/export -------------------------------------------------------------- echo $ task export.csv task export.csv @@ -507,7 +494,7 @@ printf "y\n" | task import file.yaml echo $ task new limit:2 task new limit:2 -# 26 Help +# 25 Help echo Help -------------------------------------------------------------- echo $ task help task help @@ -524,7 +511,7 @@ echo $ man task-faq echo $ man task-sync #man task-sync -# 27 Wrap up +# 26 Wrap up echo Wrap up -------------------------------------------------------------- echo $ task version task version diff --git a/doc/misc/tutorial/shell/script.200.txt b/doc/misc/tutorial/shell/script.200.txt index 2c1403815..b252661d6 100644 --- a/doc/misc/tutorial/shell/script.200.txt +++ b/doc/misc/tutorial/shell/script.200.txt @@ -10,7 +10,7 @@ task-tutorial.5 man page. ---------------------------------------- Shell ----------------------------------------------------- -task shell You can use the shell command to create a more immersive +tasksh You can use the tasksh shell to create a more immersive task> projects environment. Any task command you run outside the shell task> tags can also be run inside the shell, without the need to prefix task> list every command with "task". diff --git a/src/en-US.h b/src/en-US.h index 927ac102c..744e3ee7e 100644 --- a/src/en-US.h +++ b/src/en-US.h @@ -970,7 +970,13 @@ #define STRING_UTIL_BYTES "B" // shell -#define STRING_SHELL_UNKOWN_OPTION "Ignoring unkown option: " +#define STRING_SHELL_USAGE \ + "Usage: tasksh [] Execute task commands inside if given,\n" \ + " or otherwise, start interactive task shell.\n" \ + " tasksh --version Print task version.\n" \ + " tasksh --help Print this help.\n" + +#define STRING_SHELL_NO_FILE "Input file does not exist.\n" #endif diff --git a/src/shell/Readline.cpp b/src/shell/Readline.cpp index 1df62988a..e4f98fc3d 100644 --- a/src/shell/Readline.cpp +++ b/src/shell/Readline.cpp @@ -27,6 +27,8 @@ #define L10N // Localization complete. +#include +#include #include //////////////////////////////////////////////////////////////////////////////// @@ -45,6 +47,12 @@ std::string Readline::gets (const std::string& prompt) return ret; } +//////////////////////////////////////////////////////////////////////////////// +bool Readline::interactive_mode (const std::istream& in) +{ + return (&in == &std::cin && isatty(0) == 1); +} + //////////////////////////////////////////////////////////////////////////////// Wordexp::Wordexp (const std::string &str) { diff --git a/src/shell/Readline.h b/src/shell/Readline.h index 9b5ce8fb6..4a6cab7fa 100644 --- a/src/shell/Readline.h +++ b/src/shell/Readline.h @@ -49,6 +49,7 @@ class Readline { public: static std::string gets (const std::string& prompt); + static bool interactive_mode (const std::istream& in); private: // No construction or destruction. diff --git a/src/shell/main.cpp b/src/shell/main.cpp index 971df7982..912f3d2b5 100644 --- a/src/shell/main.cpp +++ b/src/shell/main.cpp @@ -27,6 +27,7 @@ #define L10N // Localization complete. +#include #include #include #include @@ -63,82 +64,133 @@ int main (int argc, const char** argv) #endif int status = 0; + bool read_from_file = false; - if (argc == 2 && !strcmp (argv[1], "--version")) + if (argc > 2) { - std::cout << VERSION << "\n"; + std::cout << STRING_SHELL_USAGE << "\n"; + return -1; } - else + else if (argc == 2) { - if (argc > 1) + if (!strcmp (argv[1], "--version")) { - std::cerr << STRING_SHELL_UNKOWN_OPTION << argv[1] << "\n"; + std::cout << VERSION << "\n"; + return 0; } - - // Begining initilaization - status = context.initialize (0, NULL); - - // 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" - << STRING_CMD_SHELL_HELP1 << "\n" - << STRING_CMD_SHELL_HELP2 << "\n" - << STRING_CMD_SHELL_HELP3 << "\n\n"; - - // Make a copy because context.clear will delete them. - std::string permanent_overrides; - std::vector ::iterator i; - for (i = context.a3.begin (); i != context.a3.end (); ++i) + else if (!strcmp (argv[1], "--help")) { - if (i->_category == Arg::cat_rc || - i->_category == Arg::cat_override) - { - if (i != context.a3.begin ()) - permanent_overrides += " "; - - permanent_overrides += i->_raw; + std::cout << STRING_SHELL_USAGE << "\n"; + return 0; + } + else + { + // The user has give tasksh a task commands file to execute + File input_file = File (argv[1]); + if (!input_file.exists ()) { + std::cout << STRING_SHELL_NO_FILE; + std::cout << STRING_SHELL_USAGE << "\n"; + return -1; } + + read_from_file = true; } + } - std::string input, prompt(context.config.get ("shell.prompt") + " "); + // if a file is given, read from it + std::ifstream fin; + if (read_from_file) { + fin.open(argv[1]); + } - std::vector quit_commands; - quit_commands.push_back ("quit"); - quit_commands.push_back ("exit"); - quit_commands.push_back ("bye"); + // commands may be redirected too + std::istream &in = read_from_file ? fin : std::cin; - // The event loop. - while (1) + // Begining initilaization + status = context.initialize (0, NULL); + + // 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" + << STRING_CMD_SHELL_HELP1 << "\n" + << STRING_CMD_SHELL_HELP2 << "\n" + << STRING_CMD_SHELL_HELP3 << "\n\n"; + + // Make a copy because context.clear will delete them. + std::string permanent_overrides; + std::vector ::iterator i; + for (i = context.a3.begin (); i != context.a3.end (); ++i) + { + if (i->_category == Arg::cat_rc || + i->_category == Arg::cat_override) { - context.clear (); + if (i != context.a3.begin ()) + permanent_overrides += " "; + permanent_overrides += i->_raw; + } + } + + std::string input; + + std::vector quit_commands; + quit_commands.push_back ("quit"); + quit_commands.push_back ("exit"); + quit_commands.push_back ("bye"); + + // The event loop. + while (in) + { + std::string prompt(context.config.get ("shell.prompt") + " "); + context.clear (); + + if (Readline::interactive_mode(in)) + { input = Readline::gets (prompt); - if (std::find (quit_commands.begin (), quit_commands.end (), - lowerCase (input)) != quit_commands.end ()) - break; + } + else + { + std::getline (in, input); - try - { - Wordexp w ("task " + trim (input + permanent_overrides)); - status = context.initialize (w.argc (), (const char**)w.argv ()); - if (status == 0) - status = context.run (); + // if a string has nothing but whitespaces, ignore it + if (input.find_first_not_of (" \t") == std::string::npos) + continue; + + std::cout << prompt << input << "\n"; + } + + try + { + Wordexp w ("task " + trim (input + permanent_overrides)); + + for (int i = 0; i < w.argc (); ++i) { + if (std::find (quit_commands.begin (), quit_commands.end (), + lowerCase (w.argv ()[i])) != quit_commands.end ()) + { + context.clearMessages (); + return status; + } } - catch (const std::string& error) - { - std::cerr << error << "\n"; - status = -1; - break; - } + status = context.initialize (w.argc (), (const char**)w.argv ()); + if (status == 0) + status = context.run (); + } - catch (...) - { - std::cerr << STRING_UNKNOWN_ERROR << "\n"; - status = -2; - break; - } + catch (const std::string& error) + { + std::cerr << error << "\n"; + status = -1; + break; + } + + catch (...) + { + std::cerr << STRING_UNKNOWN_ERROR << "\n"; + status = -2; + break; } } diff --git a/test/shell.t b/test/shell.t index 127c3f27e..0140bb85b 100755 --- a/test/shell.t +++ b/test/shell.t @@ -42,12 +42,12 @@ if (open my $fh, '>', 'shell.rc') } # Test the prompt. -my $output = qx{echo "quit" | ../src/task rc:shell.rc shell 2>&1}; +my $output = qx{printf "rc:test.rc add foo\nquit" | ../src/shell/tasksh 2>&1}; like ($output, qr/testprompt>/, 'custom prompt is being used'); # Test a simple add, then info. -qx{echo "add foo" | ../src/task rc:shell.rc shell 2>&1}; -$output = qx{echo "1 info" | ../src/task rc:shell.rc shell 2>&1}; +qx{echo "rc:shell.rc add foo" | ../src/shell/tasksh 2>&1}; +$output = qx{echo "rc:shell.rc 1 info" | ../src/shell/tasksh 2>&1}; like ($output, qr/Description\s+foo/, 'add/info working'); unlink 'shell.rc';