From 8b07718cb47dca6240a70e9aea57b88121cc956b Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 23:25:09 +0100 Subject: akonadi2_cli -> akonadish --- akonadish/syntax_modules/core_syntax.cpp | 139 +++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 akonadish/syntax_modules/core_syntax.cpp (limited to 'akonadish/syntax_modules/core_syntax.cpp') diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp new file mode 100644 index 0000000..393a0a5 --- /dev/null +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2014 Aaron Seigo + * + * 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 "core_syntax.h" + +#include +#include // tr() +#include +#include + +namespace CoreSyntax +{ + +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); + + Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + help.completer = &CoreSyntax::showHelpCompleter; + syntax << help; + + Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); + set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + syntax << set; + + Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); + get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + syntax << get; + + return syntax; +} + +bool exit(const QStringList &, State &) +{ + ::exit(0); + return true; +} + +bool showHelp(const QStringList &commands, State &state) +{ + SyntaxTree::Command command = SyntaxTree::self()->match(commands); + if (commands.isEmpty()) { + state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!")); + state.printLine(QObject::tr("Top-level commands:")); + + QSet sorted; + for (auto syntax: SyntaxTree::self()->syntax()) { + sorted.insert(syntax.keyword); + } + + for (auto keyword: sorted) { + state.printLine(keyword, 1); + } + } else if (const Syntax *syntax = command.first) { + //TODO: get parent! + state.print(QObject::tr("Command `%1`").arg(syntax->keyword)); + + if (!syntax->help.isEmpty()) { + state.print(": " + syntax->help); + } + state.printLine(); + + if (!syntax->children.isEmpty()) { + state.printLine("Sub-commands:", 1); + QSet sorted; + for (auto childSyntax: syntax->children) { + sorted.insert(childSyntax.keyword); + } + + for (auto keyword: sorted) { + state.printLine(keyword, 1); + } + } + } else { + state.printError("Unknown command: " + commands.join(" ")); + } + + return true; +} + +QStringList showHelpCompleter(const QStringList &commands, const QString &fragment) +{ + QStringList items; + + for (auto syntax: SyntaxTree::self()->syntax()) { + if (syntax.keyword != QObject::tr("help") && + (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) { + items << syntax.keyword; + } + } + + qSort(items); + return items; +} + +bool setDebugLevel(const QStringList &commands, State &state) +{ + if (commands.count() != 1) { + state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count())); + return false; + } + + bool ok = false; + int level = commands[0].toUInt(&ok); + + if (!ok) { + state.printError(QObject::tr("Expected a number between 0 and 6, got %1").arg(commands[0])); + return false; + } + + state.setDebugLevel(level); + return true; +} + +bool printDebugLevel(const QStringList &commands, State &state) +{ + state.printLine(QString::number(state.debugLevel())); + return true; +} + +} // namespace CoreSyntax + -- cgit v1.2.3 From 35f0ddf67c629ce9efaa1ba893afcb2921a251a2 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Thu, 24 Dec 2015 10:22:07 +0100 Subject: REGISTER_SYNTAX for automagic adding of syntax a fun abuse of static initialization and std::function --- akonadish/syntax_modules/core_syntax.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'akonadish/syntax_modules/core_syntax.cpp') diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index 393a0a5..8fb1448 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -27,6 +27,8 @@ namespace CoreSyntax { +REGISTER_SYNTAX(CoreSyntax) + Syntax::List syntax() { Syntax::List syntax; -- cgit v1.2.3 From 8b397cec29460e2cb53b9be02b4e53da9cce60c9 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Thu, 24 Dec 2015 16:23:46 +0100 Subject: bye-bye header files the function-centric approach simply does not require them! --- akonadish/syntax_modules/core_syntax.cpp | 49 ++++++++++++++++---------------- 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'akonadish/syntax_modules/core_syntax.cpp') diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index 8fb1448..a0cd4c6 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -17,37 +17,16 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "core_syntax.h" - #include #include // tr() #include #include -namespace CoreSyntax -{ - -REGISTER_SYNTAX(CoreSyntax) +#include "state.h" +#include "syntaxtree.h" -Syntax::List syntax() +namespace CoreSyntax { - Syntax::List syntax; - syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); - - Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); - help.completer = &CoreSyntax::showHelpCompleter; - syntax << help; - - Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); - set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); - syntax << set; - - Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); - get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); - syntax << get; - - return syntax; -} bool exit(const QStringList &, State &) { @@ -137,5 +116,27 @@ bool printDebugLevel(const QStringList &commands, State &state) return true; } +Syntax::List syntax() +{ + Syntax::List syntax; + syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); + + Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + help.completer = &CoreSyntax::showHelpCompleter; + syntax << help; + + Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); + set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + syntax << set; + + Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); + get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + syntax << get; + + return syntax; +} + +REGISTER_SYNTAX(CoreSyntax) + } // namespace CoreSyntax -- cgit v1.2.3 From 908dc9b172a08bfa219a834e15916e9fcfc3c4ec Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 10:31:30 +0100 Subject: syntax to turn timing on/off loving the lambdas :) --- akonadish/syntax_modules/core_syntax.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'akonadish/syntax_modules/core_syntax.cpp') diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index a0cd4c6..9cd6a6a 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -110,12 +110,18 @@ bool setDebugLevel(const QStringList &commands, State &state) return true; } -bool printDebugLevel(const QStringList &commands, State &state) +bool printDebugLevel(const QStringList &, State &state) { state.printLine(QString::number(state.debugLevel())); return true; } +bool printCommandTiming(const QStringList &, State &state) +{ + state.printLine(state.commandTiming() ? QObject::tr("on") : QObject::tr("off")); + return true; +} + Syntax::List syntax() { Syntax::List syntax; @@ -127,10 +133,15 @@ Syntax::List syntax() Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + Syntax setTiming = Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete")); + setTiming.children << Syntax(QObject::tr("on"), QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); + setTiming.children << Syntax(QObject::tr("off"), QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); + set.children << setTiming; syntax << set; Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); - get.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + get.children << Syntax(QObject::tr("debug"), QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + get.children << Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); syntax << get; return syntax; -- cgit v1.2.3 From 1231996ec2668e330ead79a16edf85ded1e07e48 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 10:47:02 +0100 Subject: print the syntax tree --- akonadish/syntax_modules/core_syntax.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'akonadish/syntax_modules/core_syntax.cpp') diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index 9cd6a6a..8f3219f 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -122,6 +122,29 @@ bool printCommandTiming(const QStringList &, State &state) return true; } +void printSyntaxBranch(State &state, const Syntax::List &list, int depth) +{ + if (list.isEmpty()) { + return; + } + + if (depth > 0) { + state.printLine("\\", depth); + } + + for (auto syntax: list) { + state.print("|-", depth); + state.printLine(syntax.keyword); + printSyntaxBranch(state, syntax.children, depth + 1); + } +} + +bool printSyntaxTree(const QStringList &, State &state) +{ + printSyntaxBranch(state, SyntaxTree::self()->syntax(), 0); + return true; +} + Syntax::List syntax() { Syntax::List syntax; @@ -131,6 +154,8 @@ Syntax::List syntax() help.completer = &CoreSyntax::showHelpCompleter; syntax << help; + syntax << Syntax("syntaxtree", QString(), &printSyntaxTree); + Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); Syntax setTiming = Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete")); -- cgit v1.2.3 From 540864148b9bccc4b00bb3ba504190d608489413 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Fri, 25 Dec 2015 11:01:01 +0100 Subject: don't translate the commands; let them be scriptable universally --- akonadish/syntax_modules/core_syntax.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'akonadish/syntax_modules/core_syntax.cpp') diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp index 8f3219f..31b824a 100644 --- a/akonadish/syntax_modules/core_syntax.cpp +++ b/akonadish/syntax_modules/core_syntax.cpp @@ -150,23 +150,23 @@ Syntax::List syntax() Syntax::List syntax; syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); - Syntax help(QObject::tr("help"), QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); + Syntax help("help", QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); help.completer = &CoreSyntax::showHelpCompleter; syntax << help; syntax << Syntax("syntaxtree", QString(), &printSyntaxTree); - Syntax set(QObject::tr("set"), QObject::tr("Sets settings for the session")); - set.children << Syntax(QObject::tr("debug"), QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); - Syntax setTiming = Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete")); - setTiming.children << Syntax(QObject::tr("on"), QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); - setTiming.children << Syntax(QObject::tr("off"), QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); + Syntax set("set", QObject::tr("Sets settings for the session")); + set.children << Syntax("debug", QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); + Syntax setTiming = Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete")); + setTiming.children << Syntax("on", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); + setTiming.children << Syntax("off", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); set.children << setTiming; syntax << set; - Syntax get(QObject::tr("get"), QObject::tr("Gets settings for the session")); - get.children << Syntax(QObject::tr("debug"), QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); - get.children << Syntax(QObject::tr("timing"), QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); + Syntax get("get", QObject::tr("Gets settings for the session")); + get.children << Syntax("debug", QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); + get.children << Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); syntax << get; return syntax; -- cgit v1.2.3