123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- /*
- * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.commandparser.commands.global;
-
- import com.dmdirc.FrameContainer;
- import com.dmdirc.WritableFrameContainer;
- import com.dmdirc.commandparser.CommandArguments;
- import com.dmdirc.commandparser.CommandInfo;
- import com.dmdirc.commandparser.CommandManager;
- import com.dmdirc.commandparser.CommandType;
- import com.dmdirc.commandparser.commands.Command;
- import com.dmdirc.commandparser.commands.IntelligentCommand;
- import com.dmdirc.commandparser.commands.context.CommandContext;
- import com.dmdirc.ui.input.AdditionalTabTargets;
- import com.dmdirc.ui.input.TabCompletionType;
- import com.dmdirc.ui.messages.Styliser;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Map;
-
- /**
- * The help command shows the user a list of available commands, along with
- * their arguments, and a description. It is context-aware, so channel commands
- * are only displayed when in a channel window, for example.
- * @author chris
- */
- public final class Help extends Command implements IntelligentCommand, CommandInfo {
-
- /**
- * Creates a new instance of Help.
- */
- public Help() {
- super();
- }
-
- /** {@inheritDoc} */
- @Override
- public void execute(final FrameContainer origin,
- final CommandArguments args, final CommandContext context) {
- if (args.getArguments().length == 0) {
- showAllCommands(origin, args.isSilent());
- } else {
- showCommand(origin, args.isSilent(), args.getArguments()[0]);
- }
- }
-
- /**
- * Shows a list of all commands valid for the current window.
- *
- * @param origin The window the command was executed in
- * @param isSilent Whether this command has been silenced or not
- */
- private void showAllCommands(final FrameContainer origin, final boolean isSilent) {
- final List<String> commands = new ArrayList<String>(((WritableFrameContainer) origin)
- .getCommandParser().getCommands().keySet());
-
- Collections.sort(commands);
-
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
- + "----------------------- Available commands -------");
-
- final StringBuilder builder = new StringBuilder();
-
- for (String command : commands) {
- if (builder.length() + command.length() + 1 > 50) {
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
- builder.delete(0, builder.length());
- } else if (builder.length() > 0) {
- builder.append(' ');
- }
-
- builder.append(command);
- }
-
- if (builder.length() > 0) {
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
- }
-
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
- + "--------------------------------------------------");
- }
-
- /**
- * Shows information about the specified command.
- *
- * @param origin The window the command was executed in
- * @param isSilent Whether this command has been silenced or not
- * @param name The name of the command to display info for
- */
- private void showCommand(final FrameContainer origin, final boolean isSilent,
- final String name) {
- Map.Entry<CommandInfo, Command> command = null;
-
- if (name.length() > 0 && name.charAt(0) == CommandManager.getCommandChar()) {
- command = CommandManager.getCommand(name.substring(1));
- } else {
- command = CommandManager.getCommand(name);
- }
-
- if (command == null) {
- sendLine(origin, isSilent, FORMAT_ERROR, "Command '" + name + "' not found.");
- } else {
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
- + "---------------------- Command information -------");
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
- + " Name: " + name);
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
- + " Type: " + command.getKey().getType());
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
- + "Usage: " + command.getKey().getHelp());
- sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
- + "--------------------------------------------------");
- }
- }
-
- /** {@inheritDoc}. */
- @Override
- public String getName() {
- return "help";
- }
-
- /** {@inheritDoc}. */
- @Override
- public boolean showInHelp() {
- return true;
- }
-
- /** {@inheritDoc} */
- @Override
- public CommandType getType() {
- return CommandType.TYPE_GLOBAL;
- }
-
- /** {@inheritDoc}. */
- @Override
- public String getHelp() {
- return "help [command] - shows client command help";
- }
-
- /** {@inheritDoc} */
- @Override
- public AdditionalTabTargets getSuggestions(final int arg,
- final IntelligentCommandContext context) {
- final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
-
- if (arg == 0) {
- res.include(TabCompletionType.COMMAND);
- }
-
- return res;
- }
-
- }
|