001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.cli.command.shell; 018 019import java.util.Stack; 020 021/** 022 * Abstraction to manage a stack of prompts. 023 * 024 * @author Phillip Webb 025 */ 026public class ShellPrompts { 027 028 private static final String DEFAULT_PROMPT = "$ "; 029 030 private final Stack<String> prompts = new Stack<String>(); 031 032 /** 033 * Push a new prompt to be used by the shell. 034 * @param prompt the prompt 035 * @see #popPrompt() 036 */ 037 public void pushPrompt(String prompt) { 038 this.prompts.push(prompt); 039 } 040 041 /** 042 * Pop a previously pushed prompt, returning to the previous value. 043 * @see #pushPrompt(String) 044 */ 045 public void popPrompt() { 046 if (!this.prompts.isEmpty()) { 047 this.prompts.pop(); 048 } 049 } 050 051 /** 052 * Returns the current prompt. 053 * @return the current prompt 054 */ 055 public String getPrompt() { 056 return this.prompts.isEmpty() ? DEFAULT_PROMPT : this.prompts.peek(); 057 } 058 059}