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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.debug;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.BaseCommand;
  23. import com.dmdirc.commandparser.commands.IntelligentCommand;
  24. import com.dmdirc.commandparser.commands.context.CommandContext;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.interfaces.WindowModel;
  27. import com.dmdirc.ui.input.AdditionalTabTargets;
  28. import javax.annotation.Nonnull;
  29. import javax.inject.Inject;
  30. import javax.inject.Singleton;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.Collection;
  34. import java.util.HashMap;
  35. import java.util.Map;
  36. import java.util.Set;
  37. import java.util.stream.Collectors;
  38. /**
  39. * Provides various handy ways to test or debug the client.
  40. */
  41. @Singleton
  42. public class Debug extends BaseCommand implements IntelligentCommand {
  43. /** A command info object for this command. */
  44. public static final CommandInfo INFO = new BaseCommandInfo("debug",
  45. "debug <command> [args] - facilitates debugging of DMDirc",
  46. CommandType.TYPE_GLOBAL);
  47. /** The command controller to use to lookup command information. */
  48. private final CommandController controller;
  49. /** List of registered debug commands. */
  50. private final Map<String, DebugCommand> commands;
  51. /**
  52. * Creates a new debug command.
  53. *
  54. * @param controller The command controller to use to lookup command information.
  55. * @param subcommands The subcommands to be loaded.
  56. */
  57. @Inject
  58. public Debug(
  59. final CommandController controller,
  60. final Set<DebugCommand> subcommands) {
  61. super(controller);
  62. this.controller = controller;
  63. this.commands = new HashMap<>(subcommands.size());
  64. for (DebugCommand command : subcommands) {
  65. commands.put(command.getName(), command);
  66. }
  67. }
  68. @Override
  69. public void execute(@Nonnull final WindowModel origin,
  70. final CommandArguments args, final CommandContext context) {
  71. if (args.getArguments().length == 0) {
  72. showUsage(origin, args.isSilent(), "debug",
  73. "<debug command> [options]");
  74. } else {
  75. final DebugCommand command = commands.get(args.getArguments()[0]);
  76. if (command == null) {
  77. showError(origin, args.isSilent(), "Unknown debug action.");
  78. } else {
  79. final CommandArguments newArgs = new CommandArguments(
  80. controller,
  81. Arrays.asList((controller.getCommandChar()
  82. + command.getName() + ' '
  83. + args.getArgumentsAsString(1)).split(" ")));
  84. command.execute(origin, newArgs, context);
  85. }
  86. }
  87. }
  88. /**
  89. * Shows output, if appropriate, in the specified target.
  90. *
  91. * @param target The command window to send the line to
  92. * @param isSilent Whether this command is being silenced or not
  93. * @param message The message to be sent
  94. */
  95. public void proxyShowOutput(final WindowModel target,
  96. final boolean isSilent, final String message) {
  97. showOutput(target, isSilent, message);
  98. }
  99. /**
  100. * Shows an error, if appropriate, in the specified target.
  101. *
  102. * @param target The command window to send the line to
  103. * @param isSilent Whether this command is being silenced or not
  104. * @param message The message to be sent
  105. */
  106. public void proxyShowError(final WindowModel target,
  107. final boolean isSilent, final String message) {
  108. showError(target, isSilent, message);
  109. }
  110. /**
  111. * Sends a usage line, if appropriate, to the specified target.
  112. *
  113. * @param target The command window to send the line to
  114. * @param isSilent Whether this command is being silenced or not
  115. * @param name The name of the command that's raising the error
  116. * @param args The arguments that the command accepts or expects
  117. */
  118. public void proxyShowUsage(final WindowModel target,
  119. final boolean isSilent, final String name, final String args) {
  120. showUsage(target, isSilent, INFO.getName(), name + ' ' + args);
  121. }
  122. /**
  123. * Formats the specified data into a table suitable for output in the textpane. It is expected
  124. * that each String[] in data has the same number of elements as the headers array.
  125. *
  126. * @param headers The headers of the table.
  127. * @param data The contents of the table.
  128. *
  129. * @return A string containing an ASCII table
  130. */
  131. public String proxyDoTable(final String[] headers, final String[][] data) {
  132. return doTable(headers, data);
  133. }
  134. /**
  135. * Returns a list of command names.
  136. *
  137. * @return List of command names
  138. */
  139. public Collection<String> getCommandNames() {
  140. final Collection<String> names = new ArrayList<>(commands.size());
  141. names.addAll(commands.values().stream()
  142. .map(DebugCommand::getName)
  143. .collect(Collectors.toList()));
  144. return names;
  145. }
  146. @Override
  147. public AdditionalTabTargets getSuggestions(final int arg,
  148. final IntelligentCommandContext context) {
  149. AdditionalTabTargets res = new AdditionalTabTargets();
  150. res.excludeAll();
  151. if (arg == 0) {
  152. res.addAll(getCommandNames());
  153. } else {
  154. final DebugCommand command = commands.get(context.getPreviousArgs().get(0));
  155. if (command instanceof IntelligentCommand) {
  156. final IntelligentCommandContext newContext = new IntelligentCommandContext(context.
  157. getWindow(),
  158. context.getPreviousArgs().subList(1,
  159. context.getPreviousArgs().size()),
  160. context.getPartial());
  161. res = ((IntelligentCommand) command).getSuggestions(
  162. arg, newContext);
  163. }
  164. }
  165. return res;
  166. }
  167. }