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
028import java.util.List;
029
030public class CommandHelpFormatter {
031
032    private final CommandManager manager;
033
034    public CommandHelpFormatter(CommandManager manager) {
035        this.manager = manager;
036    }
037
038
039    public void showAllResults(CommandHelp commandHelp, List<HelpEntry> entries) {
040        CommandIssuer issuer = commandHelp.getIssuer();
041        printHelpHeader(commandHelp, issuer);
042        for (HelpEntry e : entries) {
043            printHelpCommand(commandHelp, issuer, e);
044        }
045        printHelpFooter(commandHelp, issuer);
046    }
047
048    public void showSearchResults(CommandHelp commandHelp, List<HelpEntry> entries) {
049        CommandIssuer issuer = commandHelp.getIssuer();
050        printSearchHeader(commandHelp, issuer);
051        for (HelpEntry e : entries) {
052            printSearchEntry(commandHelp, issuer, e);
053        }
054        printSearchFooter(commandHelp, issuer);
055    }
056
057    public void showDetailedHelp(CommandHelp commandHelp, HelpEntry entry) {
058        CommandIssuer issuer = commandHelp.getIssuer();
059
060        // normal help line
061        printDetailedHelpCommand(commandHelp, issuer, entry);
062
063        // additionally detailed help for params
064        for (CommandParameter param : entry.getParameters()) {
065            String description = param.getDescription();
066            if (description != null && !description.isEmpty()) {
067                printDetailedParameter(commandHelp, issuer, entry, param);
068            }
069        }
070    }
071
072    // ########
073    // # help #
074    // ########
075
076    public void printHelpHeader(CommandHelp help, CommandIssuer issuer) {
077        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER, getHeaderFooterFormatReplacements(help));
078    }
079
080    public void printHelpCommand(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
081        String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getEntryFormatReplacements(help, entry));
082        for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
083            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
084        }
085    }
086
087    public void printHelpFooter(CommandHelp help, CommandIssuer issuer) {
088        if (help.isOnlyPage()) {
089            return;
090        }
091        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getHeaderFooterFormatReplacements(help));
092    }
093
094    // ##########
095    // # search #
096    // ##########
097
098    public void printSearchHeader(CommandHelp help, CommandIssuer issuer) {
099        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER, getHeaderFooterFormatReplacements(help));
100    }
101
102    public void printSearchEntry(CommandHelp help, CommandIssuer issuer, HelpEntry page) {
103        String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getEntryFormatReplacements(help, page));
104        for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
105            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
106        }
107    }
108
109    public void printSearchFooter(CommandHelp help, CommandIssuer issuer) {
110        if (help.isOnlyPage()) {
111            return;
112        }
113        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getHeaderFooterFormatReplacements(help));
114    }
115
116
117    // ############
118    // # detailed #
119    // ############
120
121    public void printDetailedHelpHeader(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
122        issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_DETAILED_HEADER,
123                "{command}", entry.getCommand(),
124                "{commandprefix}", help.getCommandPrefix()
125        );
126    }
127
128
129    public void printDetailedHelpCommand(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
130        String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_COMMAND_FORMAT, getEntryFormatReplacements(help, entry));
131        for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
132            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
133        }
134    }
135
136    public void printDetailedParameter(CommandHelp help, CommandIssuer issuer, HelpEntry entry, CommandParameter param) {
137        String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_PARAMETER_FORMAT, getParameterFormatReplacements(help, param, entry));
138        for (String msg : ACFPatterns.NEWLINE.split(formattedMsg)) {
139            issuer.sendMessageInternal(ACFUtil.rtrim(msg));
140        }
141    }
142
143    public void printDetailedHelpFooter(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
144        // default doesn't have a footer
145    }
146
147    /**
148     * Override this to control replacements
149     *
150     * @param help
151     * @return
152     */
153    public String[] getHeaderFooterFormatReplacements(CommandHelp help) {
154        return new String[]{
155                "{search}", help.search != null ? String.join(" ", help.search) : "",
156                "{command}", help.getCommandName(),
157                "{commandprefix}", help.getCommandPrefix(),
158                "{rootcommand}", help.getCommandName(),
159                "{page}", "" + help.getPage(),
160                "{totalpages}", "" + help.getTotalPages(),
161                "{results}", "" + help.getTotalResults()
162        };
163    }
164
165    /**
166     * Override this to control replacements
167     *
168     * @param help
169     * @param entry
170     * @return
171     */
172    public String[] getEntryFormatReplacements(CommandHelp help, HelpEntry entry) {
173        //{command} {parameters} {separator} {description}
174        return new String[]{
175                "{command}", entry.getCommand(),
176                "{commandprefix}", help.getCommandPrefix(),
177                "{parameters}", entry.getParameterSyntax(help.getIssuer()),
178                "{separator}", entry.getDescription().isEmpty() ? "" : "-",
179                "{description}", entry.getDescription()
180        };
181    }
182
183    /**
184     * Override this to control replacements
185     *
186     * @param help
187     * @param param
188     * @param entry
189     * @return
190     */
191    @NotNull
192    public String[] getParameterFormatReplacements(CommandHelp help, CommandParameter param, HelpEntry entry) {
193        //{name} {description}
194        return new String[]{
195                "{name}", param.getDisplayName(help.getIssuer()),
196                "{syntaxorname}", ACFUtil.nullDefault(param.getSyntax(help.getIssuer()), param.getDisplayName(help.getIssuer())),
197                "{syntax}", ACFUtil.nullDefault(param.getSyntax(help.getIssuer()), ""),
198                "{description}", ACFUtil.nullDefault(param.getDescription(), ""),
199                "{command}", help.getCommandName(),
200                "{fullcommand}", entry.getCommand(),
201                "{commandprefix}", help.getCommandPrefix()
202        };
203    }
204}