Skip to content

Commit 8df3aaa

Browse files
author
Jason Feinstein
committed
Make help() output prettier with groups.
1 parent b120922 commit 8df3aaa

File tree

10 files changed

+42
-12
lines changed

10 files changed

+42
-12
lines changed

library/src/main/java/jwf/debugport/annotations/Command.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
@Target(ElementType.TYPE)
1212
@Retention(RetentionPolicy.RUNTIME)
1313
public @interface Command {
14+
String GROUP_METHOD_INSPECTION = "Method Inspection";
15+
String GROUP_FIELD_INSPECTION = "Field Inspection";
16+
String GROUP_ACCESS = "Access";
17+
String GROUP_OTHER = "Other";
18+
19+
String group() default GROUP_OTHER;
1420

1521
@Target(ElementType.METHOD)
1622
@Retention(RetentionPolicy.RUNTIME)

library/src/main/java/jwf/debugport/internal/debug/commands/call.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
*
1212
*/
13-
@Command
13+
@Command(group = Command.GROUP_ACCESS)
1414
public class call {
1515
/*
1616
* In order to support a varargs-like behavior when the command is called with no third

library/src/main/java/jwf/debugport/internal/debug/commands/exit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
@Command
1212
public class exit {
13-
@Command.Help("Exit this interpreter")
13+
@Command.Help("Exit this interpreter.")
1414
public static void invoke(Interpreter env, CallStack callStack) {
1515
env.println("Thanks for using Android DebugPort!");
1616
try {

library/src/main/java/jwf/debugport/internal/debug/commands/fields.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
*
1010
*/
11-
@Command
11+
@Command(group = Command.GROUP_FIELD_INSPECTION)
1212
public class fields {
1313
@Command.Help("List all of the fields available for a particular object.")
1414
public static void invoke(Interpreter interpreter, CallStack callStack, @Command.ParamName("obj") Object obj) {

library/src/main/java/jwf/debugport/internal/debug/commands/fieldsLocal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
*
1010
*/
11-
@Command
11+
@Command(group = Command.GROUP_FIELD_INSPECTION)
1212
public class fieldsLocal {
1313
@Command.Help("List all of the fields defined locally for an object.")
1414
public static void invoke(Interpreter interpreter, CallStack callStack, @Command.ParamName("obj") Object obj) {

library/src/main/java/jwf/debugport/internal/debug/commands/get.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
*
1111
*/
12-
@Command
12+
@Command(group = Command.GROUP_ACCESS)
1313
public class get {
1414
@Command.Help("Get the value of a field, regardless of access modifiers, on the provided object.")
1515
public static Object invoke(

library/src/main/java/jwf/debugport/internal/debug/commands/help.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import bsh.CallStack;
1313
import bsh.Interpreter;
1414
import jwf.debugport.annotations.Command;
15+
import jwf.debugport.internal.Utils;
1516

1617
/**
1718
*
@@ -20,8 +21,9 @@
2021
public class help {
2122
private static final ArrayList<CommandHelpInfo> sCommandHelp = new ArrayList<>();
2223
private static final int MAX_WIDTH = 100;
23-
private static final int NAME_START_COL = 2;
24-
private static final int HELP_START_COL = 6;
24+
private static final int NAME_START_COL = 6;
25+
private static final int HELP_START_COL = 10;
26+
private static final int GROUP_START_COL = 2;
2527

2628
@Command.Help("Show this help message.")
2729
public static void invoke(Interpreter interpreter, CallStack callStack) {
@@ -38,8 +40,20 @@ public static void invoke(Interpreter interpreter, CallStack callStack) {
3840

3941
StringBuilder builder = new StringBuilder();
4042
builder.append("Available Commands:");
43+
String lastCommandGroup = "__not_a_group__";
4144
for (CommandHelpInfo info : sCommandHelp) {
4245
builder.append("\n");
46+
String commandGroup = info.getCommandGroup();
47+
if (!commandGroup.equalsIgnoreCase(lastCommandGroup)) {
48+
if (!lastCommandGroup.equals("__not_a_group__")) {
49+
builder.append("\n");
50+
}
51+
builder.append(CommandUtils.spaces(GROUP_START_COL))
52+
.append(commandGroup)
53+
.append(":")
54+
.append("\n");
55+
lastCommandGroup = commandGroup;
56+
}
4357
info.appendHelpInfo(builder, nameStartCol, helpStartCol, MAX_WIDTH);
4458
}
4559
builder.append("\n");
@@ -54,15 +68,17 @@ public static void registerCommand(Class command) {
5468
if (!command.isAnnotationPresent(Command.class)) {
5569
return;
5670
}
57-
sCommandHelp.add(new CommandHelpInfo(command));
71+
sCommandHelp.add(new CommandHelpInfo(command, (Command) command.getAnnotation(Command.class)));
5872
Collections.sort(sCommandHelp);
5973
}
6074

6175
public static class CommandHelpInfo implements Comparable<CommandHelpInfo> {
6276
private final Class mCommandClass;
77+
private final Command mAnnotation;
6378

64-
public CommandHelpInfo(Class commandClass) {
79+
public CommandHelpInfo(Class commandClass, Command annotation) {
6580
mCommandClass = commandClass;
81+
mAnnotation = annotation;
6682
}
6783

6884
public int getMaxSignatureLength() {
@@ -82,6 +98,10 @@ public int getMaxSignatureLength() {
8298
return maxLength;
8399
}
84100

101+
public String getCommandGroup() {
102+
return mAnnotation.group();
103+
}
104+
85105
private String getSignature(Method method) {
86106
Annotation[][] paramAnnotations = method.getParameterAnnotations();
87107
Class<?>[] params = method.getParameterTypes();
@@ -167,6 +187,10 @@ public int compareTo(CommandHelpInfo another) {
167187
if (another == null) {
168188
return -1;
169189
}
190+
int groupCompare = mAnnotation.group().compareTo(another.mAnnotation.group());
191+
if (groupCompare != 0) {
192+
return groupCompare;
193+
}
170194
return mCommandClass.getSimpleName().compareTo(another.mCommandClass.getSimpleName());
171195
}
172196

library/src/main/java/jwf/debugport/internal/debug/commands/methods.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
*
1010
*/
11-
@Command
11+
@Command(group = Command.GROUP_METHOD_INSPECTION)
1212
public class methods {
1313
@Command.Help("Get the available methods for the provided object.")
1414
public static void invoke(Interpreter interpreter, CallStack callStack, @Command.ParamName("obj") Object obj) {

library/src/main/java/jwf/debugport/internal/debug/commands/methodsLocal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
*
1010
*/
11-
@Command
11+
@Command(group = Command.GROUP_METHOD_INSPECTION)
1212
public class methodsLocal {
1313
@Command.Help("Show all of the locally-declared methods for the provided object.")
1414
public static void invoke(Interpreter interpreter, CallStack callStack, @Command.ParamName("obj") Object obj) {

library/src/main/java/jwf/debugport/internal/debug/commands/set.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
*
1111
*/
12-
@Command
12+
@Command(group = Command.GROUP_ACCESS)
1313
public class set {
1414
@Command.Help("Set the value of a field on the provided object to the given value, regardless of access modifiers.")
1515
public static Object invoke(

0 commit comments

Comments
 (0)