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.

Debug.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.addons.debug;
  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 java.util.ArrayList;
  34. import java.util.Arrays;
  35. import java.util.Collection;
  36. import java.util.HashMap;
  37. import java.util.Map;
  38. import java.util.Set;
  39. import java.util.stream.Collectors;
  40. import javax.annotation.Nonnull;
  41. import javax.inject.Inject;
  42. import javax.inject.Singleton;
  43. /**
  44. * Provides various handy ways to test or debug the client.
  45. */
  46. @Singleton
  47. public class Debug extends Command implements IntelligentCommand {
  48. /** A command info object for this command. */
  49. public static final CommandInfo INFO = new BaseCommandInfo("debug",
  50. "debug <command> [args] - facilitates debugging of DMDirc",
  51. CommandType.TYPE_GLOBAL);
  52. /** The command controller to use to lookup command information. */
  53. private final CommandController controller;
  54. /** List of registered debug commands. */
  55. private final Map<String, DebugCommand> commands;
  56. /**
  57. * Creates a new debug command.
  58. *
  59. * @param controller The command controller to use to lookup command information.
  60. * @param subcommands The subcommands to be loaded.
  61. */
  62. @Inject
  63. public Debug(
  64. final CommandController controller,
  65. final Set<DebugCommand> subcommands) {
  66. super(controller);
  67. this.controller = controller;
  68. this.commands = new HashMap<>(subcommands.size());
  69. for (DebugCommand command : subcommands) {
  70. commands.put(command.getName(), command);
  71. }
  72. }
  73. @Override
  74. public void execute(@Nonnull final WindowModel origin,
  75. final CommandArguments args, final CommandContext context) {
  76. if (args.getArguments().length == 0) {
  77. showUsage(origin, args.isSilent(), "debug",
  78. "<debug command> [options]");
  79. } else {
  80. final DebugCommand command = commands.get(args.getArguments()[0]);
  81. if (command == null) {
  82. showError(origin, args.isSilent(), "Unknown debug action.");
  83. } else {
  84. final CommandArguments newArgs = new CommandArguments(
  85. controller,
  86. Arrays.asList((controller.getCommandChar()
  87. + command.getName() + ' '
  88. + args.getArgumentsAsString(1)).split(" ")));
  89. command.execute(origin, newArgs, context);
  90. }
  91. }
  92. }
  93. /**
  94. * Sends a line, if appropriate, to the specified target.
  95. *
  96. * @param target The command window to send the line to
  97. * @param isSilent Whether this command is being silenced or not
  98. * @param type The type of message to send
  99. * @param args The arguments of the message
  100. */
  101. @Deprecated
  102. public void proxySendLine(final WindowModel target,
  103. final boolean isSilent, final String type, final Object... args) {
  104. sendLine(target, isSilent, type, args);
  105. }
  106. /**
  107. * Sends a usage line, if appropriate, to the specified target.
  108. *
  109. * @param target The command window to send the line to
  110. * @param isSilent Whether this command is being silenced or not
  111. * @param name The name of the command that's raising the error
  112. * @param args The arguments that the command accepts or expects
  113. */
  114. public void proxyShowUsage(final WindowModel target,
  115. final boolean isSilent, final String name, final String args) {
  116. showUsage(target, isSilent, INFO.getName(), name + ' ' + args);
  117. }
  118. /**
  119. * Formats the specified data into a table suitable for output in the textpane. It is expected
  120. * that each String[] in data has the same number of elements as the headers array.
  121. *
  122. * @param headers The headers of the table.
  123. * @param data The contents of the table.
  124. *
  125. * @return A string containing an ASCII table
  126. */
  127. public String proxyDoTable(final String[] headers, final String[][] data) {
  128. return doTable(headers, data);
  129. }
  130. /**
  131. * Returns a list of command names.
  132. *
  133. * @return List of command names
  134. */
  135. public Collection<String> getCommandNames() {
  136. final Collection<String> names = new ArrayList<>(commands.size());
  137. names.addAll(commands.values().stream()
  138. .map(DebugCommand::getName)
  139. .collect(Collectors.toList()));
  140. return names;
  141. }
  142. @Override
  143. public AdditionalTabTargets getSuggestions(final int arg,
  144. final IntelligentCommandContext context) {
  145. AdditionalTabTargets res = new AdditionalTabTargets();
  146. res.excludeAll();
  147. if (arg == 0) {
  148. res.addAll(getCommandNames());
  149. } else {
  150. final DebugCommand command = commands.get(context.getPreviousArgs().get(0));
  151. if (command instanceof IntelligentCommand) {
  152. final IntelligentCommandContext newContext = new IntelligentCommandContext(context.
  153. getWindow(),
  154. context.getPreviousArgs().subList(1,
  155. context.getPreviousArgs().size()),
  156. context.getPartial());
  157. res = ((IntelligentCommand) command).getSuggestions(
  158. arg, newContext);
  159. }
  160. }
  161. return res;
  162. }
  163. }