Skip to content

Commit 8fbcef0

Browse files
hpmellemaSteven Yuan
authored andcommitted
Update appearance of smithy init list output (smithy-lang#1901)
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.
1 parent da67404 commit 8fbcef0

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

smithy-cli/src/it/java/software/amazon/smithy/cli/InitCommandTest.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,17 @@ public void unexpectedTemplate() {
121121
.append("Invalid template `blabla`. `Smithy-Examples` provides the following templates:")
122122
.append(System.lineSeparator())
123123
.append(System.lineSeparator())
124-
.append("NAME DOCUMENTATION")
124+
.append("───────────────────── ─────────────────────────────────────────────────────────────────────────────")
125125
.append(System.lineSeparator())
126-
.append("--------------------- -----------------------------------------------------")
126+
.append("NAME DOCUMENTATION")
127127
.append(System.lineSeparator())
128-
.append("included-file-json Smithy Quickstart example with json file included. ")
128+
.append("───────────────────── ─────────────────────────────────────────────────────────────────────────────")
129129
.append(System.lineSeparator())
130-
.append("included-files-gradle Smithy Quickstart example with gradle files included.")
130+
.append("included-file-json Smithy Quickstart example with json file included.")
131131
.append(System.lineSeparator())
132-
.append("quickstart-cli Smithy Quickstart example weather service. ")
132+
.append("included-files-gradle Smithy Quickstart example with gradle files included.")
133+
.append(System.lineSeparator())
134+
.append("quickstart-cli Smithy Quickstart example weather service.")
133135
.append(System.lineSeparator())
134136
.toString();
135137

@@ -191,17 +193,19 @@ public void withListArg() {
191193
"init", "--list", "--url", templatesDir.toString()));
192194

193195
String expectedOutput = new StringBuilder()
194-
.append("NAME DOCUMENTATION")
195-
.append(System.lineSeparator())
196-
.append("--------------------- -----------------------------------------------------")
197-
.append(System.lineSeparator())
198-
.append("included-file-json Smithy Quickstart example with json file included. ")
199-
.append(System.lineSeparator())
200-
.append("included-files-gradle Smithy Quickstart example with gradle files included.")
201-
.append(System.lineSeparator())
202-
.append("quickstart-cli Smithy Quickstart example weather service. ")
203-
.append(System.lineSeparator())
204-
.toString();
196+
.append("───────────────────── ─────────────────────────────────────────────────────────────────────────────")
197+
.append(System.lineSeparator())
198+
.append("NAME DOCUMENTATION")
199+
.append(System.lineSeparator())
200+
.append("───────────────────── ─────────────────────────────────────────────────────────────────────────────")
201+
.append(System.lineSeparator())
202+
.append("included-file-json Smithy Quickstart example with json file included.")
203+
.append(System.lineSeparator())
204+
.append("included-files-gradle Smithy Quickstart example with gradle files included.")
205+
.append(System.lineSeparator())
206+
.append("quickstart-cli Smithy Quickstart example weather service.")
207+
.append(System.lineSeparator())
208+
.toString();
205209

206210
assertThat(result.getOutput(), containsString(expectedOutput));
207211
assertThat(result.getExitCode(), is(0));

smithy-cli/src/main/java/software/amazon/smithy/cli/ColorTheme.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@ public final class ColorTheme {
4949

5050
public static final Style HINT_TITLE = Style.of(Style.BRIGHT_GREEN);
5151

52+
public static final Style TEMPLATE_TITLE = Style.of(Style.BOLD, Style.BRIGHT_WHITE);
53+
public static final Style TEMPLATE_LIST_BORDER = Style.of(Style.BRIGHT_WHITE);
54+
5255
private ColorTheme() {}
5356
}

smithy-cli/src/main/java/software/amazon/smithy/cli/commands/InitCommand.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@
4141
import software.amazon.smithy.model.node.StringNode;
4242
import software.amazon.smithy.utils.IoUtils;
4343
import software.amazon.smithy.utils.ListUtils;
44+
import software.amazon.smithy.utils.StringUtils;
4445

4546
final class InitCommand implements Command {
47+
private static final int LINE_LENGTH = 100;
48+
private static final String COLUMN_SEPARATOR = " ";
4649
private static final String SMITHY_TEMPLATE_JSON = "smithy-templates.json";
4750
private static final String DEFAULT_REPOSITORY_URL = "https://github.com/smithy-lang/smithy-examples.git";
4851
private static final String DEFAULT_TEMPLATE_NAME = "quickstart-cli";
@@ -114,48 +117,58 @@ private void listTemplates(ObjectNode smithyTemplatesNode, Env env) throws IOExc
114117

115118
private String getTemplateList(ObjectNode smithyTemplatesNode, Env env) {
116119
int maxTemplateLength = 0;
117-
int maxDocumentationLength = 0;
118120
Map<String, String> templates = new TreeMap<>();
119-
120121
for (Map.Entry<StringNode, Node> entry : getTemplatesNode(smithyTemplatesNode).getMembers().entrySet()) {
121122
String template = entry.getKey().getValue();
122123
String documentation = entry.getValue()
123-
.expectObjectNode()
124-
.expectMember(DOCUMENTATION, String.format(
125-
"Missing expected member `%s` from `%s` object", DOCUMENTATION, template))
126-
.expectStringNode()
127-
.getValue();
124+
.expectObjectNode()
125+
.expectMember(DOCUMENTATION, String.format(
126+
"Missing expected member `%s` from `%s` object", DOCUMENTATION, template))
127+
.expectStringNode()
128+
.getValue();
128129

129130
templates.put(template, documentation);
130131

131132
maxTemplateLength = Math.max(maxTemplateLength, template.length());
132-
maxDocumentationLength = Math.max(maxDocumentationLength, documentation.length());
133133
}
134+
int maxDocLength = LINE_LENGTH - maxTemplateLength - COLUMN_SEPARATOR.length();
134135

135-
final String space = " ";
136+
ColorBuffer builder = ColorBuffer.of(env.colors(), new StringBuilder());
136137

137-
ColorBuffer builder = ColorBuffer.of(env.colors(), new StringBuilder())
138-
.print(pad(NAME.toUpperCase(Locale.US), maxTemplateLength), ColorTheme.LITERAL)
139-
.print(space)
140-
.print(DOCUMENTATION.toUpperCase(Locale.US), ColorTheme.LITERAL)
141-
.println()
142-
.print(pad("", maxTemplateLength).replace(' ', '-'), ColorTheme.MUTED)
143-
.print(space)
144-
.print(pad("", maxDocumentationLength).replace(' ', '-'), ColorTheme.MUTED)
138+
writeTemplateBorder(builder, maxTemplateLength, maxDocLength);
139+
builder.print(pad(NAME.toUpperCase(Locale.US), maxTemplateLength), ColorTheme.NOTE)
140+
.print(COLUMN_SEPARATOR)
141+
.print(DOCUMENTATION.toUpperCase(Locale.US), ColorTheme.NOTE)
145142
.println();
143+
writeTemplateBorder(builder, maxTemplateLength, maxDocLength);
144+
145+
int offset = maxTemplateLength + COLUMN_SEPARATOR.length();
146146

147147
for (Map.Entry<String, String> entry : templates.entrySet()) {
148148
String template = entry.getKey();
149149
String doc = entry.getValue();
150-
builder.print(pad(template, maxTemplateLength))
151-
.print(space)
152-
.print(pad(doc, maxDocumentationLength))
150+
151+
builder.print(pad(template, maxTemplateLength), ColorTheme.TEMPLATE_TITLE)
152+
.print(COLUMN_SEPARATOR)
153+
.print(wrapDocumentation(doc, maxDocLength, offset),
154+
ColorTheme.MUTED)
153155
.println();
154156
}
155157

156158
return builder.toString();
157159
}
158160

161+
private static void writeTemplateBorder(ColorBuffer writer, int maxNameLength, int maxDocLength) {
162+
writer.print(pad("", maxNameLength).replace(" ", "─"), ColorTheme.TEMPLATE_LIST_BORDER)
163+
.print(COLUMN_SEPARATOR)
164+
.print(pad("", maxDocLength).replace(" ", "─"), ColorTheme.TEMPLATE_LIST_BORDER)
165+
.println();
166+
}
167+
168+
private static String wrapDocumentation(String doc, int maxLength, int offset) {
169+
return StringUtils.wrap(doc, maxLength, System.lineSeparator() + pad("", offset), false);
170+
}
171+
159172
private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options options,
160173
StandardOptions standardOptions, Env env)
161174
throws IOException, InterruptedException, URISyntaxException {

0 commit comments

Comments
 (0)