Fix CLI class tangle

This commit is contained in:
Phillip Webb 2014-02-03 21:09:20 -08:00
parent 1061d582dc
commit 0f083c2f9d
3 changed files with 64 additions and 29 deletions

View File

@ -26,23 +26,23 @@ import org.springframework.boot.cli.command.Command;
*/ */
public class PromptCommand extends AbstractCommand { public class PromptCommand extends AbstractCommand {
private final Shell shell; private final ShellPrompts prompts;
public PromptCommand(Shell shell) { public PromptCommand(ShellPrompts shellPrompts) {
super("prompt", "Change the prompt used with the current 'shell' command. " super("prompt", "Change the prompt used with the current 'shell' command. "
+ "Execute with no arguments to return to the previous value."); + "Execute with no arguments to return to the previous value.");
this.shell = shell; this.prompts = shellPrompts;
} }
@Override @Override
public void run(String... strings) throws Exception { public void run(String... strings) throws Exception {
if (strings.length > 0) { if (strings.length > 0) {
for (String string : strings) { for (String string : strings) {
this.shell.pushPrompt(string + " "); this.prompts.pushPrompt(string + " ");
} }
} }
else { else {
this.shell.popPrompt(); this.prompts.popPrompt();
} }
} }

View File

@ -25,7 +25,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.ServiceLoader; import java.util.ServiceLoader;
import java.util.Set; import java.util.Set;
import java.util.Stack;
import jline.console.ConsoleReader; import jline.console.ConsoleReader;
import jline.console.completer.CandidateListCompletionHandler; import jline.console.completer.CandidateListCompletionHandler;
@ -61,15 +60,13 @@ public class Shell {
private static final Signal SIG_INT = new Signal("INT"); private static final Signal SIG_INT = new Signal("INT");
private static final String DEFAULT_PROMPT = "$ ";
private final ShellCommandRunner commandRunner; private final ShellCommandRunner commandRunner;
private final ConsoleReader consoleReader; private final ConsoleReader consoleReader;
private final EscapeAwareWhiteSpaceArgumentDelimiter argumentDelimiter = new EscapeAwareWhiteSpaceArgumentDelimiter(); private final EscapeAwareWhiteSpaceArgumentDelimiter argumentDelimiter = new EscapeAwareWhiteSpaceArgumentDelimiter();
private final Stack<String> prompts = new Stack<String>(); private final ShellPrompts prompts = new ShellPrompts();
/** /**
* Create a new {@link Shell} instance. * Create a new {@link Shell} instance.
@ -101,7 +98,7 @@ public class Shell {
commands.add(convertToForkCommand(command)); commands.add(convertToForkCommand(command));
} }
} }
commands.add(new PromptCommand(this)); commands.add(new PromptCommand(this.prompts));
commands.add(new ClearCommand(this.consoleReader)); commands.add(new ClearCommand(this.consoleReader));
commands.add(new ExitCommand()); commands.add(new ExitCommand());
return commands; return commands;
@ -134,23 +131,6 @@ public class Shell {
}); });
} }
/**
* Push a new prompt to be used by the shell.
* @param prompt the prompt
* @see #popPrompt()
*/
public void pushPrompt(String prompt) {
this.prompts.push(prompt);
}
/**
* Pop a previously pushed prompt, returning to the previous value.
* @see #pushPrompt(String)
*/
public void popPrompt() {
this.prompts.pop();
}
/** /**
* Run the shell until the user exists. * Run the shell until the user exists.
* @throws Exception on error * @throws Exception on error
@ -168,7 +148,7 @@ public class Shell {
} }
private void printBanner() { private void printBanner() {
String version = ShellCommand.class.getPackage().getImplementationVersion(); String version = getClass().getPackage().getImplementationVersion();
version = (version == null ? "" : " (v" + version + ")"); version = (version == null ? "" : " (v" + version + ")");
System.out.println(ansi("Spring Boot", Code.BOLD).append(version, Code.FAINT)); System.out.println(ansi("Spring Boot", Code.BOLD).append(version, Code.FAINT));
System.out.println(ansi("Hit TAB to complete. Type 'help' and hit " System.out.println(ansi("Hit TAB to complete. Type 'help' and hit "
@ -190,7 +170,7 @@ public class Shell {
} }
private String getPrompt() { private String getPrompt() {
String prompt = this.prompts.isEmpty() ? DEFAULT_PROMPT : this.prompts.peek(); String prompt = this.prompts.getPrompt();
return ansi(prompt, Code.FG_BLUE).toString(); return ansi(prompt, Code.FG_BLUE).toString();
} }

View File

@ -0,0 +1,55 @@
/*
* Copyright 2012-2014 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.boot.cli.command.shell;
import java.util.Stack;
/**
* Abstraction to manage a stack of prompts.
*
* @author Phillip Webb
*/
public class ShellPrompts {
private static final String DEFAULT_PROMPT = "$ ";
private final Stack<String> prompts = new Stack<String>();
/**
* Push a new prompt to be used by the shell.
* @param prompt the prompt
* @see #popPrompt()
*/
public void pushPrompt(String prompt) {
this.prompts.push(prompt);
}
/**
* Pop a previously pushed prompt, returning to the previous value.
* @see #pushPrompt(String)
*/
public void popPrompt() {
this.prompts.pop();
}
/**
* Returns the current prompt.
*/
public String getPrompt() {
return this.prompts.isEmpty() ? DEFAULT_PROMPT : this.prompts.peek();
}
}