Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Time.java 3.7KB

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