001/*
002 * Copyright 2012-2018 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.ArrayDeque;
020import java.util.Deque;
021
022/**
023 * Abstraction to manage a stack of prompts.
024 *
025 * @author Phillip Webb
026 */
027public class ShellPrompts {
028
029        private static final String DEFAULT_PROMPT = "$ ";
030
031        private final Deque<String> prompts = new ArrayDeque<>();
032
033        /**
034         * Push a new prompt to be used by the shell.
035         * @param prompt the prompt
036         * @see #popPrompt()
037         */
038        public void pushPrompt(String prompt) {
039                this.prompts.push(prompt);
040        }
041
042        /**
043         * Pop a previously pushed prompt, returning to the previous value.
044         * @see #pushPrompt(String)
045         */
046        public void popPrompt() {
047                if (!this.prompts.isEmpty()) {
048                        this.prompts.pop();
049                }
050        }
051
052        /**
053         * Returns the current prompt.
054         * @return the current prompt
055         */
056        public String getPrompt() {
057                return this.prompts.isEmpty() ? DEFAULT_PROMPT : this.prompts.peek();
058        }
059
060}