Skip to content

Commit 2bfcf99

Browse files
committed
Make Terminal configurable
- Add TerminalCustomizer which can be used to customise TerminalBuilder before instance from it is build. - Fixes #516
1 parent 4521452 commit 2bfcf99

File tree

3 files changed

+92
-3
lines changed

3 files changed

+92
-3
lines changed

spring-shell-autoconfigure/src/main/java/org/springframework/shell/boot/JLineShellAutoConfiguration.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.jline.utils.AttributedStyle;
2626

2727
import org.springframework.beans.factory.BeanCreationException;
28+
import org.springframework.beans.factory.ObjectProvider;
2829
import org.springframework.boot.autoconfigure.AutoConfiguration;
2930
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3031
import org.springframework.context.annotation.Bean;
@@ -41,12 +42,14 @@
4142
public class JLineShellAutoConfiguration {
4243

4344
@Bean(destroyMethod = "close")
44-
public Terminal terminal() {
45+
public Terminal terminal(ObjectProvider<TerminalCustomizer> customizers) {
4546
try {
46-
return TerminalBuilder.builder().build();
47+
TerminalBuilder builder = TerminalBuilder.builder();
48+
customizers.orderedStream().forEach(customizer -> customizer.customize(builder));
49+
return builder.build();
4750
}
4851
catch (IOException e) {
49-
throw new BeanCreationException("Could not create Terminal: " + e.getMessage());
52+
throw new BeanCreationException("Could not create Terminal", e);
5053
}
5154
}
5255

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.boot;
17+
18+
import org.jline.terminal.Terminal;
19+
import org.jline.terminal.TerminalBuilder;
20+
21+
/**
22+
* Callback interface that can be used to customize a {@link Terminal} via its
23+
* {@link TerminalBuilder}.
24+
*
25+
* @author Janne Valkealahti
26+
*/
27+
@FunctionalInterface
28+
public interface TerminalCustomizer {
29+
30+
/**
31+
* Callback to customize a {@link TerminalBuilder} instance.
32+
*
33+
* @param builder the terminal builder
34+
*/
35+
void customize(TerminalBuilder builder);
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.boot;
17+
18+
import org.jline.terminal.Terminal;
19+
import org.jline.terminal.TerminalBuilder;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.mockito.ArgumentMatchers.any;
27+
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.verify;
29+
30+
class JLineShellAutoConfigurationTests {
31+
32+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
33+
.withConfiguration(AutoConfigurations.of(JLineShellAutoConfiguration.class));
34+
35+
@Test
36+
void canCreatePlainTerminal() {
37+
contextRunner.run(context -> {
38+
assertThat(context).hasSingleBean(Terminal.class);
39+
});
40+
}
41+
42+
@Test
43+
void canCustomizeTerminalBuilder() {
44+
TerminalCustomizer customizer = mock(TerminalCustomizer.class);
45+
contextRunner.withBean(TerminalCustomizer.class, () -> customizer)
46+
.run(context -> {
47+
verify(customizer).customize(any(TerminalBuilder.class));
48+
});
49+
}
50+
}

0 commit comments

Comments
 (0)