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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.CommandArguments;
  24. import com.dmdirc.commandparser.CommandInfo;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.commandparser.commands.Command;
  27. import com.dmdirc.commandparser.commands.GlobalCommand;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.ui.input.AdditionalTabTargets;
  30. import com.dmdirc.ui.input.TabCompletionType;
  31. import com.dmdirc.ui.interfaces.InputWindow;
  32. import com.dmdirc.ui.messages.Styliser;
  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
  39. * their arguments, and a description. It is context-aware, so channel commands
  40. * are only displayed when in a channel window, for example.
  41. * @author chris
  42. */
  43. public final class Help extends GlobalCommand implements IntelligentCommand {
  44. /**
  45. * Creates a new instance of Help.
  46. */
  47. public Help() {
  48. super();
  49. CommandManager.registerCommand(this);
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public void execute(final InputWindow origin, final boolean isSilent,
  54. final CommandArguments args) {
  55. if (args.getArguments().length == 0) {
  56. showAllCommands(origin, isSilent);
  57. } else {
  58. showCommand(origin, isSilent, args.getArguments()[0]);
  59. }
  60. }
  61. /**
  62. * Shows a list of all commands valid for the current window.
  63. *
  64. * @param origin The window the command was executed in
  65. * @param isSilent Whether this command has been silenced or not
  66. */
  67. private void showAllCommands(final InputWindow origin, final boolean isSilent) {
  68. final List<String> commands = new ArrayList<String>(origin.getCommandParser()
  69. .getCommands().keySet());
  70. Collections.sort(commands);
  71. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  72. + "----------------------- Available commands -------");
  73. final StringBuilder builder = new StringBuilder();
  74. for (String command : commands) {
  75. if (builder.length() + command.length() + 1 > 50) {
  76. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
  77. builder.delete(0, builder.length());
  78. } else if (builder.length() > 0) {
  79. builder.append(' ');
  80. }
  81. builder.append(command);
  82. }
  83. if (builder.length() > 0) {
  84. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
  85. }
  86. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  87. + "--------------------------------------------------");
  88. }
  89. /**
  90. * Shows information about the specified command.
  91. *
  92. * @param origin The window the command was executed in
  93. * @param isSilent Whether this command has been silenced or not
  94. * @param name The name of the command to display info for
  95. */
  96. private void showCommand(final InputWindow origin, final boolean isSilent,
  97. final String name) {
  98. Map.Entry<CommandInfo, Command> command = null;
  99. if (name.length() > 0 && name.charAt(0) == CommandManager.getCommandChar()) {
  100. command = CommandManager.getCommand(name.substring(1));
  101. } else {
  102. command = CommandManager.getCommand(name);
  103. }
  104. if (command == null) {
  105. sendLine(origin, isSilent, FORMAT_ERROR, "Command '" + name + "' not found.");
  106. } else {
  107. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  108. + "---------------------- Command information -------");
  109. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  110. + " Name: " + name);
  111. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  112. + " Type: " + command.getKey().getType());
  113. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  114. + "Usage: " + command.getKey().getHelp());
  115. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  116. + "--------------------------------------------------");
  117. }
  118. }
  119. /** {@inheritDoc}. */
  120. @Override
  121. public String getName() {
  122. return "help";
  123. }
  124. /** {@inheritDoc}. */
  125. @Override
  126. public boolean showInHelp() {
  127. return true;
  128. }
  129. /** {@inheritDoc}. */
  130. @Override
  131. public String getHelp() {
  132. return "help [command] - shows client command help";
  133. }
  134. /** {@inheritDoc} */
  135. @Override
  136. public AdditionalTabTargets getSuggestions(final int arg,
  137. final IntelligentCommandContext context) {
  138. final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
  139. if (arg == 0) {
  140. res.include(TabCompletionType.COMMAND);
  141. }
  142. return res;
  143. }
  144. }