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