Skip to content

Commit 53221cf

Browse files
committed
Refine contribution #1241
- Add test - Update documentation
1 parent 002e729 commit 53221cf

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025-present 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.core.autoconfigure;
17+
18+
import org.jline.utils.AttributedString;
19+
import org.jline.utils.AttributedStyle;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.shell.core.command.annotation.Command;
28+
import org.springframework.shell.jline.JLineInputProvider;
29+
import org.springframework.shell.jline.PromptProvider;
30+
import org.springframework.test.util.ReflectionTestUtils;
31+
32+
public class CustomPromptProviderAutoConfigurationTests {
33+
34+
@Test
35+
void testCustomPromptProviderAutoConfiguration() {
36+
// given
37+
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
38+
.withUserConfiguration(SpringShellApplication.class)
39+
.withConfiguration(AutoConfigurations.of(SpringShellAutoConfiguration.class))
40+
.withPropertyValues("spring.shell.interactive.enabled=true");
41+
42+
// when
43+
contextRunner.run(context -> {
44+
// then
45+
assert (context.getBeansOfType(JLineInputProvider.class).size() == 1);
46+
JLineInputProvider jLineInputProvider = context.getBean(JLineInputProvider.class);
47+
PromptProvider promptProvider = (PromptProvider) ReflectionTestUtils.getField(jLineInputProvider,
48+
"promptProvider");
49+
AttributedString prompt = promptProvider.getPrompt();
50+
Assertions.assertEquals("myprompt:>", prompt.toString());
51+
});
52+
53+
}
54+
55+
@SpringBootApplication
56+
static class SpringShellApplication {
57+
58+
@Command
59+
public void hi() {
60+
System.out.println("Hello world!");
61+
}
62+
63+
@Bean
64+
public PromptProvider myPromptProvider() {
65+
return () -> new AttributedString("myprompt:>", AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW));
66+
}
67+
68+
}
69+
70+
}

spring-shell-docs/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
** xref:customization/styling.adoc[]
5656
** xref:customization/logging.adoc[]
5757
** xref:customization/contextclose.adoc[]
58+
** xref:customization/custom-prompt.adoc[]
5859
* xref:execution.adoc[]
5960
* xref:testing.adoc[]
6061
* Appendices
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[[custom-prompt]]
2+
= Custom Prompt
3+
4+
Spring Shell provides various ways to customize the shell prompt and output appearance.
5+
6+
When using the JLine-based shell with Spring Boot, you can customize the shell prompt by implementing the `PromptProvider` interface. This allows you to define your own prompt format.
7+
8+
Here is an example of how to create a custom prompt in a Spring Boot application:
9+
10+
[source,java]
11+
----
12+
@SpringBootApplication
13+
static class SpringShellApplication {
14+
15+
@Command
16+
public void hi() {
17+
System.out.println("Hello world!");
18+
}
19+
20+
@Bean
21+
public PromptProvider myPromptProvider() {
22+
return () -> new AttributedString("myprompt:>", AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW));
23+
}
24+
25+
}
26+
----
27+
28+
Adding the above `PromptProvider` bean will change the shell prompt to "myprompt:>" displayed in yellow color.

0 commit comments

Comments
 (0)