/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.geronimo.gshell.wisdom.shell;

import org.apache.geronimo.gshell.ansi.AnsiRenderer;
import org.apache.geronimo.gshell.application.Application;
import org.apache.geronimo.gshell.command.Variables;
import org.apache.geronimo.gshell.console.Console;
import org.apache.geronimo.gshell.interpolation.VariablesValueSource;
import org.codehaus.plexus.interpolation.InterpolationException;
import org.codehaus.plexus.interpolation.Interpolator;
import org.codehaus.plexus.interpolation.PrefixedObjectValueSource;
import org.codehaus.plexus.interpolation.StringSearchInterpolator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * {@link Console.Prompter} component.
 *
 * @version $Rev$ $Date$
 */
public class ConsolePrompterImpl
    implements Console.Prompter
{
    private final Logger log = LoggerFactory.getLogger(getClass());

    private final Application application;

    private final Interpolator interp = new StringSearchInterpolator("%{", "}");

    private final VariablesValueSource variablesValueSource = new VariablesValueSource();

    private final AnsiRenderer renderer = new AnsiRenderer();

    public ConsolePrompterImpl(final Application application) {
        assert application != null;
        this.application = application;
    }

    // @PostConstruct
    public void init() {
        assert application != null;
        interp.addValueSource(new PrefixedObjectValueSource("application", application));
        interp.addValueSource(new PrefixedObjectValueSource("branding", application.getModel().getBranding()));
        interp.addValueSource(variablesValueSource);
    }

    public String prompt() {
        String prompt = null;

        assert application != null;
        Variables vars = application.getVariables();
        String pattern = (String) vars.get("gshell.prompt");

        if (pattern != null) {
            assert variablesValueSource != null;
            variablesValueSource.setVariables(vars);

            try {
                assert interp != null;
                prompt = interp.interpolate(pattern);
            }
            catch (InterpolationException e) {
                log.error("Failed to render prompt pattern: " + pattern, e);
            }
        }

        // Use a default prompt if we don't have anything here
        if (prompt == null) {
            prompt = defaultPrompt();
        }

        // Encode ANSI muck if it looks like there are codes encoded
        if (AnsiRenderer.test(prompt)) {
            prompt = renderer.render(prompt);
        }

        return prompt;
    }

    private String defaultPrompt() {
        assert application != null;
        return application.getModel().getBranding().getName() + "> ";
    }
}
@@ -1129,32 +1129,92 @@
 lesValueSource;%0A
+import org.apache.geronimo.gshell.shell.ShellContextHolder;%0A
 import org.codeh
@@ -1725,52 +1725,8 @@
 );%0A%0A
-    private final Application application;%0A%0A
     
@@ -1953,24 +1953,65 @@
 enderer();%0A%0A
+    private final String defaultPrompt;%0A%0A
     public C
@@ -2110,129 +2110,8 @@
     
-this.application = application;%0A    %7D%0A%0A    // @PostConstruct%0A    public void init() %7B%0A        assert application != null;
 %0A   
@@ -2362,27 +2362,102 @@
 ource);%0A
+%0A
     
-%7D%0A%0A
     
+defaultPrompt = application.getModel().getBranding().getPrompt();%0A    %7D%0A%0A    
 public S
@@ -2499,53 +2499,16 @@
 mpt 
-= null;%0A%0A        assert application !
 = null;%0A
     
@@ -2495,32 +2495,33 @@
  prompt = null;%0A
+%0A
         Variable
@@ -2529,27 +2529,40 @@
  vars = 
-application
+ShellContextHolder.get()
 .getVari
@@ -2598,17 +2598,8 @@
 rn =
- (String)
  var
@@ -2619,16 +2619,30 @@
 .prompt%22
+, String.class
 );%0A%0A    
@@ -3168,26 +3168,24 @@
 efaultPrompt
-()
 ;%0A        %7D%0A
@@ -3384,156 +3384,6 @@
    %7D
-%0A%0A    private String defaultPrompt() %7B%0A        assert application != null;%0A        return application.getModel().getBranding().getName() + %22%3E %22;%0A    %7D
 %0A%7D

Use ShellContextHolder to get variables