|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.ansi; |
| 18 | + |
| 19 | +import org.springframework.util.Assert; |
| 20 | + |
| 21 | +/** |
| 22 | + * {@link AnsiElement} implementation for ANSI 8-bit foreground or background color codes. |
| 23 | + * |
| 24 | + * @author Toshiaki Maki |
| 25 | + * @author Phillip Webb |
| 26 | + * @since 2.2.0 |
| 27 | + * @see #foreground(int) |
| 28 | + * @see #background(int) |
| 29 | + */ |
| 30 | +public final class Ansi8BitColor implements AnsiElement { |
| 31 | + |
| 32 | + private final String prefix; |
| 33 | + |
| 34 | + private final int code; |
| 35 | + |
| 36 | + /** |
| 37 | + * Create a new {@link Ansi8BitColor} instance. |
| 38 | + * @param prefix the prefix escape chars |
| 39 | + * @param code color code (must be 0-255) |
| 40 | + * @throws IllegalArgumentException if color code is not between 0 and 255. |
| 41 | + */ |
| 42 | + private Ansi8BitColor(String prefix, int code) { |
| 43 | + Assert.isTrue(code >= 0 && code <= 255, "Code must be between 0 and 255"); |
| 44 | + this.prefix = prefix; |
| 45 | + this.code = code; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public boolean equals(Object obj) { |
| 50 | + if (this == obj) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + if (obj == null || getClass() != obj.getClass()) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + Ansi8BitColor other = (Ansi8BitColor) obj; |
| 57 | + return this.prefix.equals(other.prefix) && this.code == other.code; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public int hashCode() { |
| 62 | + return this.prefix.hashCode() * 31 + this.code; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String toString() { |
| 67 | + return this.prefix + this.code; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Return a foreground ANSI color code instance for the given code. |
| 72 | + * @param code the color code |
| 73 | + * @return an ANSI color code instance |
| 74 | + */ |
| 75 | + public static Ansi8BitColor foreground(int code) { |
| 76 | + return new Ansi8BitColor("38;5;", code); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Return a background ANSI color code instance for the given code. |
| 81 | + * @param code the color code |
| 82 | + * @return an ANSI color code instance |
| 83 | + */ |
| 84 | + public static Ansi8BitColor background(int code) { |
| 85 | + return new Ansi8BitColor("48;5;", code); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments