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.

RedirectCommand.java 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.redirect;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandType;
  21. import com.dmdirc.commandparser.commands.BaseCommand;
  22. import com.dmdirc.commandparser.commands.IntelligentCommand;
  23. import com.dmdirc.commandparser.commands.context.ChatCommandContext;
  24. import com.dmdirc.commandparser.commands.context.CommandContext;
  25. import com.dmdirc.interfaces.Chat;
  26. import com.dmdirc.interfaces.CommandController;
  27. import com.dmdirc.events.eventbus.EventBus;
  28. import com.dmdirc.interfaces.InputModel;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import com.dmdirc.ui.input.AdditionalTabTargets;
  31. import com.dmdirc.ui.input.TabCompleterUtils;
  32. import com.dmdirc.ui.messages.BackBufferFactory;
  33. import javax.annotation.Nonnull;
  34. import javax.inject.Inject;
  35. /**
  36. * The redirect command allows the user to redirect the output from another command that would
  37. * normally echo results locally to a query or channel window instead.
  38. */
  39. public class RedirectCommand extends BaseCommand implements IntelligentCommand {
  40. /** A command info object for this command. */
  41. public static final BaseCommandInfo INFO = new BaseCommandInfo("redirect",
  42. "redirect <command> - sends the output of the command to a "
  43. + "channel or query window",
  44. CommandType.TYPE_CHAT);
  45. /** The bus to dispatch events on. */
  46. private final EventBus eventBus;
  47. private final BackBufferFactory backBufferFactory;
  48. /** Tab-completer utilities. */
  49. private final TabCompleterUtils tabCompleterUtils;
  50. /**
  51. * Creates a new instance of this command.
  52. */
  53. @Inject
  54. public RedirectCommand(
  55. final CommandController controller,
  56. final EventBus eventBus,
  57. final BackBufferFactory backBufferFactory,
  58. final TabCompleterUtils tabCompleterUtils) {
  59. super(controller);
  60. this.eventBus = eventBus;
  61. this.backBufferFactory = backBufferFactory;
  62. this.tabCompleterUtils = tabCompleterUtils;
  63. }
  64. @Override
  65. public void execute(@Nonnull final WindowModel origin,
  66. final CommandArguments args, final CommandContext context) {
  67. final Chat target = ((ChatCommandContext) context).getChat();
  68. target.getWindowModel().getInputModel().map(InputModel::getCommandParser).
  69. ifPresent(cp -> cp.parseCommand(
  70. new FakeWriteableFrameContainer(target.getWindowModel(),
  71. eventBus, backBufferFactory),
  72. args.getArgumentsAsString()));
  73. }
  74. @Override
  75. public AdditionalTabTargets getSuggestions(final int arg,
  76. final IntelligentCommandContext context) {
  77. return tabCompleterUtils.getIntelligentResults(arg, context, 0);
  78. }
  79. }