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.

Time.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.commands;
  23. import com.dmdirc.addons.debug.Debug;
  24. import com.dmdirc.addons.debug.DebugCommand;
  25. import com.dmdirc.commandparser.CommandArguments;
  26. import com.dmdirc.commandparser.commands.IntelligentCommand;
  27. import com.dmdirc.commandparser.commands.context.CommandContext;
  28. import com.dmdirc.interfaces.WindowModel;
  29. import com.dmdirc.ui.input.AdditionalTabTargets;
  30. import com.dmdirc.ui.input.TabCompletionType;
  31. import javax.annotation.Nonnull;
  32. import javax.inject.Inject;
  33. import javax.inject.Provider;
  34. /**
  35. * Times how long it takes to execute commands.
  36. */
  37. public class Time extends DebugCommand implements IntelligentCommand {
  38. /**
  39. * Creates a new instance of the command.
  40. *
  41. * @param commandProvider The provider to use to access the main debug command.
  42. */
  43. @Inject
  44. public Time(final Provider<Debug> commandProvider) {
  45. super(commandProvider);
  46. }
  47. @Override
  48. public String getName() {
  49. return "time";
  50. }
  51. @Override
  52. public String getUsage() {
  53. return "<command to time> - times the specified command";
  54. }
  55. @Override
  56. public void execute(@Nonnull final WindowModel origin,
  57. final CommandArguments args, final CommandContext context) {
  58. doTime(origin, args);
  59. }
  60. /**
  61. * Facilitates timing of a command.
  62. *
  63. * @param origin The origin of the command
  64. * @param args The arguments that were passed to the command
  65. */
  66. private void doTime(final WindowModel origin,
  67. final CommandArguments args) {
  68. if (args.getArguments().length == 0) {
  69. showUsage(origin, args.isSilent(), getName(), getUsage());
  70. return;
  71. }
  72. if (origin.isWritable()) {
  73. final long start = System.currentTimeMillis();
  74. origin.getCommandParser().parseCommand(origin, args.getArgumentsAsString(0));
  75. final long end = System.currentTimeMillis();
  76. sendLine(origin, args.isSilent(), FORMAT_OUTPUT,
  77. "Command executed in " + (end - start) + " milliseconds.");
  78. }
  79. }
  80. @Override
  81. public AdditionalTabTargets getSuggestions(final int arg,
  82. final IntelligentCommandContext context) {
  83. final AdditionalTabTargets res = new AdditionalTabTargets();
  84. res.excludeAll();
  85. if (arg == 1) {
  86. res.include(TabCompletionType.COMMAND);
  87. }
  88. return res;
  89. }
  90. }