This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Shell 3.4.0! |
Theming
Current terminal implementations are rich in features and can usually show something else that just plain text. For example, a text can be styled to be bold or have different colors. It’s also common for terminals to be able to show various characters from a unicode table like emoji’s which are usually used to make shell output more pretty.
Spring Shell supports these via it’s theming framework which contains two parts. Firstly, styling can be used to change text type and secondly, figures are used to customize how characters are shown. These two parts are then combined as a theme.
For more detail about theming internals, refer to see Theming.
Default theme is named default but can be changed using the property
spring.shell.theme.name . There is also another built-in theme named dump
that uses no styling for colors and tries to not use any special figures.
|
You can modify existing styles and figures by overriding the default settings:
static class MyStyleSettings extends StyleSettings {
@Override
public String highlight() {
return super.highlight();
}
}
static class MyFigureSettings extends FigureSettings {
@Override
public String error() {
return super.error();
}
}
You can also create a new theme, by creating a ThemeSettings
and provide your own style
and figure implementations:
static class MyThemeSettings extends ThemeSettings {
@Override
public StyleSettings styles() {
return new MyStyleSettings();
}
@Override
public FigureSettings figures() {
return new MyFigureSettings();
}
}
Register a new bean Theme
where you can return your custom ThemeSettings
and a theme name.
@Configuration
static class CustomThemeConfig {
@Bean
Theme myTheme() {
return new Theme() {
@Override
public String getName() {
return "mytheme";
}
@Override
public ThemeSettings getSettings() {
return new MyThemeSettings();
}
};
}
}
You can use ThemeResolver
to resolve styles if you want to create
JLine-styled strings programmatically and figures if you want to
theme characters for being more pretty.
@Autowired
private ThemeResolver resolver;
void resolve() {
String resolvedStyle = resolver.resolveStyleTag(StyleSettings.TAG_TITLE);
// bold,fg:bright-white
AttributedStyle style = resolver.resolveStyle(resolvedStyle);
// jline attributed style from expression above
String resolvedFigure = resolver.resolveFigureTag(FigureSettings.TAG_ERROR);
// character i.e. U+2716 Heavy Multiplication X Emoji, cross
}