001/*
002 * Copyright (c) 2016-2018 Daniel Ennis (Aikar) - MIT License
003 *
004 *  Permission is hereby granted, free of charge, to any person obtaining
005 *  a copy of this software and associated documentation files (the
006 *  "Software"), to deal in the Software without restriction, including
007 *  without limitation the rights to use, copy, modify, merge, publish,
008 *  distribute, sublicense, and/or sell copies of the Software, and to
009 *  permit persons to whom the Software is furnished to do so, subject to
010 *  the following conditions:
011 *
012 *  The above copyright notice and this permission notice shall be
013 *  included in all copies or substantial portions of the Software.
014 *
015 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
016 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
017 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
018 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
019 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
020 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
021 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
022 */
023
024package co.aikar.commands;
025
026import org.jetbrains.annotations.NotNull;
027
028public class CommandHelpFormatter {
029
030    private final CommandManager manager;
031
032    public CommandHelpFormatter(CommandManager manager) {
033        this.manager = manager;
034    }
035
036    // ########
037    // # help #
038    // ########
039
040    public void printHelpHeader(CommandHelp help, CommandIssuer issuer) {
041        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER, getHeaderFooterFormatReplacements(help));
042    }
043
044    public void printHelpCommand(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
045        String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getEntryFormatReplacements(help, entry));
046        for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
047            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
048        }
049    }
050
051    public void printHelpFooter(CommandHelp help, CommandIssuer issuer) {
052        if (help.isLastPage()) {
053            return;
054        }
055        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getHeaderFooterFormatReplacements(help));
056    }
057
058    // ##########
059    // # search #
060    // ##########
061
062    public void printSearchHeader(CommandHelp help, CommandIssuer issuer) {
063        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER, getHeaderFooterFormatReplacements(help));
064    }
065
066    public void printSearchEntry(CommandHelp help, CommandIssuer issuer, HelpEntry page) {
067        String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getEntryFormatReplacements(help, page));
068        for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
069            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
070        }
071    }
072
073    public void printSearchFooter(CommandHelp help, CommandIssuer issuer) {
074        if (help.isLastPage()) {
075            return;
076        }
077        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getHeaderFooterFormatReplacements(help));
078    }
079
080
081    // ############
082    // # detailed #
083    // ############
084
085    public void printDetailedHelpHeader(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
086        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_DETAILED_HEADER,
087                "{command}", entry.getCommand(),
088                "{commandprefix}", help.getCommandPrefix()
089        );
090    }
091
092
093    public void printDetailedHelpCommand(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
094        String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_COMMAND_FORMAT, getEntryFormatReplacements(help, entry));
095        for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
096            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
097        }
098    }
099
100    public void printDetailedParameter(CommandHelp help, CommandIssuer issuer, HelpEntry entry, CommandParameter param) {
101        String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_PARAMETER_FORMAT, getParameterFormatReplacements(help, param, entry));
102        for (String msg : ACFPatterns.NEWLINE.split(formattedMsg)) {
103            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
104        }
105    }
106
107    public void printDetailedHelpFooter(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
108        // default doesn't have a footer
109    }
110
111    /**
112     * Override this to control replacements
113     *
114     * @param help
115     * @return
116     */
117    public String[] getHeaderFooterFormatReplacements(CommandHelp help) {
118        return new String[]{
119                "{search}", help.search != null ? String.join(" ", help.search) : "",
120                "{command}", help.getCommandName(),
121                "{commandprefix}", help.getCommandPrefix(),
122                "{rootcommand}", help.getCommandName(),
123                "{page}", "" + help.getPage(),
124                "{totalpages}", "" + help.getTotalPages(),
125                "{results}", "" + help.getTotalResults()
126        };
127    }
128
129    /**
130     * Override this to control replacements
131     *
132     * @param help
133     * @param entry
134     * @return
135     */
136    public String[] getEntryFormatReplacements(CommandHelp help, HelpEntry entry) {
137        //{command} {parameters} {separator} {description}
138        return new String[]{
139                "{command}", entry.getCommand(),
140                "{commandprefix}", help.getCommandPrefix(),
141                "{parameters}", entry.getParameterSyntax(),
142                "{separator}", entry.getDescription().isEmpty() ? "" : "-",
143                "{description}", entry.getDescription()
144        };
145    }
146
147    /**
148     * Override this to control replacements
149     *
150     * @param help
151     * @param param
152     * @param entry
153     * @return
154     */
155    @NotNull
156    public String[] getParameterFormatReplacements(CommandHelp help, CommandParameter param, HelpEntry entry) {
157        //{name} {description}
158        return new String[]{
159                "{name}", param.getName(),
160                "{syntax}", ACFUtil.nullDefault(param.getSyntax(), ""),
161                "{description}", ACFUtil.nullDefault(param.getDescription(), ""),
162                "{command}", help.getCommandName(),
163                "{fullcommand}", entry.getCommand(),
164                "{commandprefix}", help.getCommandPrefix()
165        };
166    }
167}