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.

Help.java 6.0KB

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.Command;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.commandparser.commands.context.CommandContext;
  30. import com.dmdirc.interfaces.CommandController;
  31. import com.dmdirc.interfaces.WindowModel;
  32. import com.dmdirc.ui.input.AdditionalTabTargets;
  33. import com.dmdirc.ui.input.TabCompletionType;
  34. import com.dmdirc.ui.messages.IRCControlCodes;
  35. import java.util.ArrayList;
  36. import java.util.Collections;
  37. import java.util.List;
  38. import java.util.Map;
  39. import javax.annotation.Nonnull;
  40. import javax.inject.Inject;
  41. /**
  42. * The help command shows the user a list of available commands, along with their arguments, and a
  43. * description. It is context-aware, so channel commands are only displayed when in a channel
  44. * window, for example.
  45. */
  46. public class Help extends Command implements IntelligentCommand {
  47. /** A command info object for this command. */
  48. public static final CommandInfo INFO = new BaseCommandInfo("help",
  49. "help [command] - shows client command help",
  50. CommandType.TYPE_GLOBAL);
  51. /**
  52. * Creates a new instance of this command.
  53. *
  54. * @param controller The controller to use for command information.
  55. */
  56. @Inject
  57. public Help(final CommandController controller) {
  58. super(controller);
  59. }
  60. @Override
  61. public void execute(@Nonnull final WindowModel origin,
  62. final CommandArguments args, final CommandContext context) {
  63. if (args.getArguments().length == 0) {
  64. showAllCommands(origin, args.isSilent());
  65. } else {
  66. showCommand(origin, args.isSilent(), args.getArguments()[0]);
  67. }
  68. }
  69. /**
  70. * Shows a list of all commands valid for the current window.
  71. *
  72. * @param origin The window the command was executed in
  73. * @param isSilent Whether this command has been silenced or not
  74. */
  75. private void showAllCommands(final WindowModel origin, final boolean isSilent) {
  76. final List<String> commands = new ArrayList<>(origin.getInputModel().get()
  77. .getCommandParser().getCommands().keySet());
  78. Collections.sort(commands);
  79. showOutput(origin, isSilent, IRCControlCodes.FIXED
  80. + "----------------------- Available commands -------");
  81. final StringBuilder builder = new StringBuilder();
  82. for (String command : commands) {
  83. if (builder.length() + command.length() + 1 > 50) {
  84. showOutput(origin, isSilent, IRCControlCodes.FIXED + builder.toString());
  85. builder.delete(0, builder.length());
  86. } else if (builder.length() > 0) {
  87. builder.append(' ');
  88. }
  89. builder.append(command);
  90. }
  91. if (builder.length() > 0) {
  92. showOutput(origin, isSilent, IRCControlCodes.FIXED + builder.toString());
  93. }
  94. showOutput(origin, isSilent, IRCControlCodes.FIXED
  95. + "--------------------------------------------------");
  96. }
  97. /**
  98. * Shows information about the specified command.
  99. *
  100. * @param origin The window the command was executed in
  101. * @param isSilent Whether this command has been silenced or not
  102. * @param name The name of the command to display info for
  103. */
  104. private void showCommand(final WindowModel origin, final boolean isSilent,
  105. final String name) {
  106. final Map.Entry<CommandInfo, Command> command;
  107. if (!name.isEmpty() && name.charAt(0) == getController().getCommandChar()) {
  108. command = getController().getCommand(name.substring(1));
  109. } else {
  110. command = getController().getCommand(name);
  111. }
  112. if (command == null) {
  113. showError(origin, isSilent, "Command '" + name + "' not found.");
  114. } else {
  115. showOutput(origin, isSilent, IRCControlCodes.FIXED
  116. + "---------------------- Command information -------");
  117. showOutput(origin, isSilent, IRCControlCodes.FIXED
  118. + " Name: " + name);
  119. showOutput(origin, isSilent, IRCControlCodes.FIXED
  120. + " Type: " + command.getKey().getType());
  121. showOutput(origin, isSilent, IRCControlCodes.FIXED
  122. + "Usage: " + command.getKey().getHelp());
  123. showOutput(origin, isSilent, IRCControlCodes.FIXED
  124. + "--------------------------------------------------");
  125. }
  126. }
  127. @Override
  128. public AdditionalTabTargets getSuggestions(final int arg,
  129. final IntelligentCommandContext context) {
  130. final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
  131. if (arg == 0) {
  132. res.include(TabCompletionType.COMMAND);
  133. }
  134. return res;
  135. }
  136. }