001package com.pi4j.util;
002
003import java.util.Objects;
004
005/*
006 * #%L
007 * **********************************************************************
008 * ORGANIZATION  :  Pi4J
009 * PROJECT       :  Pi4J :: Java Library (Core)
010 * FILENAME      :  ConsoleColor.java
011 *
012 * This file is part of the Pi4J project. More information about
013 * this project can be found here:  http://www.pi4j.com/
014 * **********************************************************************
015 * %%
016 * Copyright (C) 2012 - 2016 Pi4J
017 * %%
018 * This program is free software: you can redistribute it and/or modify
019 * it under the terms of the GNU Lesser General Public License as
020 * published by the Free Software Foundation, either version 3 of the
021 * License, or (at your option) any later version.
022 *
023 * This program is distributed in the hope that it will be useful,
024 * but WITHOUT ANY WARRANTY; without even the implied warranty of
025 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
026 * GNU General Lesser Public License for more details.
027 *
028 * You should have received a copy of the GNU General Lesser Public
029 * License along with this program.  If not, see
030 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
031 * #L%
032 */
033public enum ConsoleColor {
034
035    RESET("\033[0m"),
036    BLACK("\033[30m"),
037    RED("\033[31m"),
038    GREEN("\033[32m"),
039    YELLOW("\033[33m"),
040    BLUE("\033[34m"),
041    MAGENTA("\033[35m"),
042    CYAN("\033[36m"),
043    WHITE("\033[37m");
044
045    private final String escapeSequence;
046
047    private ConsoleColor(String escapeSequence) {
048        this.escapeSequence = escapeSequence;
049    }
050
051    public static CharSequence build(ConsoleColor color, Object ... data){
052        return build(color, true, data);
053    }
054
055    public static CharSequence build(ConsoleColor color, boolean reset, Object ... data){
056        StringBuilder sb = new StringBuilder(color.getEscapeSequence());
057        for(Object d : data){
058            sb.append(d);
059        }
060        if(reset) sb.append(RESET.getEscapeSequence());
061        return sb.toString();
062    }
063
064    public static CharSequence conditional(boolean condition, ConsoleColor positive, ConsoleColor negative, Object ... data) {
065        if (condition) {
066            return positive.build(data);
067        }
068        else{
069            return negative.build(data);
070        }
071    }
072
073    @Override
074    public String toString(){
075        return getEscapeSequence();
076    }
077
078    public String toString(Object ... data){
079        return build(data).toString();
080    }
081
082    public String getEscapeSequence(){
083        return escapeSequence;
084    }
085
086    public CharSequence build(Object ... data){
087        return ConsoleColor.build(this, data);
088    }
089
090    public CharSequence build(boolean reset, Object ... data){
091        return ConsoleColor.build(this, reset, data);
092    }
093}