Skip to content

Commit

Permalink
Update appearance of smithy init list output (smithy-lang#1901)
Browse files Browse the repository at this point in the history
Updates the appearance of the smithy init list output. Updates both the style of the output and also adds line wrapping of long list descriptions.
  • Loading branch information
hpmellema authored and syall committed Aug 11, 2023
1 parent da67404 commit 8fbcef0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<String, String> templates = new TreeMap<>();

for (Map.Entry<StringNode, Node> 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<String, String> 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 {
Expand Down

0 comments on commit 8fbcef0

Please sign in to comment.