Search in sources :

Example 1 with Command

use of henplus.Command in project henplus by neurolabs.

the class HelpCommand method complete.

/**
     * Returns a list of strings that are possible at this stage.
     */
@Override
public Iterator<String> complete(final CommandDispatcher disp, final String partialCommand, final String lastWord) {
    // if we already have one arguemnt and try to expand the next: no.
    final int argc = argumentCount(partialCommand);
    if (argc > 2 || argc == 2 && lastWord.length() == 0) {
        return null;
    }
    final Iterator<String> it = disp.getRegisteredCommandNames(lastWord);
    return new SortedMatchIterator(lastWord, it) {

        @Override
        protected boolean exclude(final String cmdName) {
            final Command cmd = disp.getCommandFrom(cmdName);
            return cmd.getLongDescription(cmdName) == null;
        }
    };
}
Also used : SortedMatchIterator(henplus.view.util.SortedMatchIterator) AbstractCommand(henplus.AbstractCommand) Command(henplus.Command)

Example 2 with Command

use of henplus.Command in project henplus by neurolabs.

the class PluginCommand method execute.

/**
     * execute the command given.
     */
@Override
public int execute(final SQLSession currentSession, final String cmd, final String param) {
    final StringTokenizer st = new StringTokenizer(param);
    final int argc = st.countTokens();
    if ("list-plugins".equals(cmd)) {
        if (argc != 0) {
            return SYNTAX_ERROR;
        }
        HenPlus.msg().println("loaded plugins are marked with '*'");
        DRV_META[0].resetWidth();
        DRV_META[1].resetWidth();
        final TableRenderer table = new TableRenderer(DRV_META, HenPlus.out());
        for (Entry<String, Command> entry : _plugins.entrySet()) {
            final Column[] row = new Column[2];
            final Command c = entry.getValue();
            final String clsName = entry.getKey();
            row[0] = new Column((c != null ? "* " : "  ") + clsName);
            if (c != null) {
                final StringBuilder cmds = new StringBuilder();
                final String[] cmdList = c.getCommandList();
                for (int i = 0; i < cmdList.length; ++i) {
                    cmds.append(cmdList[i]).append("\n");
                }
                row[1] = new Column(cmds.toString().trim());
            } else {
                row[1] = new Column(null);
            }
            table.addRow(row);
        }
        table.closeTable();
        return SUCCESS;
    } else if ("plug-in".equals(cmd)) {
        if (argc != 1) {
            return SYNTAX_ERROR;
        }
        final String pluginClass = (String) st.nextElement();
        if (_plugins.containsKey(pluginClass)) {
            HenPlus.msg().println("plugin '" + pluginClass + "' already loaded");
            return EXEC_FAILED;
        }
        Command plugin = null;
        try {
            plugin = loadPlugin(pluginClass);
        } catch (final Exception e) {
            HenPlus.msg().println("couldn't load plugin: " + e.getMessage());
            return EXEC_FAILED;
        }
        if (plugin != null) {
            _plugins.put(pluginClass, plugin);
            final String[] cmds = plugin.getCommandList();
            HenPlus.out().print("adding commands: ");
            for (int i = 0; i < cmds.length; ++i) {
                if (i != 0) {
                    HenPlus.out().print(", ");
                }
                HenPlus.out().print(cmds[i]);
            }
            HenPlus.out().println();
        }
    } else if ("plug-out".equals(cmd)) {
        if (argc != 1) {
            return SYNTAX_ERROR;
        }
        final String pluginClass = (String) st.nextElement();
        if (!_plugins.containsKey(pluginClass)) {
            HenPlus.msg().println("unknown plugin '" + pluginClass + "'");
            return EXEC_FAILED;
        } else {
            final Command c = _plugins.remove(pluginClass);
            _henplus.getDispatcher().unregister(c);
        }
    }
    return SUCCESS;
}
Also used : TableRenderer(henplus.view.TableRenderer) StringTokenizer(java.util.StringTokenizer) AbstractCommand(henplus.AbstractCommand) Command(henplus.Command) Column(henplus.view.Column)

Example 3 with Command

use of henplus.Command in project henplus by neurolabs.

the class HelpCommand method execute.

/**
     * execute the command given.
     */
@Override
public int execute(final SQLSession session, final String cmdstr, String param) {
    final StringTokenizer st = new StringTokenizer(param);
    if (st.countTokens() > 1) {
        return SYNTAX_ERROR;
    }
    param = st.hasMoreElements() ? (String) st.nextElement() : null;
    /*
         * nothing given: provide generic help.
         */
    if (param == null) {
        final Iterator<Command> it = HenPlus.getInstance().getDispatcher().getRegisteredCommands();
        while (it.hasNext()) {
            final Command cmd = it.next();
            final String description = cmd.getShortDescription();
            if (description == null) {
                continue;
            }
            final StringBuilder cmdPrint = new StringBuilder(" ");
            final String[] cmds = cmd.getCommandList();
            final String firstSynopsis = cmd.getSynopsis(cmds[0]);
            /*
                 * either print a list of known commands or the complete
                 * synopsis, if there is only one command.
                 */
            if (cmds.length > 1 || firstSynopsis == null) {
                for (int i = 0; i < cmds.length; ++i) {
                    if (i != 0) {
                        cmdPrint.append(" | ");
                    }
                    cmdPrint.append(cmds[i]);
                }
            } else {
                cmdPrint.append(firstSynopsis.length() < INDENT ? firstSynopsis : cmds[0]);
            }
            HenPlus.msg().print(cmdPrint.toString());
            for (int i = cmdPrint.length(); i < INDENT; ++i) {
                HenPlus.msg().print(" ");
            }
            HenPlus.msg().print(": ");
            HenPlus.msg().println(description);
        }
        HenPlus.msg().println("Full documentation at http://henplus.sf.net/");
        HenPlus.msg().println("config read from [" + HenPlus.getInstance().getConfigurationDirectoryInfo() + "]");
    } else {
        final CommandDispatcher disp = HenPlus.getInstance().getDispatcher();
        final String cmdString = disp.getCommandNameFrom(param);
        final Command c = disp.getCommandFrom(param);
        if (c == null) {
            HenPlus.msg().println("Help: unknown command '" + param + "'");
            return EXEC_FAILED;
        }
        printDescription(cmdString, c);
    }
    return SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) AbstractCommand(henplus.AbstractCommand) Command(henplus.Command) CommandDispatcher(henplus.CommandDispatcher)

