SHL-12 customize prompt text

This commit is contained in:
Jarred Li
2012-04-12 12:08:36 +08:00
parent 956d72ee28
commit db68586afd
9 changed files with 182 additions and 53 deletions

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.samples.helloworld.commands;
import org.springframework.shell.plugin.PromptProvider;
import org.springframework.stereotype.Component;
/**
* @author Jarred Li
*
*/
@Component
public class MyPromptProvider implements PromptProvider {
/* (non-Javadoc)
* @see org.springframework.core.Ordered#getOrder()
*/
public int getOrder() {
return 1;
}
/* (non-Javadoc)
* @see org.springframework.shell.plugin.PromptProvider#getPromptText()
*/
public String getPromptText() {
return "vHelper>";
}
}

View File

@@ -31,7 +31,6 @@ import org.springframework.util.StopWatch;
public class Bootstrap {
private static Bootstrap bootstrap;
//TODO using JLineShellComponenet to override and get access to "getSimpleParser" method on the shell - look into autowire...and move back to reference Shell interface.
private JLineShellComponent shell;
private ConfigurableApplicationContext ctx;
private static StopWatch sw = new StopWatch("Spring Sehll");

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell;
/**
* @author Jarred Li
*
*/
public interface Constant {
String HISTORY_FILE_NAME = "spring-shell.log";
String COMMAND_LINE_PROMPT = "spring>";
}

View File

@@ -1,19 +0,0 @@
package org.springframework.shell;
import org.springframework.stereotype.Component;
/**
* {@link Service} with hard-coded input data.
*/
@Component
public class ExampleService implements Service {
/**
* Reads next record from input
*/
public String getMessage() {
return "Hello world!";
}
}

View File

@@ -15,7 +15,6 @@ import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Handler;
import java.util.logging.Level;
@@ -29,7 +28,6 @@ import jline.WindowsTerminal;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.roo.shell.AbstractShell;
import org.springframework.roo.shell.CommandMarker;
import org.springframework.roo.shell.ExitShellRequest;
@@ -43,8 +41,8 @@ import org.springframework.roo.support.util.FileCopyUtils;
import org.springframework.roo.support.util.IOUtils;
import org.springframework.roo.support.util.OsUtils;
import org.springframework.roo.support.util.StringUtils;
import org.springframework.shell.commands.HintCommands;
import org.springframework.shell.plugin.HistoryFileProvider;
import org.springframework.shell.plugin.PromptProvider;
/**
@@ -69,10 +67,6 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker,
private static final boolean APPLE_TERMINAL = Boolean.getBoolean("is.apple.terminal");
private static final char ESCAPE = 27;
private static final String BEL = "\007";
//TODO make configurable
private static final String COMMAND_LINE_PROMPT = "spring>";
// Fields
private ConsoleReader reader;
private boolean developmentMode = false;
@@ -203,7 +197,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker,
if (reader.getTerminal().isANSISupported()) {
ANSIBuffer ansi = JLineLogHandler.getANSIBuffer();
if (path == null || "".equals(path)) {
shellPrompt = ansi.yellow(COMMAND_LINE_PROMPT).toString();
shellPrompt = ansi.yellow(getPromptText()).toString();
}
else {
if (overrideStyle) {
@@ -212,7 +206,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker,
else {
ansi.cyan(path);
}
shellPrompt = ansi.yellow(" " + COMMAND_LINE_PROMPT).toString();
shellPrompt = ansi.yellow(" " + getPromptText()).toString();
}
}
else {
@@ -464,21 +458,7 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker,
}
}
private String getHistoryFileName() {
String historyFileName = null;
Map<String, HistoryFileProvider> historyFileProviders = BeanFactoryUtils.beansOfTypeIncludingAncestors(
this.applicatonContext, HistoryFileProvider.class);
int order = Integer.MIN_VALUE;
System.out.println("history file provider count:" + Arrays.toString(historyFileProviders.keySet().toArray()));
for(HistoryFileProvider p : historyFileProviders.values()){
if(p.getOrder() > order){
order = p.getOrder();
historyFileName = p.getHistoryFileName();
}
}
historyFileName = (historyFileName == null)?"spring-shell.log":historyFileName;
return historyFileName;
}
@Override
protected void logCommandToOutput(final String processedLine) {
@@ -541,4 +521,39 @@ public abstract class JLineShell extends AbstractShell implements CommandMarker,
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicatonContext = applicationContext;
}
private String getHistoryFileName() {
String historyFileName = null;
Map<String, HistoryFileProvider> historyFileProviders = getBeansInFactory(HistoryFileProvider.class);
int order = Integer.MIN_VALUE;
for(HistoryFileProvider p : historyFileProviders.values()){
if(p.getOrder() > order){
order = p.getOrder();
historyFileName = p.getHistoryFileName();
}
}
historyFileName = (historyFileName == null)?Constant.HISTORY_FILE_NAME:historyFileName;
return historyFileName;
}
private String getPromptText(){
String promptText = null;
Map<String, PromptProvider> promptProviders = getBeansInFactory(PromptProvider.class);
int order = Integer.MIN_VALUE;
for(PromptProvider p : promptProviders.values()){
if(p.getOrder() > order){
order = p.getOrder();
promptText = p.getPromptText();
}
}
promptText = (promptText == null)?Constant.COMMAND_LINE_PROMPT:promptText;
return promptText;
}
private <T> Map<String, T> getBeansInFactory(Class<T> t){
Map<String, T> result = BeanFactoryUtils.beansOfTypeIncludingAncestors(
this.applicatonContext, t);
return result;
}
}

View File

@@ -1,7 +0,0 @@
package org.springframework.shell;
public interface Service {
String getMessage();
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.plugin;
import org.springframework.core.Ordered;
/**
* Provider for prompt
*
* @author Jarred Li
*
*/
public interface PromptProvider extends Ordered {
String getPromptText();
}

View File

@@ -15,7 +15,7 @@
*/
package org.springframework.shell.plugin.support;
import org.springframework.core.Ordered;
import org.springframework.shell.Constant;
import org.springframework.shell.plugin.HistoryFileProvider;
import org.springframework.stereotype.Component;
@@ -37,7 +37,7 @@ public class DefaultHistoryFileProvider implements HistoryFileProvider{
* @see org.springframework.shell.plugin.HistoryFileProvider#getHistoryFileName()
*/
public String getHistoryFileName() {
return "spring-shell.log";
return Constant.HISTORY_FILE_NAME;
}
}

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2011-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.shell.plugin.support;
import org.springframework.shell.Constant;
import org.springframework.shell.plugin.PromptProvider;
/**
* @author Jarred Li
*
*/
public class DefaultPromptProvider implements PromptProvider{
/* (non-Javadoc)
* @see org.springframework.core.Ordered#getOrder()
*/
public int getOrder() {
return 0;
}
/* (non-Javadoc)
* @see org.springframework.shell.plugin.PromptProvider#getPromptText()
*/
public String getPromptText() {
return Constant.COMMAND_LINE_PROMPT;
}
}