forked from auberonedu/truffula
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathColorPrinter.java
More file actions
127 lines (114 loc) · 3.54 KB
/
ColorPrinter.java
File metadata and controls
127 lines (114 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import java.io.PrintStream;
/**
* A utility class for printing colored text to a PrintStream using ANSI escape
* codes.
*
* The ColorPrinter allows setting a current color and printing messages in that
* color
* to the specified output stream. The color can be reset after each print or
* kept active
* based on the provided parameters.
*
* Colors are managed using the ConsoleColor enum, which defines standard ANSI
* color codes.
*
* Example Usage:
*
* ColorPrinter printer = new ColorPrinter(System.out);
* printer.setCurrentColor(ConsoleColor.RED);
* printer.println("This is red text");
*
* The printed text will appear in the terminal with the specified colors if the
* terminal supports ANSI codes.
*/
public class ColorPrinter {
/**
* The current color used for printing.
*/
private ConsoleColor currentColor;
/**
* The PrintStream to which the colored output will be written.
*/
private final PrintStream printStream;
/**
* Returns the current color set for the printer.
*
* @return the current ConsoleColor
*/
public ConsoleColor getCurrentColor() {
return currentColor;
}
/**
* Sets the current color for the printer. All subsequent print operations will
* use this color
* until it is changed or reset.
*
* @param color the ConsoleColor to set as the current color
*/
public void setCurrentColor(ConsoleColor color) {
this.currentColor = color;
}
/**
* Prints the given message followed by a newline in the current color.
* The color is reset to default after printing.
*
* @param message the message to print
*/
public void println(String message) {
println(message, true);
}
/**
* Prints the given message followed by a newline in the current color.
* Optionally resets the color after printing based on the reset parameter.
*
* @param message the message to print
* @param reset if true, resets the color after printing; if false, keeps the
* current color
*/
public void println(String message, boolean reset) {
print(message + System.lineSeparator(), reset);
}
/**
* Prints the given message in the current color without appending a newline.
* The color is reset to default after printing.
*
* @param message the message to print
*/
public void print(String message) {
print(message, true);
}
/**
* Prints the given message in the current color without appending a newline.
* Optionally resets the color after printing based on the reset parameter.
*
* @param message the message to print
* @param reset if true, resets the color after printing; if false, keeps the
* current color
*/
public void print(String message, boolean reset) {
String out = getCurrentColor() + message;
if (reset) {
out += ConsoleColor.RESET;
}
printStream.print(out);
}
/**
* Constructs a ColorPrinter with the specified PrintStream.
* The default color is set to ConsoleColor.WHITE.
*
* @param printStream the PrintStream to which output will be written
*/
public ColorPrinter(PrintStream printStream) {
this(printStream, ConsoleColor.WHITE);
}
/**
* Constructs a ColorPrinter with the specified PrintStream and initial color.
*
* @param printStream the PrintStream to which output will be written
* @param color the initial ConsoleColor to use for printing
*/
public ColorPrinter(PrintStream printStream, ConsoleColor color) {
this.printStream = printStream;
this.currentColor = color;
}
}