Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Debug.java 6.9KB

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