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

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