-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathText.java
86 lines (69 loc) · 2.43 KB
/
Text.java
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
public class Text{
/*Base colors*/
public static final int BLACK = 30;
public static final int RED = 31;
public static final int GREEN = 32;
public static final int YELLOW = 33;
public static final int BLUE = 34;
public static final int MAGENTA = 35;
public static final int CYAN = 36;
public static final int WHITE = 37;
/*Text modifiers to be ADDED to a color*/
public static final int BACKGROUND = 10;
public static final int BRIGHT = 60;
/*Text modifiers that are separate from color*/
public static final int BOLD = 1;
public static final int UNDERLINE = 4;
public static final int INVERTED = 7;
/*Reset colors*/
public static void reset(){
System.out.print("\u001b[0m");
}
public static void hideCursor(){
System.out.print("\u001b[?25l");
}
public static void showCursor(){
System.out.print("\u001b[?25h");
}
/*Move the cursor to a specified row/col on the terminal*/
public static void go(int row,int col){
System.out.print("\u001b[" + row + ";" + col + "f");
}
/*Erases all text on the terminal.*/
public static void clear(){
System.out.print("\u001b[2J");
}
// method to only accept color
public static String colorize(int c1){
return ("\u001b[" + c1 + "m");
}
/*Overloaded Colorize methods.
c1,c2 and c3 are any color modifiers such as bold/color/background color etc.
*/
public static String colorize(String text,int c1){
return ("\u001b[" + c1 + "m"+text+"\u001b[0m");
}
public static String colorize(String text,int c1,int c2){
return ("\u001b[" + c1 + ";" + c2 + "m"+text+"\u001b[0m");
}
public static String colorize(String text,int c1,int c2,int c3){
return ("\u001b[" + c1 + ";" + c2 + ";" + c3 + "m"+text+"\u001b[0m");
}
//Tested and working in:
//git-bash (windows 10), wsl (windows 10+11), powershell windows 11
public static void main(String[] args) {
hideCursor();
clear();
go(1,1);
System.out.println("Abra cadabra hocus pocus...");
System.out.println("Abra "+colorize("cadabra",BOLD,YELLOW+BRIGHT,BLUE+BACKGROUND)+" hocus pocus...");
System.out.println(colorize("Abra",BOLD,BLACK,YELLOW+BACKGROUND+BRIGHT)+" cadabra hocus pocus...");
go(1,1);//top left
System.out.print("TOP!");//overwrites the "Abra" on line 1
go(4,30);
System.out.print("right?");
go(5,1);//put the terminal at the end of the output again before the program ends.
reset();
showCursor();
}
}