From 0f083c2f9dfa39f90980ee9ef4a8c9f969e8fde0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 3 Feb 2014 21:09:20 -0800 Subject: [PATCH] Fix CLI class tangle --- .../boot/cli/command/shell/PromptCommand.java | 10 ++-- .../boot/cli/command/shell/Shell.java | 28 ++-------- .../boot/cli/command/shell/ShellPrompts.java | 55 +++++++++++++++++++ 3 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/ShellPrompts.java diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/PromptCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/PromptCommand.java index e6a86054653..4c147bc1b59 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/PromptCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/PromptCommand.java @@ -26,23 +26,23 @@ import org.springframework.boot.cli.command.Command; */ 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. " + "Execute with no arguments to return to the previous value."); - this.shell = shell; + this.prompts = shellPrompts; } @Override public void run(String... strings) throws Exception { if (strings.length > 0) { for (String string : strings) { - this.shell.pushPrompt(string + " "); + this.prompts.pushPrompt(string + " "); } } else { - this.shell.popPrompt(); + this.prompts.popPrompt(); } } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/Shell.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/Shell.java index 90a74a91043..6e4f98f3923 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/Shell.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/Shell.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.Map; import java.util.ServiceLoader; import java.util.Set; -import java.util.Stack; import jline.console.ConsoleReader; import jline.console.completer.CandidateListCompletionHandler; @@ -61,15 +60,13 @@ public class Shell { private static final Signal SIG_INT = new Signal("INT"); - private static final String DEFAULT_PROMPT = "$ "; - private final ShellCommandRunner commandRunner; private final ConsoleReader consoleReader; private final EscapeAwareWhiteSpaceArgumentDelimiter argumentDelimiter = new EscapeAwareWhiteSpaceArgumentDelimiter(); - private final Stack prompts = new Stack(); + private final ShellPrompts prompts = new ShellPrompts(); /** * Create a new {@link Shell} instance. @@ -101,7 +98,7 @@ public class Shell { commands.add(convertToForkCommand(command)); } } - commands.add(new PromptCommand(this)); + commands.add(new PromptCommand(this.prompts)); commands.add(new ClearCommand(this.consoleReader)); commands.add(new ExitCommand()); 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. * @throws Exception on error @@ -168,7 +148,7 @@ public class Shell { } private void printBanner() { - String version = ShellCommand.class.getPackage().getImplementationVersion(); + String version = getClass().getPackage().getImplementationVersion(); version = (version == null ? "" : " (v" + version + ")"); System.out.println(ansi("Spring Boot", Code.BOLD).append(version, Code.FAINT)); System.out.println(ansi("Hit TAB to complete. Type 'help' and hit " @@ -190,7 +170,7 @@ public class Shell { } 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(); } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/ShellPrompts.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/ShellPrompts.java new file mode 100644 index 00000000000..a792801a7f4 --- /dev/null +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/shell/ShellPrompts.java @@ -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 prompts = new Stack(); + + /** + * 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(); + } +}