changeset 1600:0f55b554d197

Shorten descriptions for command summaries Reviewed-by: jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-December/012052.html PR2130
author Omair Majid <omajid@redhat.com>
date Mon, 08 Dec 2014 10:26:02 -0500
parents 3570aec27794
children a75dfbb7a137
files launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java
diffstat 2 files changed, 49 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java	Wed Dec 03 16:56:00 2014 -0700
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/HelpCommand.java	Mon Dec 08 10:26:02 2014 -0500
@@ -113,7 +113,29 @@
     }
 
     private void printCommandSummary(TableRenderer renderer, CommandInfo info) {
-        renderer.printLine(" " + info.getName(), info.getDescription());
+        renderer.printLine(" " + info.getName(), createShortDescription(info));
+    }
+
+    private String createShortDescription(CommandInfo info) {
+        String fullDescription = info.getDescription();
+
+        int firstDot = fullDescription.indexOf('.');
+        if (firstDot == -1) {
+            return fullDescription;
+        }
+
+        String firstSentence = fullDescription.substring(0, firstDot);
+        String shortDescription = firstSentence.trim();
+        shortDescription = lowerCaseFirstLetter(shortDescription);
+        return shortDescription;
+    }
+
+    private String lowerCaseFirstLetter(String shortDescription) {
+        if (Character.isUpperCase(shortDescription.charAt(0))) {
+            shortDescription = shortDescription.substring(0, 1).toLowerCase()
+                    + shortDescription.substring(1);
+        }
+        return shortDescription;
     }
 
     private void printCommandUsage(CommandContext ctx, String cmdName) {
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java	Wed Dec 03 16:56:00 2014 -0700
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/HelpCommandTest.java	Mon Dec 08 10:26:02 2014 -0500
@@ -128,6 +128,32 @@
     }
 
     @Test
+    public void verifyHelpShortensCommandDescriptions() {
+        Collection<CommandInfo> infoList = new ArrayList<CommandInfo>();
+
+        CommandInfo info1 = mock(CommandInfo.class);
+        when(info1.getName()).thenReturn("test1");
+        when(info1.getDescription()).thenReturn("A test command. This command does some test stuff."
+                + "This is a very, very long description that provides too much information for the summary");
+        when(info1.getEnvironments()).thenReturn(EnumSet.of(Environment.CLI, Environment.SHELL));
+
+        infoList.add(info1);
+
+        when(infos.getCommandInfos()).thenReturn(infoList);
+
+        HelpCommand cmd = new HelpCommand();
+        cmd.setEnvironment(Environment.CLI);
+        cmd.setCommandInfoSource(infos);
+
+        Arguments args = mock(Arguments.class);
+        cmd.run(ctxFactory.createContext(args));
+        String expected = "list of commands:\n\n"
+                        + " test1         a test command\n";
+        String actual = ctxFactory.getOutput();
+        assertEquals(expected, actual);
+    }
+
+    @Test
     public void verifyHelpKnownCmdPrintsCommandUsage() {
         CommandInfo testCommandInfo = mock(CommandInfo.class);
         when(testCommandInfo.getName()).thenReturn("test1");