All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.groovy.tools.shell.util.ANSI Maven / Gradle / Ivy

There is a newer version: 3.0.22
Show newest version
/*
 * Copyright 2003-2007 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.codehaus.groovy.tools.shell.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;

import java.util.HashMap;
import java.util.Map;

import jline.ANSIBuffer.ANSICodes;
import jline.Terminal;

/**
 * Provides support for using ANSI color escape codes.
 *
 * @version $Id: ANSI.java 11949 2008-04-22 09:19:18Z paulk $
 * @author Jason Dillon
 */
public class ANSI
{
    //
    // Detection/Enabled Muck
    //

    /**
     * Tries to detect if the current system supports ANSI.
     */
    private static boolean detect() {
        boolean enabled = Terminal.getTerminal().isANSISupported();

        if (!enabled) {
            String force = System.getProperty(ANSI.class.getName() + ".force", "false");
            enabled = Boolean.valueOf(force).booleanValue();
        }

        return enabled;
    }

    public static boolean isDetected() {
        return detect();
    }

    private static Boolean enabled;

    public static void setEnabled(final boolean flag) {
        enabled = Boolean.valueOf(flag);
    }

    public static boolean isEnabled() {
        if (enabled == null) {
            enabled = Boolean.valueOf(isDetected());
        }
        
        return enabled.booleanValue();
    }

    //
    // Code
    //

    public static class Code
    {
        //
        // NOTE: Some fields duplicated from jline.ANSIBuffer.ANSICodes to change access modifiers
        //
        
        public static final int OFF = 0;
        public static final int BOLD = 1;
        public static final int UNDERSCORE = 4;
        public static final int BLINK = 5;
        public static final int REVERSE = 7;
        public static final int CONCEALED = 8;
        
        public static final int FG_BLACK = 30;
        public static final int FG_RED = 31;
        public static final int FG_GREEN = 32;
        public static final int FG_YELLOW = 33;
        public static final int FG_BLUE = 34;
        public static final int FG_MAGENTA = 35;
        public static final int FG_CYAN = 36;
        public static final int FG_WHITE = 37;

        public static final int BLACK = FG_BLACK;
        public static final int RED = FG_RED;
        public static final int GREEN = FG_GREEN;
        public static final int YELLOW = FG_YELLOW;
        public static final int BLUE = FG_BLUE;
        public static final int MAGENTA = FG_MAGENTA;
        public static final int CYAN = FG_CYAN;
        public static final int WHITE = FG_WHITE;
        
        public static final int BG_BLACK = 40;
        public static final int BG_RED = 41;
        public static final int BG_GREEN = 42;
        public static final int BG_YELLOW = 43;
        public static final int BG_BLUE = 44;
        public static final int BG_MAGENTA = 45;
        public static final int BG_CYAN = 46;
        public static final int BG_WHITE = 47;

        /** A map of code names to values. */
        private static final Map NAMES_TO_CODES;

        /** A map of codes to name. */
        private static final Map CODES_TO_NAMES;

        static {
            Field[] fields = Code.class.getDeclaredFields();
            Map names = new HashMap(fields.length);
            Map codes = new HashMap(fields.length);

            try {
                for (int i=0; i= 0;
        }

        public static String encode(final String text, final int code) {
            return new StringBuffer(BEGIN_TOKEN).
                    append(Code.name(code)).
                    append(CODE_TEXT_SEPARATOR).
                    append(text).
                    append(END_TOKEN).
                    toString();
        }
    }

    //
    // RenderWriter
    //

    public static class RenderWriter
        extends PrintWriter
    {
        private final Renderer renderer = new Renderer();

        public RenderWriter(final OutputStream out) {
            super(out);
        }

        public RenderWriter(final OutputStream out, final boolean autoFlush) {
            super(out, autoFlush);
        }

        public RenderWriter(final Writer out) {
            super(out);
        }

        public RenderWriter(final Writer out, final boolean autoFlush) {
            super(out, autoFlush);
        }

        public void write(final String s) {
            if (Renderer.test(s)) {
                super.write(renderer.render(s));
            }
            else {
                super.write(s);
            }
        }
    }

    //
    // RenderMessageSource
    //

    public static class RenderMessageSource
        extends MessageSource
    {
        private final Renderer renderer = new Renderer();

        public RenderMessageSource(final String[] names) {
            super(names);
        }

        public RenderMessageSource(final String name) {
            super(name);
        }

        public RenderMessageSource(final Class[] types) {
            super(types);
        }

        public RenderMessageSource(final Class type) {
            super(type);
        }

        public String getMessage(final String code) {
            final String msg = super.getMessage(code);

            if (Renderer.test(msg)) {
                return renderer.render(msg);
            }

            return msg;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy