您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AliasCommand.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.commandparser.commands.global;
  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.aliases.Alias;
  23. import com.dmdirc.commandparser.aliases.AliasFactory;
  24. import com.dmdirc.commandparser.aliases.AliasManager;
  25. import com.dmdirc.commandparser.commands.BaseCommand;
  26. import com.dmdirc.commandparser.commands.IntelligentCommand;
  27. import com.dmdirc.commandparser.commands.context.CommandContext;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.interfaces.WindowModel;
  30. import com.dmdirc.ui.input.AdditionalTabTargets;
  31. import com.dmdirc.ui.input.TabCompleterUtils;
  32. import javax.annotation.Nonnull;
  33. import javax.inject.Inject;
  34. import java.util.Optional;
  35. /**
  36. * The alias command allows users to create aliases on-the-fly.
  37. */
  38. public class AliasCommand extends BaseCommand implements IntelligentCommand {
  39. /** A command info object for this command. */
  40. public static final CommandInfo INFO = new BaseCommandInfo("alias",
  41. "alias [--remove] <name> [command] - creates or removes the specified alias",
  42. CommandType.TYPE_GLOBAL);
  43. /** Factory to use when creating aliases. */
  44. private final AliasFactory aliasFactory;
  45. /** Manager to use to modify aliases. */
  46. private final AliasManager aliasManager;
  47. /** Tab-completer utilities. */
  48. private final TabCompleterUtils tabCompleterUtils;
  49. /**
  50. * Creates a new instance of {@link AliasCommand}.
  51. *
  52. * @param controller The controller that owns this command.
  53. * @param aliasFactory The factory to use when creating new aliases.
  54. * @param aliasManager The manager to use to modify aliases.
  55. */
  56. @Inject
  57. public AliasCommand(
  58. final CommandController controller,
  59. final AliasFactory aliasFactory,
  60. final AliasManager aliasManager,
  61. final TabCompleterUtils tabCompleterUtils) {
  62. super(controller);
  63. this.aliasFactory = aliasFactory;
  64. this.aliasManager = aliasManager;
  65. this.tabCompleterUtils = tabCompleterUtils;
  66. }
  67. @Override
  68. public void execute(@Nonnull final WindowModel origin,
  69. final CommandArguments args, final CommandContext context) {
  70. if (args.getArguments().length < 2) {
  71. showUsage(origin, args.isSilent(), "alias", "[--remove] <name> [command]");
  72. return;
  73. }
  74. if ("--remove".equalsIgnoreCase(args.getArguments()[0])) {
  75. final String name = removeCommandChar(args.getArguments()[1]);
  76. if (doRemove(name)) {
  77. showOutput(origin, args.isSilent(), "Alias '" + name + "' removed.");
  78. } else {
  79. showError(origin, args.isSilent(), "Alias '" + name + "' not found.");
  80. }
  81. return;
  82. }
  83. final String name = removeCommandChar(args.getArguments()[0]);
  84. if (aliasManager.getAlias(name).isPresent()) {
  85. showError(origin, args.isSilent(), "Alias '" + name + "' already exists.");
  86. return;
  87. }
  88. final Alias myAlias = aliasFactory.createAlias(name, 0,
  89. removeCommandChar(args.getArgumentsAsString(1)));
  90. aliasManager.addAlias(myAlias);
  91. showOutput(origin, args.isSilent(), "Alias '" + name + "' created.");
  92. }
  93. /**
  94. * Removes the alias with the specified name.
  95. *
  96. * @param name The name of the alias to remove
  97. *
  98. * @return True if the alias was deleted, false otherwise
  99. */
  100. private boolean doRemove(final String name) {
  101. final Optional<Alias> alias = aliasManager.getAlias(name);
  102. alias.ifPresent(aliasManager::removeAlias);
  103. return alias.isPresent();
  104. }
  105. @Override
  106. public AdditionalTabTargets getSuggestions(final int arg,
  107. final IntelligentCommandContext context) {
  108. final AdditionalTabTargets res = new AdditionalTabTargets().excludeAll();
  109. if (arg == 0) {
  110. res.add("--remove");
  111. } else if (arg == 1 && "--remove".equals(context.getPreviousArgs().get(0))) {
  112. res.addAll(aliasManager.getAliasNames());
  113. } else if (arg >= 1 && !"--remove".equals(context.getPreviousArgs().get(0))) {
  114. return tabCompleterUtils.getIntelligentResults(arg, context, 1);
  115. }
  116. return res;
  117. }
  118. private String removeCommandChar(final String input) {
  119. return input.charAt(0) == getController().getCommandChar()
  120. ? input.substring(1) : input;
  121. }
  122. }