Example 4 with Command

use of henplus.Command in project henplus by neurolabs.

the class AliasCommand method getLongDescription.

@Override
public String getLongDescription(final String cmd) {
    String dsc = null;
    if ("list-aliases".equals(cmd)) {
        dsc = "\tList all aliases, that have been stored with the\n" + "\t'alias' command";
    } else if ("alias".equals(cmd)) {
        dsc = "\tAdd an alias for a command. This means, that you can\n" + "\tgive a short name for a command you often use.  This\n" + "\tmight be as simple as\n" + "\t   alias ls tables\n" + "\tto execute the tables command with a short 'ls'.\n" + "\n\tFor longer commands it is even more helpful:\n" + "\t   alias size select count(*) from\n" + "\tThis command  needs a table  name as a  parameter to\n" + "\texpand  to  a  complete command.  So 'size students'\n" + "\texpands to 'select count(*) from students' and yields\n" + "\tthe expected result.\n" + "\n\tTo make life easier, HenPlus tries to determine the\n" + "\tcommand  to be executed so that the  tab-completion\n" + "\tworks even here; in this latter case it  would help\n" + "\tcomplete table names.";
    } else if ("unalias".equals(cmd)) {
        dsc = "\tremove an alias name";
    } else {
        // not session-proof:
        if (_currentExecutedAliases.contains(cmd)) {
            dsc = "\t[ this command cyclicly references itself ]";
        } else {
            _currentExecutedAliases.add(cmd);
            dsc = "\tThis is an alias for the command\n" + "\t   " + _aliases.get(cmd);
            String actualCmdStr = _aliases.get(cmd);
            if (actualCmdStr != null) {
                final StringTokenizer st = new StringTokenizer(actualCmdStr);
                actualCmdStr = st.nextToken();
                final Command c = _dispatcher.getCommandFrom(actualCmdStr);
                String longDesc = null;
                if (c != null && (longDesc = c.getLongDescription(actualCmdStr)) != null) {
                    dsc += "\n\n\t..the following description could be determined for this";
                    dsc += "\n\t------- [" + actualCmdStr + "] ---\n";
                    dsc += longDesc;
                }
                _currentExecutedAliases.clear();
            }
        }
    }
    return dsc;
}
Also used : StringTokenizer(java.util.StringTokenizer) AbstractCommand(henplus.AbstractCommand) Command(henplus.Command)

Example 5 with Command

use of henplus.Command in project henplus by neurolabs.

the class AliasCommand method complete.

@Override
public Iterator<String> complete(final CommandDispatcher disp, final String partialCommand, final String lastWord) {
    final StringTokenizer st = new StringTokenizer(partialCommand);
    final String cmd = (String) st.nextElement();
    final int argc = st.countTokens();
    // list-aliases gets no names.
    if ("list-aliases".equals(cmd)) {
        return null;
    }
    /*
         * some completion within the alias/unalias commands.
         */
    if ("alias".equals(cmd) || "unalias".equals(cmd)) {
        final HashSet<String> alreadyGiven = new HashSet<String>();
        if ("alias".equals(cmd)) {
            // do not complete beyond first word.
            if (argc > ("".equals(lastWord) ? 0 : 1)) {
                return null;
            }
        } else {
            /*
                 * remember all aliases, that have already been given on the
                 * commandline and exclude from completion.. cool, isn't it ?
                 */
            while (st.hasMoreElements()) {
                alreadyGiven.add(st.nextToken());
            }
        }
        // ok, now return the list.
        return new SortedMatchIterator(lastWord, _aliases) {

            @Override
            protected boolean exclude(final String current) {
                return alreadyGiven.contains(current);
            }
        };
    }
    /*
         * ok, someone tries to complete something that is a command. try to
         * find the actual command and ask that command to do the completion.
         */
    final String toExecute = _aliases.get(cmd);
    if (toExecute != null) {
        final Command c = disp.getCommandFrom(toExecute);
        if (c != null) {
            int i = 0;
            final String param = partialCommand;
            while (param.length() < i && Character.isWhitespace(param.charAt(i))) {
                ++i;
            }
            while (param.length() < i && !Character.isWhitespace(param.charAt(i))) {
                ++i;
            }
            return c.complete(disp, toExecute + param.substring(i), lastWord);
        }
    }
    return null;
}
Also used : StringTokenizer(java.util.StringTokenizer) SortedMatchIterator(henplus.view.util.SortedMatchIterator) AbstractCommand(henplus.AbstractCommand) Command(henplus.Command) HashSet(java.util.HashSet)

Aggregations

AbstractCommand (henplus.AbstractCommand)6 Command (henplus.Command)6 StringTokenizer (java.util.StringTokenizer)4 SortedMatchIterator (henplus.view.util.SortedMatchIterator)2 CommandDispatcher (henplus.CommandDispatcher)1 Column (henplus.view.Column)1 TableRenderer (henplus.view.TableRenderer)1 HashSet (java.util.HashSet)1