Clean up help formatter method signatures

This commit is contained in:
Aikar
2018-03-21 22:56:19 -04:00
parent e8bb460ae0
commit db94aa4317
5 changed files with 111 additions and 69 deletions
@@ -42,8 +42,11 @@ public class CommandHelp {
final String commandPrefix;
private int page;
private int perPage;
private List<String> search;
List<String> search;
private HelpEntry selectedEntry;
private int totalResults;
private int totalPages;
private boolean lastPage;
public CommandHelp(CommandManager manager, RootCommand rootCommand, CommandIssuer issuer) {
this.manager = manager;
@@ -136,22 +139,17 @@ public class CommandHelp {
helpEntries = getHelpEntries();
results = helpEntries.iterator();
}
int totalResults = helpEntries.size();
this.totalResults = helpEntries.size();
int min = (this.page - 1) * this.perPage; // TODO: per page configurable?
int max = min + this.perPage;
int totalPages = (int) Math.ceil((float) totalResults / (float) this.perPage);
this.totalPages = (int) Math.ceil((float) totalResults / (float) this.perPage);
int i = 0;
if (min >= totalResults) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_NO_RESULTS);
return;
}
if (search == null) {
manager.getHelpFormatter().printHelpHeader(issuer, commandName, page, totalPages, totalResults);
} else {
manager.getHelpFormatter().printSearchHeader(issuer, commandName, page, totalPages, totalResults, search);
}
List<HelpEntry> printEntries = new ArrayList<>();
while (results.hasNext()) {
HelpEntry e = results.next();
if (i >= max) {
@@ -160,40 +158,51 @@ public class CommandHelp {
if (i++ < min) {
continue;
}
printEntries.add(e);
}
this.lastPage = !(min > 0 || results.hasNext());
CommandHelpFormatter formatter = manager.getHelpFormatter();
if (search == null) {
formatter.printHelpHeader(this, issuer);
} else {
formatter.printSearchHeader(this, issuer);
}
for (HelpEntry e : printEntries) {
if (search == null) {
manager.getHelpFormatter().printHelpLine(issuer, commandName, e);
formatter.printHelpEntry(this, issuer, e);
} else {
manager.getHelpFormatter().printSearchLine(issuer, commandName, e, e.getSearchScore());
formatter.printSearchEntry(this, issuer, e);
}
}
boolean lastPage = !(min > 0 || results.hasNext());
if (search == null) {
manager.getHelpFormatter().printHelpFooter(issuer, commandName, page, totalPages, totalResults, lastPage);
formatter.printHelpFooter(this, issuer);
} else {
manager.getHelpFormatter().printSearchFooter(issuer, commandName, page, totalPages, totalResults, search, lastPage);
formatter.printSearchFooter(this, issuer);
}
}
public void showDetailedHelp(HelpEntry entry, CommandIssuer issuer) {
// header
CommandHelpFormatter formatter = manager.getHelpFormatter();
formatter.printDetailedHelpHeader(issuer, commandName, entry);
formatter.printDetailedHelpHeader(this, issuer, commandName, entry);
// normal help line
formatter.printHelpLine(issuer, commandName, entry);
formatter.printHelpEntry(this, issuer, entry);
// additionally detailed help for params
for (CommandParameter param : entry.getParameters()) {
String description = param.getDescription();
if (description != null && !description.isEmpty()) {
formatter.printDetailedHelpLine(issuer, commandName, entry, param.getName(), description);
formatter.printDetailedParameter(this, issuer, entry, param);
}
}
// footer
formatter.printDetailedHelpFooter(issuer, commandName, entry);
formatter.printDetailedHelpFooter(this, issuer, entry);
}
public List<HelpEntry> getHelpEntries() {
@@ -217,4 +226,44 @@ public class CommandHelp {
this.search = search;
getHelpEntries().forEach(this::updateSearchScore);
}
public CommandIssuer getIssuer() {
return issuer;
}
public String getCommandName() {
return commandName;
}
public String getCommandPrefix() {
return commandPrefix;
}
public int getPage() {
return page;
}
public int getPerPage() {
return perPage;
}
public List<String> getSearch() {
return search;
}
public HelpEntry getSelectedEntry() {
return selectedEntry;
}
public int getTotalResults() {
return totalResults;
}
public int getTotalPages() {
return totalPages;
}
public boolean isLastPage() {
return lastPage;
}
}
@@ -25,8 +25,6 @@ package co.aikar.commands;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class CommandHelpFormatter {
private final CommandManager manager;
@@ -39,54 +37,44 @@ public class CommandHelpFormatter {
// # help #
// ########
public void printHelpHeader(CommandIssuer issuer, String command, int page, int totalPages, int totalResults) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER,
"{page}", "" + page,
"{totalpages}", "" + totalPages,
"{results}", "" + totalResults,
"{command}", "" + command
);
public void printHelpHeader(CommandHelp help, CommandIssuer issuer) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_HEADER, getHeaderFooterFormatReplacements(help));
}
public void printHelpLine(CommandIssuer issuer, String command, HelpEntry page) {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page, command));
public void printHelpEntry(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getEntryFormatReplacements(help, entry));
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
}
}
public void printHelpFooter(CommandIssuer issuer, String command, int page, int totalPages, int totalResults, boolean lastPage) {
if (lastPage) {
public void printHelpFooter(CommandHelp help, CommandIssuer issuer) {
if (help.isLastPage()) {
return;
}
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION,
"{page}", "" + page,
"{totalpages}", "" + totalPages,
"{results}", "" + totalResults,
"{command}", "" + command
);
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getHeaderFooterFormatReplacements(help));
}
// ##########
// # search #
// ##########
public void printSearchHeader(CommandIssuer issuer, String command, int page, int totalPages, int totalResults, List<String> search) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER, getPaginationFormatReplacements(command, page, totalPages, totalResults, search));
public void printSearchHeader(CommandHelp help, CommandIssuer issuer) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_SEARCH_HEADER, getHeaderFooterFormatReplacements(help));
}
public void printSearchLine(CommandIssuer issuer, String command, HelpEntry page, int score) {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getFormatReplacements(page, command));
public void printSearchEntry(CommandHelp help, CommandIssuer issuer, HelpEntry page) {
String formatted = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_FORMAT, getEntryFormatReplacements(help, page));
for (String msg : ACFPatterns.NEWLINE.split(formatted)) {
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
}
}
public void printSearchFooter(CommandIssuer issuer, String command, int page, int totalPages, int totalResults, List<String> search, boolean lastPage) {
if (lastPage) {
public void printSearchFooter(CommandHelp help, CommandIssuer issuer) {
if (help.isLastPage()) {
return;
}
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getPaginationFormatReplacements(command, page, totalPages, totalResults, search)
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_PAGE_INFORMATION, getHeaderFooterFormatReplacements(help)
);
}
@@ -95,48 +83,53 @@ public class CommandHelpFormatter {
// # detailed #
// ############
public void printDetailedHelpHeader(CommandIssuer issuer, String command, HelpEntry entry) {
public void printDetailedHelpHeader(CommandHelp help, CommandIssuer issuer, String command, HelpEntry entry) {
issuer.sendMessage(MessageType.HELP, MessageKeys.HELP_DETAILED_HEADER,
"{command}", entry.getCommand(),
"{command}", command
);
}
public void printDetailedHelpLine(CommandIssuer issuer, String rootCommand, HelpEntry entry, String subCommand, String paramDescription) {
String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_FORMAT, getDetailedFormatReplacements(subCommand, paramDescription, entry, rootCommand));
public void printDetailedParameter(CommandHelp help, CommandIssuer issuer, HelpEntry entry, CommandParameter param) {
String formattedMsg = this.manager.formatMessage(issuer, MessageType.HELP, MessageKeys.HELP_DETAILED_PARAMETER_FORMAT, getParameterFormatReplacements(help, param.getName(), param.getDescription(), entry));
for (String msg : ACFPatterns.NEWLINE.split(formattedMsg)) {
issuer.sendMessageInternal(ACFUtil.rtrim(msg));
}
}
public void printDetailedHelpFooter(CommandIssuer issuer, String command, HelpEntry page) {
public void printDetailedHelpFooter(CommandHelp help, CommandIssuer issuer, HelpEntry entry) {
// default doesn't have a footer
}
@NotNull
public String[] getPaginationFormatReplacements(String command, int page, int totalPages, int totalResults, List<String> search) {
/**
* Override this to control replacements
*
* @param help
* @return
*/
public String[] getHeaderFooterFormatReplacements(CommandHelp help) {
return new String[]{
"{search}", String.join(" ", search),
"{command}", command,
"{rootcommand}", command,
"{page}", "" + page,
"{totalpages}", "" + totalPages,
"{results}", "" + totalResults
"{search}", help.search != null ? String.join(" ", help.search) : "",
"{command}", help.getCommandName(),
"{rootcommand}", help.getCommandName(),
"{page}", "" + help.getPage(),
"{totalpages}", "" + help.getTotalPages(),
"{results}", "" + help.getTotalResults()
};
}
/**
* Override this to control replacements
*
* @param help
* @param e
* @param command
* @return
*/
public String[] getFormatReplacements(HelpEntry e, String command) {
public String[] getEntryFormatReplacements(CommandHelp help, HelpEntry e) {
//{command} {parameters} {separator} {description}
return new String[]{
"{command}", e.getCommand(),
"{rootcommand}", command,
"{rootcommand}", help.getCommandName(),
"{parameters}", e.getParameterSyntax(),
"{separator}", e.getDescription().isEmpty() ? "" : "-",
"{description}", e.getDescription()
@@ -146,20 +139,20 @@ public class CommandHelpFormatter {
/**
* Override this to control replacements
*
* @param help
* @param name
* @param description
* @param page
* @param rootCommand
* @return
*/
@NotNull
public String[] getDetailedFormatReplacements(String name, String description, HelpEntry page, String rootCommand) {
public String[] getParameterFormatReplacements(CommandHelp help, String name, String description, HelpEntry page) {
//{name} {description}
return new String[]{
"{name}", name,
"{description}", description,
"{command}", page.getCommand(),
"{rootcommand}", rootCommand,
"{description}", description
"{rootcommand}", help.getCommandName()
};
}
}
@@ -43,11 +43,11 @@ public class HelpEntry {
}
public String getParameterSyntax(){
return this.command.syntaxText;
return this.command.syntaxText != null ? this.command.syntaxText : "";
}
public String getDescription(){
return this.command.helpText;
return this.command.helpText != null ? this.command.helpText : "";
}
public void setSearchScore(int searchScore) {
@@ -52,7 +52,7 @@ public enum MessageKeys implements MessageKeyProvider {
HELP_HEADER,
HELP_FORMAT,
HELP_DETAILED_HEADER,
HELP_DETAILED_FORMAT,
HELP_DETAILED_PARAMETER_FORMAT,
HELP_SEARCH_HEADER,
;
+3 -3
View File
@@ -40,6 +40,6 @@ acf-core.help_page_information = - Showing page <c2>{page}</c2> of <c2>{totalpag
acf-core.help_no_results = Error: No more results.
acf-core.help_header = <c3>=== </c3><c1>Showing help for </c1><c2>{command}</c2><c3> ===</c3>
acf-core.help_format = <c1>{command}</c1> <c2>{parameters}</c2> <c3>{separator} {description}</c3>
acf-core.help_detailed_header = <c3>=== </c3><c1>Showing detailed help for </c1><c2>{command}</c2><c3> ===</c3>
acf-core.help_detailed_format = <c2>{name}</c2>: <c3>{description}</c3>
acf-core.help_search_header = <c3>=== </c3><c1>Showing help for </c1><c2>{command}</c2> <c1>{search}</c1><c3> ===</c3>
acf-core.help_detailed_header = <c3>=== </c3><c1>Showing detailed help for </c1><c2>{command}</c1><c3> ===</c3>
acf-core.help_detailed_parameter_format = <c2>{name}</c2>: <c3>{description}</c3>
acf-core.help_search_header = <c3>=== </c3><c1>Search results for </c1><c2>{command} {search}</c2><c3> ===</c3>