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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (c) 2006-2012 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.FrameContainer;
  24. import com.dmdirc.WritableFrameContainer;
  25. import com.dmdirc.commandparser.BaseCommandInfo;
  26. import com.dmdirc.commandparser.CommandArguments;
  27. import com.dmdirc.commandparser.CommandInfo;
  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. */
  44. public class Help extends Command implements IntelligentCommand {
  45. /** A command info object for this command. */
  46. public static final CommandInfo INFO = new BaseCommandInfo("help",
  47. "help [command] - shows client command help",
  48. CommandType.TYPE_GLOBAL);
  49. /** {@inheritDoc} */
  50. @Override
  51. public void execute(final FrameContainer origin,
  52. final CommandArguments args, final CommandContext context) {
  53. if (args.getArguments().length == 0) {
  54. showAllCommands(origin, args.isSilent());
  55. } else {
  56. showCommand(origin, args.isSilent(), args.getArguments()[0]);
  57. }
  58. }
  59. /**
  60. * Shows a list of all commands valid for the current window.
  61. *
  62. * @param origin The window the command was executed in
  63. * @param isSilent Whether this command has been silenced or not
  64. */
  65. private void showAllCommands(final FrameContainer origin, final boolean isSilent) {
  66. final List<String> commands = new ArrayList<String>(((WritableFrameContainer) origin)
  67. .getCommandParser().getCommands().keySet());
  68. Collections.sort(commands);
  69. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  70. + "----------------------- Available commands -------");
  71. final StringBuilder builder = new StringBuilder();
  72. for (String command : commands) {
  73. if (builder.length() + command.length() + 1 > 50) {
  74. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
  75. builder.delete(0, builder.length());
  76. } else if (builder.length() > 0) {
  77. builder.append(' ');
  78. }
  79. builder.append(command);
  80. }
  81. if (builder.length() > 0) {
  82. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED + builder.toString());
  83. }
  84. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  85. + "--------------------------------------------------");
  86. }
  87. /**
  88. * Shows information about the specified command.
  89. *
  90. * @param origin The window the command was executed in
  91. * @param isSilent Whether this command has been silenced or not
  92. * @param name The name of the command to display info for
  93. */
  94. private void showCommand(final FrameContainer origin, final boolean isSilent,
  95. final String name) {
  96. Map.Entry<CommandInfo, Command> command = null;
  97. if (name.length() > 0 && name.charAt(0) == getController().getCommandChar()) {
  98. command = getController().getCommand(name.substring(1));
  99. } else {
  100. command = getController().getCommand(name);
  101. }
  102. if (command == null) {
  103. sendLine(origin, isSilent, FORMAT_ERROR, "Command '" + name + "' not found.");
  104. } else {
  105. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  106. + "---------------------- Command information -------");
  107. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  108. + " Name: " + name);
  109. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  110. + " Type: " + command.getKey().getType());
  111. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  112. + "Usage: " + command.getKey().getHelp());
  113. sendLine(origin, isSilent, FORMAT_OUTPUT, Styliser.CODE_FIXED
  114. + "--------------------------------------------------");
  115. }
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public AdditionalTabTargets getSuggestions(final int arg,
  120. final IntelligentCommandContext context) {
  121. final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
  122. if (arg == 0) {
  123. res.include(TabCompletionType.COMMAND);
  124. }
  125. return res;
  126. }
  127. }