|
41 | 41 | import software.amazon.smithy.model.node.StringNode; |
42 | 42 | import software.amazon.smithy.utils.IoUtils; |
43 | 43 | import software.amazon.smithy.utils.ListUtils; |
| 44 | +import software.amazon.smithy.utils.StringUtils; |
44 | 45 |
|
45 | 46 | final class InitCommand implements Command { |
| 47 | + private static final int LINE_LENGTH = 100; |
| 48 | + private static final String COLUMN_SEPARATOR = " "; |
46 | 49 | private static final String SMITHY_TEMPLATE_JSON = "smithy-templates.json"; |
47 | 50 | private static final String DEFAULT_REPOSITORY_URL = "https://github.com/smithy-lang/smithy-examples.git"; |
48 | 51 | private static final String DEFAULT_TEMPLATE_NAME = "quickstart-cli"; |
@@ -114,48 +117,58 @@ private void listTemplates(ObjectNode smithyTemplatesNode, Env env) throws IOExc |
114 | 117 |
|
115 | 118 | private String getTemplateList(ObjectNode smithyTemplatesNode, Env env) { |
116 | 119 | int maxTemplateLength = 0; |
117 | | - int maxDocumentationLength = 0; |
118 | 120 | Map<String, String> templates = new TreeMap<>(); |
119 | | - |
120 | 121 | for (Map.Entry<StringNode, Node> entry : getTemplatesNode(smithyTemplatesNode).getMembers().entrySet()) { |
121 | 122 | String template = entry.getKey().getValue(); |
122 | 123 | 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(); |
128 | 129 |
|
129 | 130 | templates.put(template, documentation); |
130 | 131 |
|
131 | 132 | maxTemplateLength = Math.max(maxTemplateLength, template.length()); |
132 | | - maxDocumentationLength = Math.max(maxDocumentationLength, documentation.length()); |
133 | 133 | } |
| 134 | + int maxDocLength = LINE_LENGTH - maxTemplateLength - COLUMN_SEPARATOR.length(); |
134 | 135 |
|
135 | | - final String space = " "; |
| 136 | + ColorBuffer builder = ColorBuffer.of(env.colors(), new StringBuilder()); |
136 | 137 |
|
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) |
145 | 142 | .println(); |
| 143 | + writeTemplateBorder(builder, maxTemplateLength, maxDocLength); |
| 144 | + |
| 145 | + int offset = maxTemplateLength + COLUMN_SEPARATOR.length(); |
146 | 146 |
|
147 | 147 | for (Map.Entry<String, String> entry : templates.entrySet()) { |
148 | 148 | String template = entry.getKey(); |
149 | 149 | 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) |
153 | 155 | .println(); |
154 | 156 | } |
155 | 157 |
|
156 | 158 | return builder.toString(); |
157 | 159 | } |
158 | 160 |
|
| 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 | + |
159 | 172 | private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options options, |
160 | 173 | StandardOptions standardOptions, Env env) |
161 | 174 | throws IOException, InterruptedException, URISyntaxException { |
|
0 commit comments