For the latest stable version, please use Spring Shell 3.3.3!spring-doc.cn

Component Render

You can implement component rendering in either of two ways: fully programmatically or by using a ANTLR Stringtemplate. Strictly speaking, there is a simple Function renderer interface that takes Context as an input and outputs a list of AttributedString. This lets you choose between templating and code.spring-doc.cn

Templating is a good choice if you do not need to do anything complex or you just want to slightly modify existing component layouts. Rendering through code then gives you flexibility to do whatever you need.spring-doc.cn

The programmatic way to render is to create a Function:spring-doc.cn

class StringInputCustomRenderer implements Function<StringInputContext, List<AttributedString>> {
	@Override
	public List<AttributedString> apply(StringInputContext context) {
		AttributedStringBuilder builder = new AttributedStringBuilder();
		builder.append(context.getName());
		builder.append(" ");
		if (context.getResultValue() != null) {
			builder.append(context.getResultValue());
		}
		else  {
			String input = context.getInput();
			if (StringUtils.hasText(input)) {
				builder.append(input);
			}
			else {
				builder.append("[Default " + context.getDefaultValue() + "]");
			}
		}
		return Arrays.asList(builder.toAttributedString());
	}
}

Then you can hook it to a component:spring-doc.cn

@ShellMethod(key = "component stringcustom", value = "String input", group = "Components")
public String stringInputCustom(boolean mask) {
	StringInput component = new StringInput(getTerminal(), "Enter value", "myvalue",
			new StringInputCustomRenderer());
	component.setResourceLoader(getResourceLoader());
	component.setTemplateExecutor(getTemplateExecutor());
	if (mask) {
		component.setMaskCharacter('*');
	}
	StringInputContext context = component.run(StringInputContext.empty());
	return "Got value " + context.getResultValue();
}

Components have their own context but usually share some functionality from a parent component types. The following tables show those context variables:spring-doc.cn

Table 1. TextComponentContext Template Variables
Key Description

resultValuespring-doc.cn

The value after a component renders its result.spring-doc.cn

namespring-doc.cn

The name of a component — that is, its title.spring-doc.cn

messagespring-doc.cn

The possible message set for a component.spring-doc.cn

messageLevelspring-doc.cn

The level of a message — one of INFO, WARN, or ERROR.spring-doc.cn

hasMessageLevelInfospring-doc.cn

Return true if level is INFO. Otherwise, false.spring-doc.cn

hasMessageLevelWarnspring-doc.cn

Return true if level is WARN. Otherwise, false.spring-doc.cn

hasMessageLevelErrorspring-doc.cn

Return true if level is ERROR. Otherwise, false.spring-doc.cn

inputspring-doc.cn

The raw user input.spring-doc.cn

Table 2. SelectorComponentContext Template Variables
Key Description

namespring-doc.cn

The name of a component — that is, its title.spring-doc.cn

inputspring-doc.cn

The raw user input — mostly used for filtering.spring-doc.cn

itemStatesspring-doc.cn

The full list of item states.spring-doc.cn

itemStateViewspring-doc.cn

The visible list of item states.spring-doc.cn

isResultspring-doc.cn

Return true if the context is in a result mode.spring-doc.cn

cursorRowspring-doc.cn

The current cursor row in a selector.spring-doc.cn

Table 3. ComponentContext Template Variables
Key Description

terminalWidthspring-doc.cn

The width of terminal, type is Integer and defaults to NULL if not set.spring-doc.cn