001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.plugin.cmd;
019
020import jdk.nashorn.internal.ir.annotations.Immutable;
021import net.tridentsdk.Console;
022
023/**
024 * ANSI codes for coloring the console
025 *
026 * <p>Don't use this class if you need console colors. Use {@link Console}</p>
027 *
028 * @author The TridentSDK Team
029 * @since 0.3-alpha-DP
030 */
031@Immutable
032public final class PlatformColor {
033    /**
034     * An empty string
035     */
036    public static final String EMPTY = "";
037
038    private PlatformColor() {
039    }
040
041    /**
042     * Obtains the colors needed by name
043     *
044     * <p>This is <strong>not</strong> case-sensitive</p>
045     *
046     * <p>The list of colors which can be retrieved:
047     * <ul>
048     * <li>reset</li>
049     * <li>black</li>
050     * <li>red</li>
051     * <li>green</li>
052     * <li>yellow</li>
053     * <li>purple</li>
054     * <li>cyan</li>
055     * <li>white</li>
056     * <li>cursoreol2 - ANSI escape which moves the insertion cursor 2 spaces to the right</li>
057     * </ul></p>
058     *
059     * @param color the name of the color to retrieve the escape for
060     * @return the proper escape code or {@link net.tridentsdk.plugin.cmd.PlatformColor#EMPTY} if it does not exist, or
061     * if the platform is windows, which does not support color codes or ANSI escapes
062     */
063    public static String forColor(String color) {
064        if (isWindows())
065            return EMPTY;
066
067        switch (color.toLowerCase()) {
068            case "reset":
069                return "\u001B[0m";
070            case "black":
071                return "\u001B[30m";
072            case "red":
073                return "\u001B[31m";
074            case "green":
075                return "\u001B[32m";
076            case "yellow":
077                return "\u001B[33m";
078            case "blue":
079                return "\u001B[34m";
080            case "purple":
081                return "\u001B[35m";
082            case "cyan":
083                return "\u001B[36m";
084            case "white":
085                return "\u001B[37m";
086        }
087
088        return EMPTY;
089    }
090
091    public static boolean isWindows() {
092        String os = System.getProperty("os.name");
093        return os.toLowerCase().contains("windows");
094    }
095}