|
16 | 16 | package org.springframework.shell.samples.standard; |
17 | 17 |
|
18 | 18 | import java.util.ArrayList; |
| 19 | +import java.util.Arrays; |
19 | 20 | import java.util.List; |
20 | 21 | import java.util.Optional; |
| 22 | +import java.util.function.Function; |
21 | 23 | import java.util.stream.Collectors; |
22 | 24 |
|
| 25 | +import org.jline.utils.AttributedString; |
| 26 | +import org.jline.utils.AttributedStringBuilder; |
| 27 | + |
23 | 28 | import org.springframework.shell.component.ConfirmationInput; |
24 | 29 | import org.springframework.shell.component.ConfirmationInput.ConfirmationInputContext; |
25 | 30 | import org.springframework.shell.component.MultiItemSelector; |
|
34 | 39 | import org.springframework.shell.standard.AbstractShellComponent; |
35 | 40 | import org.springframework.shell.standard.ShellComponent; |
36 | 41 | import org.springframework.shell.standard.ShellMethod; |
| 42 | +import org.springframework.util.StringUtils; |
37 | 43 |
|
38 | 44 | @ShellComponent |
39 | 45 | public class ComponentCommands extends AbstractShellComponent { |
@@ -100,4 +106,42 @@ public String multiSelector() { |
100 | 106 | .collect(Collectors.joining(",")); |
101 | 107 | return "Got value " + result; |
102 | 108 | } |
| 109 | + |
| 110 | + @ShellMethod(key = "component stringcustom", value = "String input", group = "Components") |
| 111 | + public String stringInputCustom(boolean mask) { |
| 112 | + StringInput component = new StringInput(getTerminal(), "Enter value", "myvalue", |
| 113 | + new StringInputCustomRenderer()); |
| 114 | + component.setResourceLoader(getResourceLoader()); |
| 115 | + component.setTemplateExecutor(getTemplateExecutor()); |
| 116 | + if (mask) { |
| 117 | + component.setMaskCharater('*'); |
| 118 | + } |
| 119 | + StringInputContext context = component.run(StringInputContext.empty()); |
| 120 | + return "Got value " + context.getResultValue(); |
| 121 | + } |
| 122 | + |
| 123 | + private static class StringInputCustomRenderer implements Function<StringInputContext, List<AttributedString>> { |
| 124 | + |
| 125 | + @Override |
| 126 | + public List<AttributedString> apply(StringInputContext context) { |
| 127 | + AttributedStringBuilder builder = new AttributedStringBuilder(); |
| 128 | + builder.append(context.getName()); |
| 129 | + builder.append(" "); |
| 130 | + |
| 131 | + if (context.getResultValue() != null) { |
| 132 | + builder.append(context.getResultValue()); |
| 133 | + } |
| 134 | + else { |
| 135 | + String input = context.getInput(); |
| 136 | + if (StringUtils.hasText(input)) { |
| 137 | + builder.append(input); |
| 138 | + } |
| 139 | + else { |
| 140 | + builder.append("[Default " + context.getDefaultValue() + "]"); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return Arrays.asList(builder.toAttributedString()); |
| 145 | + } |
| 146 | + } |
103 | 147 | } |
0 commit comments