Skip to content

Instantly share code, notes, and snippets.

@nigjo
Last active June 5, 2020 15:03
Show Gist options
  • Save nigjo/06b32f17bc5b012adb67a148dc362ef8 to your computer and use it in GitHub Desktop.
Save nigjo/06b32f17bc5b012adb67a148dc362ef8 to your computer and use it in GitHub Desktop.
A class to make colored console applications.
/*
* 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 de.nigjo.gist.clicolor;
import java.util.Arrays;
import java.util.stream.IntStream;
/**
* Provides Basic Color Escape codes. Codes are defined by ISO 6429 (see [1]). The
* {@link #toString()} method will respect the "Standard for ANSI Colors in Terminals" as
* defined in [2].
*
* Original Gist:
* https://gist.github.com/nigjo/06b32f17bc5b012adb67a148dc362ef8
*
* <p>
* Sources:
* <ol>
* <li>https://en.wikipedia.org/wiki/ANSI_escape_code#Colors</li>
* <li>https://bixense.com/clicolors/</li>
* </ol>
* </p>
*
* @author Jens Hofschröer nigjo@github.com
*/
public enum CliColor
{
// 0 to restore default color
RESET(0),
// 1 for brighter colors
BRIGHT(1),
// 4 for underlined text
UNDERLINE(4),
// 5 for flashing text
FLASH(5),
// 30 for black foreground
BLACK(30),
// 31 for red foreground
RED(31),
// 32 for green foreground
GREEN(32),
// 33 for yellow (or brown) foreground
YELLOW(33),
// 34 for blue foreground
BLUE(34),
// 35 for purple foreground
PURPLE(35),
// 36 for cyan foreground
CYAN(36),
// 37 for white (or gray) foreground
WHITE(37),
// 40 for black background
BG_BLACK(40),
// 41 for red background
BG_RED(41),
// 42 for green background
BG_GREEN(42),
// 43 for yellow (or brown) background
BG_YELLOW(43),
// 44 for blue background
BG_BLUE(44),
// 45 for purple background
BG_PURPLE(45),
// 46 for cyan background
BG_CYAN(46),
// 47 for white (or gray) background
BG_WHITE(47);
private static final boolean clicolorActive =
(System.getenv("CLICOLOR_FORCE") != null
&& !"0".equals(System.getenv("CLICOLOR_FORCE")))
|| (System.getenv("CLICOLOR") != null
&& !"0".equals(System.getenv("CLICOLOR"))
&& System.console() != null);
private final int val;
private CliColor(int val)
{
this.val = val;
}
public int getValue()
{
return val;
}
/**
* Generates an escape sequence to a 256-Color code as foreground color.
*
* @param colorCode 256-Color code.
*/
public static CharSequence fgColor(int colorCode)
{
return toString(38, 5, colorCode & 0xFF);
}
/**
* Generates an escape sequence to a 24-Bit color as foreground color.
*
* @param r red color component. Value should be from 0 to 255.
* @param g green color component. Value should be from 0 to 255.
* @param b blue color component. Value should be from 0 to 255.
*/
public static CharSequence fgColor(int r, int g, int b)
{
return toString(38, 2, r & 0xFF, g & 0xFF, b & 0xFF);
}
/**
* Generates an escape sequence to a 24-Bit color as foreground color.
*
* @param c Java Color. All transparent infomation are ignored.
*/
public static CharSequence fgColor(java.awt.Color c)
{
return toString(38, 2, c.getRed(), c.getGreen(), c.getBlue());
}
/**
* Generates an escape sequence to a 256-Color code as background color.
*
* @param colorCode 256-Color code.
*/
public static CharSequence bgColor(int colorCode)
{
return toString(48, 5, colorCode & 0xFF);
}
/**
* Generates an escape sequence to a 24-Bit color as background color.
*
* @param r red color component. Value should be from 0 to 255.
* @param g green color component. Value should be from 0 to 255.
* @param b blue color component. Value should be from 0 to 255.
*/
public static CharSequence bgColor(int r, int g, int b)
{
return toString(48, 2, r & 0xFF, g & 0xFF, b & 0xFF);
}
/**
* Generates an escape sequence to a 24-Bit color as background color.
*
* @param c Java Color. All transparent infomation are ignored.
*/
public static CharSequence bgColor(java.awt.Color c)
{
return toString(48, 2, c.getRed(), c.getGreen(), c.getBlue());
}
/**
* Generates an escape sequence from an array of single values.
*
* @param codes
*
* @return
*/
private static CharSequence toString(int... codes)
{
if(clicolorActive && codes.length > 0)
{
StringBuilder b = new StringBuilder();
b.append("\u001B[");
boolean first = true;
for(int code : codes)
{
if(first)
{
first = false;
}
else
{
b.append(';');
}
b.append(code);
}
b.append("m");
return b;
}
return "";
}
/**
* Combines other predfined Codes with this code. The other codes will be "printed"
* after this code.
*
* @param others other predefined codes.
*
* @return returns an empty String if colored console is deactivated. Is equal to
* {@link #getCode()} if {@code others} is empty.
*/
public CharSequence and(CliColor... others)
{
if(others.length == 0)
{
return getCode();
}
return toString(IntStream
.concat(
IntStream.of(getValue()),
Arrays.stream(others)
.mapToInt(CliColor::getValue))
.toArray());
}
/**
* merged multiple pre-defined Color values to a single escape sequence.
*
* @param codes list
*
* @return returns an empty String if colored console is deactivated or the list of
* codes is empty.
*/
public static CharSequence merge(CliColor... codes)
{
return toString(
Arrays.stream(codes)
.mapToInt(CliColor::getValue)
.toArray());
}
public CharSequence getCode()
{
return new StringBuilder("\u001B[").append(val).append('m');
}
/**
* Will return the escape sequence of this Colorcode.
*
* @return returns an empty String if colored console is deactivated.
*/
@Override
public String toString()
{
if(clicolorActive)
{
return getCode().toString();
}
else
{
return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment