From c3c7f1fccdac358f0a65b5b32bf9838b987a7f36 Mon Sep 17 00:00:00 2001 From: Eric Bottard Date: Tue, 8 Aug 2017 16:07:12 +0200 Subject: [PATCH] Add PromptProvider extension point --- .../shell/SpringShellAutoConfiguration.java | 3 --- .../shell/jline/ExtendedDefaultParser.java | 2 +- ....java => JLineShellAutoConfiguration.java} | 24 +++++++++++++++---- .../shell/jline/PromptProvider.java | 13 ++++++++++ .../main/resources/META-INF/spring.factories | 2 +- .../shell/samples/SpringShellSample.java | 17 ++++++------- 6 files changed, 43 insertions(+), 18 deletions(-) rename spring-shell-core/src/main/java/org/springframework/shell/jline/{JLineShell.java => JLineShellAutoConfiguration.java} (88%) create mode 100644 spring-shell-core/src/main/java/org/springframework/shell/jline/PromptProvider.java diff --git a/spring-shell-core/src/main/java/org/springframework/shell/SpringShellAutoConfiguration.java b/spring-shell-core/src/main/java/org/springframework/shell/SpringShellAutoConfiguration.java index 6b39e30b..321e4500 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/SpringShellAutoConfiguration.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/SpringShellAutoConfiguration.java @@ -22,10 +22,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; -import org.springframework.shell.jline.JLineShell; import org.springframework.shell.result.ResultHandlerConfig; /** @@ -33,7 +31,6 @@ import org.springframework.shell.result.ResultHandlerConfig; */ @Configuration @ComponentScan(basePackageClasses = ResultHandlerConfig.class) -@Import(JLineShell.class) public class SpringShellAutoConfiguration { @Bean diff --git a/spring-shell-core/src/main/java/org/springframework/shell/jline/ExtendedDefaultParser.java b/spring-shell-core/src/main/java/org/springframework/shell/jline/ExtendedDefaultParser.java index e4f318b2..0f55f97f 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/jline/ExtendedDefaultParser.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/jline/ExtendedDefaultParser.java @@ -35,7 +35,7 @@ import org.springframework.shell.CompletingParsedLine; * @author Original JLine author * @author Eric Bottard */ -public class ExtendedDefaultParser implements Parser { +class ExtendedDefaultParser implements Parser { private char[] quoteChars = { '\'', '"' }; diff --git a/spring-shell-core/src/main/java/org/springframework/shell/jline/JLineShell.java b/spring-shell-core/src/main/java/org/springframework/shell/jline/JLineShellAutoConfiguration.java similarity index 88% rename from spring-shell-core/src/main/java/org/springframework/shell/jline/JLineShell.java rename to spring-shell-core/src/main/java/org/springframework/shell/jline/JLineShellAutoConfiguration.java index 038e9506..6ac56393 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/jline/JLineShell.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/jline/JLineShellAutoConfiguration.java @@ -38,6 +38,7 @@ import org.jline.utils.AttributedStyle; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.shell.CompletingParsedLine; @@ -55,12 +56,15 @@ import org.springframework.shell.Shell; * @author Florent Biville */ @Configuration -public class JLineShell { +class JLineShellAutoConfiguration { @Autowired @Qualifier("main") private ResultHandler resultHandler; + @Autowired + private PromptProvider promptProvider; + @Bean public Terminal terminal() { try { @@ -73,7 +77,13 @@ public class JLineShell { @Bean public Shell shell() { - return new Shell(new JLineInputProvider(lineReader()), resultHandler); + return new Shell(new JLineInputProvider(lineReader(), promptProvider), resultHandler); + } + + @Bean + @ConditionalOnMissingBean(PromptProvider.class) + public PromptProvider promptProvider() { + return () -> new AttributedString("shell:>", AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW)); } @Bean @@ -97,7 +107,7 @@ public class JLineShell { LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder() .terminal(terminal()) - .appName("Foo") + .appName("Spring Shell") .completer(completer()) .highlighter(new Highlighter() { @@ -173,14 +183,18 @@ public class JLineShell { private final LineReader lineReader; - public JLineInputProvider(LineReader lineReader) { + private final PromptProvider promptProvider; + + public JLineInputProvider(LineReader lineReader, PromptProvider promptProvider) { this.lineReader = lineReader; + this.promptProvider = promptProvider; } @Override public Input readInput() { try { - lineReader.readLine(new AttributedString("shell:>", AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW)).toAnsi(lineReader.getTerminal())); + AttributedString prompt = promptProvider.getPrompt(); + lineReader.readLine(prompt.toAnsi(lineReader.getTerminal())); } catch (UserInterruptException e) { if (e.getPartialLine().isEmpty()) { diff --git a/spring-shell-core/src/main/java/org/springframework/shell/jline/PromptProvider.java b/spring-shell-core/src/main/java/org/springframework/shell/jline/PromptProvider.java new file mode 100644 index 00000000..f1d8ecfa --- /dev/null +++ b/spring-shell-core/src/main/java/org/springframework/shell/jline/PromptProvider.java @@ -0,0 +1,13 @@ +package org.springframework.shell.jline; + +import org.jline.utils.AttributedString; + +/** + * Called at each REPL cycle to decide what the prompt should be. + * + * @author Eric Bottard + */ +public interface PromptProvider { + + AttributedString getPrompt(); +} diff --git a/spring-shell-core/src/main/resources/META-INF/spring.factories b/spring-shell-core/src/main/resources/META-INF/spring.factories index df56751b..066c65bc 100644 --- a/spring-shell-core/src/main/resources/META-INF/spring.factories +++ b/spring-shell-core/src/main/resources/META-INF/spring.factories @@ -1,3 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.shell.SpringShellAutoConfiguration,\ -org.springframework.shell.jline.JLineShell +org.springframework.shell.jline.JLineShellAutoConfiguration diff --git a/spring-shell-samples/src/main/java/org/springframework/shell/samples/SpringShellSample.java b/spring-shell-samples/src/main/java/org/springframework/shell/samples/SpringShellSample.java index f104dc81..4ae09724 100644 --- a/spring-shell-samples/src/main/java/org/springframework/shell/samples/SpringShellSample.java +++ b/spring-shell-samples/src/main/java/org/springframework/shell/samples/SpringShellSample.java @@ -16,18 +16,14 @@ package org.springframework.shell.samples; +import org.jline.utils.AttributedString; +import org.jline.utils.AttributedStyle; + import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.convert.ConversionService; -import org.springframework.core.convert.support.DefaultConversionService; -import org.springframework.shell.Shell; -import org.springframework.shell.jline.JLineShell; -import org.springframework.shell.result.ResultHandlerConfig; +import org.springframework.shell.jline.PromptProvider; /** * Main entry point for the application. @@ -42,4 +38,9 @@ public class SpringShellSample { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(SpringShellSample.class, args); } + + @Bean + public PromptProvider myPromptProvider() { + return () -> new AttributedString("my-shell:>", AttributedStyle.DEFAULT.foreground(AttributedStyle.YELLOW)); + } }