@@ -11,13 +11,32 @@ public class Ansi {
1111 public static final String STYLE_DEFAULT_BACKGROUND =
1212 styles (DEFAULT_BACKGROUND ); // Reset background color
1313
14+ /**
15+ * Returns the ANSI escape sequence for the given styles. The styles can be any combination of
16+ * the style constants defined in the Constants class, such as BOLD, UNDERLINED, or the output
17+ * of the color functions like foregroundArg(). The output is a string that can be used in the
18+ * console to apply the specified styles to the text that follows.
19+ *
20+ * @param styles the style codes to apply
21+ * @return the ANSI escape sequence for the given styles
22+ */
1423 public static String styles (Object ... styles ) {
1524 if (styles == null || styles .length == 0 ) {
1625 return "" ;
1726 }
1827 return styles (new StringBuilder (), styles ).toString ();
1928 }
2029
30+ /**
31+ * Appends the ANSI escape sequence for the given styles to the provided Appendable. The styles
32+ * can be any combination of the style constants defined in the Constants class, such as BOLD,
33+ * UNDERLINED, or the output of the color functions like foregroundArg(). The output will be
34+ * passed to the provided Appendable.
35+ *
36+ * @param appendable the Appendable to which the ANSI escape sequence will be appended
37+ * @param styles the style codes to apply
38+ * @return the provided Appendable with the ANSI escape sequence appended
39+ */
2140 public static Appendable styles (Appendable appendable , Object ... styles ) {
2241 if (styles == null || styles .length == 0 ) {
2342 return appendable ;
@@ -37,88 +56,213 @@ public static Appendable styles(Appendable appendable, Object... styles) {
3756 return appendable ;
3857 }
3958
40- public static String foreground (int index ) {
41- return styles (FOREGROUND_BASE + index );
59+ /**
60+ * Returns the ANSI code for the given basic color. The index should be between 0-7. The output
61+ * is a string that can be used in the styles() method to create the final ANSI escape sequence.
62+ *
63+ * @param index the color index (0-7)
64+ * @return the ANSI code for the given basic color
65+ */
66+ public static String foregroundArg (int index ) {
67+ return String .valueOf (FOREGROUND_BASE + index );
4268 }
4369
44- public static Appendable foreground (Appendable appendable , int index ) {
45- return styles (appendable , FOREGROUND_BASE + index );
46- }
47-
48- public static String foregroundDark (int index ) {
70+ /**
71+ * Returns the ANSI code for the given dark color. The index should be between 0-7. The output
72+ * is a string that can be used in the styles() method to create the final ANSI escape sequence.
73+ *
74+ * @param index the color index (0-7)
75+ * @return the ANSI code for the given dark color
76+ */
77+ public static String foregroundDarkArg (int index ) {
4978 return String .valueOf (FOREGROUND_DARK_BASE + index );
5079 }
5180
52- public static String foregroundBright (int index ) {
81+ /**
82+ * Returns the ANSI code for the given bright color. The index should be between 0-7. The output
83+ * is a string that can be used in the styles() method to create the final ANSI escape sequence.
84+ *
85+ * @param index the color index (0-7)
86+ * @return the ANSI code for the given bright color
87+ */
88+ public static String foregroundBrightArg (int index ) {
5389 return String .valueOf (FOREGROUND_BRIGHT_BASE + index );
5490 }
5591
56- public static String background (int index ) {
92+ /**
93+ * Returns the ANSI code for the given basic background color. The index should be between 0-7.
94+ * The output is a string that can be used in the styles() method to create the final ANSI
95+ * escape sequence.
96+ *
97+ * @param index the color index (0-7)
98+ * @return the ANSI code for the given basic background color
99+ */
100+ public static String backgroundArg (int index ) {
57101 return String .valueOf (BACKGROUND_BASE + index );
58102 }
59103
60- public static String backgroundDark (int index ) {
104+ /**
105+ * Returns the ANSI code for the given dark background color. The index should be between 0-7.
106+ * The output is a string that can be used in the styles() method to create the final ANSI
107+ * escape sequence.
108+ *
109+ * @param index the color index (0-7)
110+ * @return the ANSI code for the given dark background color
111+ */
112+ public static String backgroundDarkArg (int index ) {
61113 return String .valueOf (BACKGROUND_DARK_BASE + index );
62114 }
63115
64- public static String backgroundBright (int index ) {
116+ /**
117+ * Returns the ANSI code for the given bright background color. The index should be between 0-7.
118+ * The output is a string that can be used in the styles() method to create the final ANSI
119+ * escape sequence.
120+ *
121+ * @param index the color index (0-7)
122+ * @return the ANSI code for the given bright background color
123+ */
124+ public static String backgroundBrightArg (int index ) {
65125 return String .valueOf (BACKGROUND_BRIGHT_BASE + index );
66126 }
67127
68- public static String foregroundIndexed (int index ) {
128+ /**
129+ * Returns the ANSI code for the given indexed foreground color. The index should be between
130+ * 0-255. The indexes 0-7 are the standard colors, 8-15 are the bright versions of the standard
131+ * colors, 16-231 are a 6x6x6 color cube, and 232-255 are a grayscale ramp. The output is a
132+ * string that can be used in the styles() method to create the final ANSI escape sequence.
133+ *
134+ * @param index the color index (0-255)
135+ * @return the ANSI code for the given indexed foreground color
136+ */
137+ public static String foregroundIndexedArg (int index ) {
69138 return FOREGROUND_COLORS + ";" + COLORS_INDEXED + ";" + index ;
70139 }
71140
72- public static String foregroundRgb (int r , int g , int b ) {
73- return FOREGROUND_COLORS + ";" + COLORS_RGB + ";" + r + ";" + g + ";" + b ;
74- }
75-
76- public static String backgroundIndexed (int index ) {
141+ /**
142+ * Returns the ANSI code for the given indexed background color. The index should be between
143+ * 0-255. The indexes 0-7 are the standard colors, 8-15 are the bright versions of the standard
144+ * colors, 16-231 are a 6x6x6 color cube, and 232-255 are a grayscale ramp. The output is a
145+ * string that can be used in the styles() method to create the final ANSI escape sequence.
146+ *
147+ * @param index the color index (0-255)
148+ * @return the ANSI code for the given indexed background color
149+ */
150+ public static String backgroundIndexedArg (int index ) {
77151 return BACKGROUND_COLORS + ";" + COLORS_INDEXED + ";" + index ;
78152 }
79153
80- public static String backgroundRgb (int r , int g , int b ) {
81- return BACKGROUND_COLORS + ";" + COLORS_RGB + ";" + r + ";" + g + ";" + b ;
82- }
83-
84- public static String linkStart (String url ) {
85- return OSC + "8;;" + url + OSC_END ;
154+ /**
155+ * Returns the ANSI code for the given RGB foreground color. The r, g, and b values should be
156+ * between 0-255. The output is a string that can be used in the styles() method to create the
157+ * final ANSI escape sequence.
158+ *
159+ * @param r the red component (0-255)
160+ * @param g the green component (0-255)
161+ * @param b the blue component (0-255)
162+ * @return the ANSI code for the given RGB foreground color
163+ */
164+ public static String foregroundRgbArg (int r , int g , int b ) {
165+ return FOREGROUND_COLORS + ";" + COLORS_RGB + ";" + r + ";" + g + ";" + b ;
86166 }
87167
88- public static String linkEnd () {
89- return OSC + "8;;" + OSC_END ;
168+ /**
169+ * Returns the ANSI code for the given RGB background color. The r, g, and b values should be
170+ * between 0-255. The output is a string that can be used in the styles() method to create the
171+ * final ANSI escape sequence.
172+ *
173+ * @param r the red component (0-255)
174+ * @param g the green component (0-255)
175+ * @param b the blue component (0-255)
176+ * @return the ANSI code for the given RGB background color
177+ */
178+ public static String backgroundRgbArg (int r , int g , int b ) {
179+ return BACKGROUND_COLORS + ";" + COLORS_RGB + ";" + r + ";" + g + ";" + b ;
90180 }
91181
182+ /**
183+ * Returns the ANSI escape sequence for moving the cursor in the specified direction by 1. The
184+ * direction is determined by the command parameter, which can be one of the cursor movement
185+ * commands defined in the Constants class, such as CURSOR_UP, CURSOR_DOWN, etc.
186+ *
187+ * @param command the cursor movement command (e.g. CURSOR_UP, CURSOR_DOWN, etc.)
188+ * @return the ANSI escape sequence for moving the cursor
189+ */
92190 public static String cursorMove (char command ) {
93191 return cursorMove (command , 1 );
94192 }
95193
194+ /**
195+ * Returns the ANSI escape sequence for moving the cursor in the specified direction by the
196+ * specified amount. The direction is determined by the command parameter, which can be one of
197+ * the cursor movement commands defined in the Constants class, such as CURSOR_UP, CURSOR_DOWN,
198+ * etc. The amount parameter specifies how many positions to move the cursor.
199+ *
200+ * @param command the cursor movement command (e.g. CURSOR_UP, CURSOR_DOWN, etc.)
201+ * @param amount the number of positions to move the cursor
202+ * @return the ANSI escape sequence for moving the cursor
203+ */
96204 public static String cursorMove (char command , int amount ) {
97205 return CSI + amount + command ;
98206 }
99207
100208 public static String cursorPos (int row , int col ) {
101- return CSI + row + ";" + col + "H" ;
209+ return CSI + row + ";" + col + CURSOR_POSITION ;
102210 }
103211
104212 public static String cursorHome () {
105- return CSI + "H" ;
213+ return CSI + CURSOR_POSITION ;
214+ }
215+
216+ public static String cursorToColumn (int col ) {
217+ return CSI + col + CURSOR_COLUMN ;
218+ }
219+
220+ public static String cursorUp (int amount ) {
221+ return cursorMove (CURSOR_UP , amount );
106222 }
107223
108- public static String hideCursor ( ) {
109- return CSI + "?25l" ;
224+ public static String cursorDown ( int amount ) {
225+ return cursorMove ( CURSOR_DOWN , amount ) ;
110226 }
111227
112- public static String showCursor () {
113- return CSI + "?25h" ;
228+ public static String cursorForward (int amount ) {
229+ return cursorMove (CURSOR_FORWARD , amount );
230+ }
231+
232+ public static String cursorBackward (int amount ) {
233+ return cursorMove (CURSOR_BACKWARD , amount );
234+ }
235+
236+ public static String cursorHide () {
237+ return CSI + CURSOR_HIDE ;
238+ }
239+
240+ public static String cursorShow () {
241+ return CSI + CURSOR_SHOW ;
242+ }
243+
244+ public static String cursorSave () {
245+ return "" + ESC + CURSOR_SAVE ;
246+ }
247+
248+ public static String cursorRestore () {
249+ return "" + ESC + CURSOR_RESTORE ;
114250 }
115251
116252 public static String clearScreen () {
117- return CSI + "2J" ;
253+ return CSI + SCREEN_ERASE_FULL ;
118254 }
119255
120256 public static String autoWrap (boolean enabled ) {
121- return CSI + (enabled ? "?7h" : "?7l" );
257+ return CSI + (enabled ? LINE_WRAP_ON : LINE_WRAP_OFF );
258+ }
259+
260+ public static String linkStart (String url ) {
261+ return OSC + "8;;" + url + OSC_END ;
262+ }
263+
264+ public static String linkEnd () {
265+ return OSC + "8;;" + OSC_END ;
122266 }
123267
124268 private Ansi () {}
0 commit comments