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