You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.commandparser.commands.global;
  23. import com.dmdirc.commandparser.BaseCommandInfo;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandType;
  27. import com.dmdirc.commandparser.commands.BaseCommand;
  28. import com.dmdirc.commandparser.commands.Command;
  29. import com.dmdirc.commandparser.commands.IntelligentCommand;
  30. import com.dmdirc.commandparser.commands.context.CommandContext;
  31. import com.dmdirc.interfaces.CommandController;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import com.dmdirc.ui.input.AdditionalTabTargets;
  34. import com.dmdirc.ui.input.TabCompletionType;
  35. import com.dmdirc.ui.messages.IRCControlCodes;
  36. import javax.annotation.Nonnull;
  37. import javax.inject.Inject;
  38. import java.util.ArrayList;
  39. import java.util.Collections;
  40. import java.util.List;
  41. import java.util.Map;
  42. /**
  43. * The help command shows the user a list of available commands, along with their arguments, and a
  44. * description. It is context-aware, so channel commands are only displayed when in a channel
  45. * window, for example.
  46. */
  47. public class Help extends BaseCommand implements IntelligentCommand {
  48. /** A command info object for this command. */
  49. public static final CommandInfo INFO = new BaseCommandInfo("help",
  50. "help [command] - shows client command help",
  51. CommandType.TYPE_GLOBAL);
  52. /**
  53. * Creates a new instance of this command.
  54. *
  55. * @param controller The controller to use for command information.
  56. */
  57. @Inject
  58. public Help(final CommandController controller) {
  59. super(controller);
  60. }
  61. @Override
  62. public void execute(@Nonnull final WindowModel origin,
  63. final CommandArguments args, final CommandContext context) {
  64. if (args.getArguments().length == 0) {
  65. showAllCommands(origin, args.isSilent());
  66. } else {
  67. showCommand(origin, args.isSilent(), args.getArguments()[0]);
  68. }
  69. }
  70. /**
  71. * Shows a list of all commands valid for the current window.
  72. *
  73. * @param origin The window the command was executed in
  74. * @param isSilent Whether this command has been silenced or not
  75. */
  76. private void showAllCommands(final WindowModel origin, final boolean isSilent) {
  77. final List<String> commands = new ArrayList<>(origin.getInputModel().get()
  78. .getCommandParser().getCommands().keySet());
  79. Collections.sort(commands);
  80. showOutput(origin, isSilent, IRCControlCodes.FIXED
  81. + "----------------------- Available commands -------");
  82. final StringBuilder builder = new StringBuilder();
  83. for (String command : commands) {
  84. if (builder.length() + command.length() + 1 > 50) {
  85. showOutput(origin, isSilent, IRCControlCodes.FIXED + builder.toString());
  86. builder.delete(0, builder.length());
  87. } else if (builder.length() > 0) {
  88. builder.append(' ');
  89. }
  90. builder.append(command);
  91. }
  92. if (builder.length() > 0) {
  93. showOutput(origin, isSilent, IRCControlCodes.FIXED + builder.toString());
  94. }
  95. showOutput(origin, isSilent, IRCControlCodes.FIXED
  96. + "--------------------------------------------------");
  97. }
  98. /**
  99. * Shows information about the specified command.
  100. *
  101. * @param origin The window the command was executed in
  102. * @param isSilent Whether this command has been silenced or not
  103. * @param name The name of the command to display info for
  104. */
  105. private void showCommand(final WindowModel origin, final boolean isSilent,
  106. final String name) {
  107. final Map.Entry<CommandInfo, Command> command;
  108. if (!name.isEmpty() && name.charAt(0) == getController().getCommandChar()) {
  109. command = getController().getCommand(name.substring(1));
  110. } else {
  111. command = getController().getCommand(name);
  112. }
  113. if (command == null) {
  114. showError(origin, isSilent, "Command '" + name + "' not found.");
  115. } else {
  116. showOutput(origin, isSilent, IRCControlCodes.FIXED
  117. + "---------------------- Command information -------");
  118. showOutput(origin, isSilent, IRCControlCodes.FIXED
  119. + " Name: " + name);
  120. showOutput(origin, isSilent, IRCControlCodes.FIXED
  121. + " Type: " + command.getKey().getType());
  122. showOutput(origin, isSilent, IRCControlCodes.FIXED
  123. + "Usage: " + command.getKey().getHelp());
  124. showOutput(origin, isSilent, IRCControlCodes.FIXED
  125. + "--------------------------------------------------");
  126. }
  127. }
  128. @Override
  129. public AdditionalTabTargets getSuggestions(final int arg,
  130. final IntelligentCommandContext context) {
  131. final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
  132. if (arg == 0) {
  133. res.include(TabCompletionType.COMMAND);
  134. }
  135. return res;
  136. }
  137. }