Skip to content

Commit 8fc3ff9

Browse files
committed
Update docs
- Some new docs for theming. - Relates #433
1 parent 5eaa5dd commit 8fc3ff9

File tree

4 files changed

+221
-33
lines changed

4 files changed

+221
-33
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[#appendix-tech-intro-theming]
2+
=== Theming
3+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
4+
5+
Styling in a theming is provided by a use of a _AttributedString_ from `JLine`.
6+
Unfortunately styling in `JLine` is mostly undocumented but we try to go through
7+
some of its features here.
8+
9+
In `JLine` a style spec is a string having a special format. Spec can be given
10+
multiple times if separated by a comma. A spec will either define a color for
11+
foreground, background or its mode. Special format `<spec>:=<spec>` allows to
12+
define a default within latter spec if former for some reason is invalid.
13+
14+
If spec contains a colon its former part indicates either foreground or background
15+
and possible values are `foreground`, `fg`, `f`, `background`, `bg`, `b`, `foreground-rgb`,
16+
`fg-rgb`, `f-rgb`, `background-rgb`, `bg-rgb` or `b-rgb`. Without rbg a color value
17+
is name from an allowable colors `black`, `red`, `green`, `yellow`, `blue`, `magenta`,
18+
`cyan` or `white`. Colors have their short formats `k`, `r`, `g`, `y`, `b`, `m`, `c` and `w`
19+
respectively. If color is prefixed with either `!` or `bright-`, bright mode is automatically
20+
applied. Prefixing with `~` will resolve from JLine internal bsd color table.
21+
22+
If rgb format is expected and prefixed with either `x` or `#` a normal
23+
hex format is used.
24+
25+
====
26+
[source, text]
27+
----
28+
fg-red
29+
fg-r
30+
fg-rgb:red
31+
fg-rgb:xff3333
32+
fg-rgb:#ff3333
33+
----
34+
====
35+
36+
If spec contains special names `default`, `bold`, `faint`, `italic`, `underline`, `blink`,
37+
`inverse`, `inverse-neg`, `inverseneg`, `conceal`, `crossed-out`, `crossedout` or `hidden`
38+
a style is changed accordingly with an existing color.
39+
40+
====
41+
[source, text]
42+
----
43+
bold
44+
bold,fg:red
45+
----
46+
====
47+
48+
If spec is a number or numbers separated with semicolon, format is a plain part of an ansi
49+
ascii codes.
50+
51+
====
52+
[source, text]
53+
----
54+
31
55+
31;1
56+
----
57+
====
58+
59+
NOTE: JLine special mapping format which would resolve spec starting with dot can't be
60+
used as we don't yet map those into Spring Shell styling names.

spring-shell-docs/src/main/asciidoc/appendices-techical-intro.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ include::appendices-techical-intro-execution.adoc[]
1313
include::appendices-techical-intro-commandcontext.adoc[]
1414

1515
include::appendices-techical-intro-commandcatalog.adoc[]
16+
17+
include::appendices-techical-intro-theming.adoc[]
Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,68 @@
1-
[[styling]]
2-
==== Styling
1+
[[theming]]
2+
==== Theming
3+
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
34

4-
Version 2.1.x introduced support for centrally handling styling and theming.
5-
You can change the default theme (named `default`)by setting the
6-
`spring.shell.theme.name` property.
5+
Current terminal implementations are rich in features and can usually show
6+
something else that just plain text. For example a text can be styled to be
7+
_bold_ or have different colors. It's also common for terminals to be able
8+
to show various characters from an unicode table like emoji's which are usually
9+
used to make shell output more pretty.
710

8-
To create a new theme, register a new `Theme` bean with custom `ThemeSettings`. This new bean
9-
lets you tweak styles. The following example shows how to do so:
11+
Spring Shell supports these via it's theming framework which contains two parts,
12+
firstly _styling_ can be used to change text type and secondly _figures_ how
13+
some characters are shown. These two are then combined together as a _theme_.
14+
15+
More about _theming_ internals, see <<appendix-tech-intro-theming>>.
16+
17+
NOTE: Default theme is named `default` but can be change using property
18+
`spring.shell.theme.name`. Other built-in theme named `dump` uses
19+
no styling for colors and tries to not use any special figures.
20+
21+
Modify existing style by overriding settings.
22+
23+
====
24+
[source, java, indent=0]
25+
----
26+
include::{snippets}/ThemingSnippets.java[tag=custom-style-class]
27+
----
28+
====
29+
30+
Modify existing figures by overriding settings.
1031

1132
====
12-
[source, java]
33+
[source, java, indent=0]
1334
----
14-
@Configuration
15-
static class CustomThemeConfig {
35+
include::{snippets}/ThemingSnippets.java[tag=custom-figure-class]
36+
----
37+
====
1638

17-
@Bean
18-
public Theme myTheme() {
19-
return new Theme() {
20-
@Override
21-
public String getName() {
22-
return "mytheme";
23-
}
24-
@Override
25-
public ThemeSettings getSettings() {
26-
return new MyThemeSettings();
27-
}
28-
};
29-
}
30-
}
39+
To create a new theme, create a `ThemeSettings` and provide your own _style_
40+
and _figure_ implementations.
3141

32-
static class MyThemeSettings extends ThemeSettings {
33-
}
42+
====
43+
[source, java, indent=0]
44+
----
45+
include::{snippets}/ThemingSnippets.java[tag=custom-theme-class]
3446
----
3547
====
3648

37-
You can use `ThemeResolver` to resolve styles if you want to create
38-
JLine-styled strings programmatically. The following example shows how to do so:
49+
Register a new bean `Theme` where you can return your custom `ThemeSettings`
50+
and a _theme_ name.
3951

4052
====
41-
[source, java]
53+
[source, java, indent=0]
54+
----
55+
include::{snippets}/ThemingSnippets.java[tag=custom-theme-config]
4256
----
43-
@Autowired
44-
private ThemeResolver themeResolver;
57+
====
58+
59+
You can use `ThemeResolver` to resolve _styles_ if you want to create
60+
JLine-styled strings programmatically and _figures_ if you want to
61+
theme characters for being more pretty.
4562

46-
String resolvedStyle = themeResolver.resolveTag(TAG_TITLE);
47-
AttributedStyle style = themeResolver.resolveStyle(resolvedStyle);
63+
====
64+
[source, java, indent=0]
65+
----
66+
include::{snippets}/ThemingSnippets.java[tag=using-theme-resolver]
4867
----
4968
====
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.docs;
17+
18+
import org.jline.utils.AttributedStyle;
19+
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.shell.style.FigureSettings;
24+
import org.springframework.shell.style.StyleSettings;
25+
import org.springframework.shell.style.Theme;
26+
import org.springframework.shell.style.ThemeResolver;
27+
import org.springframework.shell.style.ThemeSettings;
28+
29+
@SuppressWarnings("unused")
30+
public class ThemingSnippets {
31+
32+
// tag::custom-style-class[]
33+
static class MyStyleSettings extends StyleSettings {
34+
35+
@Override
36+
public String highlight() {
37+
return super.highlight();
38+
}
39+
}
40+
// end::custom-style-class[]
41+
42+
// tag::custom-figure-class[]
43+
static class MyFigureSettings extends FigureSettings {
44+
45+
@Override
46+
public String error() {
47+
return super.error();
48+
}
49+
}
50+
// end::custom-figure-class[]
51+
52+
// tag::custom-theme-class[]
53+
static class MyThemeSettings extends ThemeSettings {
54+
55+
@Override
56+
public StyleSettings styles() {
57+
return new MyStyleSettings();
58+
}
59+
60+
@Override
61+
public FigureSettings figures() {
62+
return new MyFigureSettings();
63+
}
64+
}
65+
// end::custom-theme-class[]
66+
67+
// tag::custom-theme-config[]
68+
@Configuration
69+
static class CustomThemeConfig {
70+
71+
@Bean
72+
Theme myTheme() {
73+
return new Theme() {
74+
@Override
75+
public String getName() {
76+
return "mytheme";
77+
}
78+
79+
@Override
80+
public ThemeSettings getSettings() {
81+
return new MyThemeSettings();
82+
}
83+
};
84+
}
85+
}
86+
// end::custom-theme-config[]
87+
88+
class Dump1 {
89+
90+
// tag::using-theme-resolver[]
91+
@Autowired
92+
private ThemeResolver resolver;
93+
94+
void resolve() {
95+
String resolvedStyle = resolver.resolveStyleTag(StyleSettings.TAG_TITLE);
96+
// bold,fg:bright-white
97+
98+
AttributedStyle style = resolver.resolveStyle(resolvedStyle);
99+
// jline attributed style from expression above
100+
101+
String resolvedFigure = resolver.resolveFigureTag(FigureSettings.TAG_ERROR);
102+
// character i.e. U+2716 Heavy Multiplication X Emoji, cross
103+
}
104+
// end::using-theme-resolver[]
105+
}
106+
107+
}

0 commit comments

Comments
 (0)