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.1KB

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