Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Debug.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. * Shows output, if appropriate, in 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 message The message to be sent
  99. */
  100. public void proxyShowOutput(final WindowModel target,
  101. final boolean isSilent, final String message) {
  102. showOutput(target, isSilent, message);
  103. }
  104. /**
  105. * Shows an error, if appropriate, in the specified target.
  106. *
  107. * @param target The command window to send the line to
  108. * @param isSilent Whether this command is being silenced or not
  109. * @param message The message to be sent
  110. */
  111. public void proxyShowError(final WindowModel target,
  112. final boolean isSilent, final String message) {
  113. showError(target, isSilent, message);
  114. }
  115. /**
  116. * Sends a usage line, if appropriate, to the specified target.
  117. *
  118. * @param target The command window to send the line to
  119. * @param isSilent Whether this command is being silenced or not
  120. * @param name The name of the command that's raising the error
  121. * @param args The arguments that the command accepts or expects
  122. */
  123. public void proxyShowUsage(final WindowModel target,
  124. final boolean isSilent, final String name, final String args) {
  125. showUsage(target, isSilent, INFO.getName(), name + ' ' + args);
  126. }
  127. /**
  128. * Formats the specified data into a table suitable for output in the textpane. It is expected
  129. * that each String[] in data has the same number of elements as the headers array.
  130. *
  131. * @param headers The headers of the table.
  132. * @param data The contents of the table.
  133. *
  134. * @return A string containing an ASCII table
  135. */
  136. public String proxyDoTable(final String[] headers, final String[][] data) {
  137. return doTable(headers, data);
  138. }
  139. /**
  140. * Returns a list of command names.
  141. *
  142. * @return List of command names
  143. */
  144. public Collection<String> getCommandNames() {
  145. final Collection<String> names = new ArrayList<>(commands.size());
  146. names.addAll(commands.values().stream()
  147. .map(DebugCommand::getName)
  148. .collect(Collectors.toList()));
  149. return names;
  150. }
  151. @Override
  152. public AdditionalTabTargets getSuggestions(final int arg,
  153. final IntelligentCommandContext context) {
  154. AdditionalTabTargets res = new AdditionalTabTargets();
  155. res.excludeAll();
  156. if (arg == 0) {
  157. res.addAll(getCommandNames());
  158. } else {
  159. final DebugCommand command = commands.get(context.getPreviousArgs().get(0));
  160. if (command instanceof IntelligentCommand) {
  161. final IntelligentCommandContext newContext = new IntelligentCommandContext(context.
  162. getWindow(),
  163. context.getPreviousArgs().subList(1,
  164. context.getPreviousArgs().size()),
  165. context.getPartial());
  166. res = ((IntelligentCommand) command).getSuggestions(
  167. arg, newContext);
  168. }
  169. }
  170. return res;
  171. }
  172. }