Component Render

您可以使用以下两种方式之一实现组件渲染:完全通过编程方式或使用 ANTLR Stringtemplate。严格来说,有一个简单的 Function 呈现器界面,它将 Context 作为输入,并输出 AttributedString 的列表。这允许您在模板化和代码之间进行选择。

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.

如果您不需要执行任何复杂操作,或者您只想稍微修改现有组件布局,那么模板化是一个不错的选择。然后通过代码渲染为您提供了所需的灵活性。

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.

通过编程方式进行渲染是创建 Function

The programmatic way to render is to create a Function:

Unresolved include directive in modules/ROOT/pages/components/ui/render.adoc - include::../../test/java/org/springframework/shell/docs/UiComponentSnippets.java[]

然后,您可以将其连接到组件:

Then you can hook it to a component:

Unresolved include directive in modules/ROOT/pages/components/ui/render.adoc - include::../../test/java/org/springframework/shell/docs/UiComponentSnippets.java[]

组件有自己的上下文,但通常会从父组件类型共享一些功能。以下表格显示了这些上下文变量:

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

Table 1. TextComponentContext Template Variables
Key Description

resultValue

The value after a component renders its result.

name

The name of a component — that is, its title.

message

The possible message set for a component.

messageLevel

The level of a message — one of INFO, WARN, or ERROR.

hasMessageLevelInfo

Return true if level is INFO. Otherwise, false.

hasMessageLevelWarn

Return true if level is WARN. Otherwise, false.

hasMessageLevelError

Return true if level is ERROR. Otherwise, false.

input

The raw user input.

Table 2. SelectorComponentContext Template Variables
Key Description

name

The name of a component — that is, its title.

input

The raw user input — mostly used for filtering.

itemStates

The full list of item states.

itemStateView

The visible list of item states.

isResult

Return true if the context is in a result mode.

cursorRow

The current cursor row in a selector.

Table 3. ComponentContext Template Variables
Key Description

terminalWidth

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