diff --git a/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java b/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java index 2667a6606af..d649e8250de 100644 --- a/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java +++ b/smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java @@ -121,15 +121,17 @@ public void unexpectedTemplate() { .append("Invalid template `blabla`. `Smithy-Examples` provides the following templates:") .append(System.lineSeparator()) .append(System.lineSeparator()) - .append("NAME DOCUMENTATION") + .append("───────────────────── ─────────────────────────────────────────────────────────────────────────────") .append(System.lineSeparator()) - .append("--------------------- -----------------------------------------------------") + .append("NAME DOCUMENTATION") .append(System.lineSeparator()) - .append("included-file-json Smithy Quickstart example with json file included. ") + .append("───────────────────── ─────────────────────────────────────────────────────────────────────────────") .append(System.lineSeparator()) - .append("included-files-gradle Smithy Quickstart example with gradle files included.") + .append("included-file-json Smithy Quickstart example with json file included.") .append(System.lineSeparator()) - .append("quickstart-cli Smithy Quickstart example weather service. ") + .append("included-files-gradle Smithy Quickstart example with gradle files included.") + .append(System.lineSeparator()) + .append("quickstart-cli Smithy Quickstart example weather service.") .append(System.lineSeparator()) .toString(); @@ -191,17 +193,19 @@ public void withListArg() { "init", "--list", "--url", templatesDir.toString())); String expectedOutput = new StringBuilder() - .append("NAME DOCUMENTATION") - .append(System.lineSeparator()) - .append("--------------------- -----------------------------------------------------") - .append(System.lineSeparator()) - .append("included-file-json Smithy Quickstart example with json file included. ") - .append(System.lineSeparator()) - .append("included-files-gradle Smithy Quickstart example with gradle files included.") - .append(System.lineSeparator()) - .append("quickstart-cli Smithy Quickstart example weather service. ") - .append(System.lineSeparator()) - .toString(); + .append("───────────────────── ─────────────────────────────────────────────────────────────────────────────") + .append(System.lineSeparator()) + .append("NAME DOCUMENTATION") + .append(System.lineSeparator()) + .append("───────────────────── ─────────────────────────────────────────────────────────────────────────────") + .append(System.lineSeparator()) + .append("included-file-json Smithy Quickstart example with json file included.") + .append(System.lineSeparator()) + .append("included-files-gradle Smithy Quickstart example with gradle files included.") + .append(System.lineSeparator()) + .append("quickstart-cli Smithy Quickstart example weather service.") + .append(System.lineSeparator()) + .toString(); assertThat(result.getOutput(), containsString(expectedOutput)); assertThat(result.getExitCode(), is(0)); diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/ColorTheme.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/ColorTheme.java index 5e012d005fb..e4aa1842ccf 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/ColorTheme.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/ColorTheme.java @@ -49,5 +49,8 @@ public final class ColorTheme { public static final Style HINT_TITLE = Style.of(Style.BRIGHT_GREEN); + public static final Style TEMPLATE_TITLE = Style.of(Style.BOLD, Style.BRIGHT_WHITE); + public static final Style TEMPLATE_LIST_BORDER = Style.of(Style.BRIGHT_WHITE); + private ColorTheme() {} } diff --git a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java index bd4a85c2224..3ef7154394e 100644 --- a/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java +++ b/smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java @@ -41,8 +41,11 @@ import software.amazon.smithy.model.node.StringNode; import software.amazon.smithy.utils.IoUtils; import software.amazon.smithy.utils.ListUtils; +import software.amazon.smithy.utils.StringUtils; final class InitCommand implements Command { + private static final int LINE_LENGTH = 100; + private static final String COLUMN_SEPARATOR = " "; private static final String SMITHY_TEMPLATE_JSON = "smithy-templates.json"; private static final String DEFAULT_REPOSITORY_URL = "https://github.com/smithy-lang/smithy-examples.git"; private static final String DEFAULT_TEMPLATE_NAME = "quickstart-cli"; @@ -114,48 +117,58 @@ private void listTemplates(ObjectNode smithyTemplatesNode, Env env) throws IOExc private String getTemplateList(ObjectNode smithyTemplatesNode, Env env) { int maxTemplateLength = 0; - int maxDocumentationLength = 0; Map templates = new TreeMap<>(); - for (Map.Entry entry : getTemplatesNode(smithyTemplatesNode).getMembers().entrySet()) { String template = entry.getKey().getValue(); String documentation = entry.getValue() - .expectObjectNode() - .expectMember(DOCUMENTATION, String.format( - "Missing expected member `%s` from `%s` object", DOCUMENTATION, template)) - .expectStringNode() - .getValue(); + .expectObjectNode() + .expectMember(DOCUMENTATION, String.format( + "Missing expected member `%s` from `%s` object", DOCUMENTATION, template)) + .expectStringNode() + .getValue(); templates.put(template, documentation); maxTemplateLength = Math.max(maxTemplateLength, template.length()); - maxDocumentationLength = Math.max(maxDocumentationLength, documentation.length()); } + int maxDocLength = LINE_LENGTH - maxTemplateLength - COLUMN_SEPARATOR.length(); - final String space = " "; + ColorBuffer builder = ColorBuffer.of(env.colors(), new StringBuilder()); - ColorBuffer builder = ColorBuffer.of(env.colors(), new StringBuilder()) - .print(pad(NAME.toUpperCase(Locale.US), maxTemplateLength), ColorTheme.LITERAL) - .print(space) - .print(DOCUMENTATION.toUpperCase(Locale.US), ColorTheme.LITERAL) - .println() - .print(pad("", maxTemplateLength).replace(' ', '-'), ColorTheme.MUTED) - .print(space) - .print(pad("", maxDocumentationLength).replace(' ', '-'), ColorTheme.MUTED) + writeTemplateBorder(builder, maxTemplateLength, maxDocLength); + builder.print(pad(NAME.toUpperCase(Locale.US), maxTemplateLength), ColorTheme.NOTE) + .print(COLUMN_SEPARATOR) + .print(DOCUMENTATION.toUpperCase(Locale.US), ColorTheme.NOTE) .println(); + writeTemplateBorder(builder, maxTemplateLength, maxDocLength); + + int offset = maxTemplateLength + COLUMN_SEPARATOR.length(); for (Map.Entry entry : templates.entrySet()) { String template = entry.getKey(); String doc = entry.getValue(); - builder.print(pad(template, maxTemplateLength)) - .print(space) - .print(pad(doc, maxDocumentationLength)) + + builder.print(pad(template, maxTemplateLength), ColorTheme.TEMPLATE_TITLE) + .print(COLUMN_SEPARATOR) + .print(wrapDocumentation(doc, maxDocLength, offset), + ColorTheme.MUTED) .println(); } return builder.toString(); } + private static void writeTemplateBorder(ColorBuffer writer, int maxNameLength, int maxDocLength) { + writer.print(pad("", maxNameLength).replace(" ", "─"), ColorTheme.TEMPLATE_LIST_BORDER) + .print(COLUMN_SEPARATOR) + .print(pad("", maxDocLength).replace(" ", "─"), ColorTheme.TEMPLATE_LIST_BORDER) + .println(); + } + + private static String wrapDocumentation(String doc, int maxLength, int offset) { + return StringUtils.wrap(doc, maxLength, System.lineSeparator() + pad("", offset), false); + } + private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options options, StandardOptions standardOptions, Env env) throws IOException, InterruptedException, URISyntaxException {