Add PromptProvider extension point

This commit is contained in:
Eric Bottard
2017-08-08 16:07:12 +02:00
parent 75911b5fed
commit c3c7f1fccd
6 changed files with 43 additions and 18 deletions

View File

@@ -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

View File

@@ -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 = { '\'', '"' };

View File

@@ -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()) {

View File

@@ -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();
}

View File

@@ -1,3 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.shell.SpringShellAutoConfiguration,\
org.springframework.shell.jline.JLineShell
org.springframework.shell.jline.JLineShellAutoConfiguration

View File

@@ -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));
}
}