2
2
3
3
import io .github .ludovicianul .prettylogger .PrettyLogger ;
4
4
import io .github .ludovicianul .prettylogger .PrettyLoggerFactory ;
5
+ import lombok .Getter ;
5
6
import org .apache .commons .lang3 .StringUtils ;
6
7
import org .fusesource .jansi .Ansi ;
7
8
import picocli .CommandLine ;
@@ -21,7 +22,14 @@ public abstract class ConsoleUtils {
21
22
22
23
private static final Pattern ANSI_REMOVE_PATTERN = Pattern .compile ("\u001B \\ [[;\\ d]*m" );
23
24
24
- private static int terminalWidth = 0 ;
25
+ /**
26
+ * -- GETTER --
27
+ * Get the width of the terminal.
28
+ * <p>
29
+ * Size is cached, so it won't reach to width changes during run.
30
+ */
31
+ @ Getter
32
+ private static int terminalWidth = 80 ;
25
33
26
34
private ConsoleUtils () {
27
35
//ntd
@@ -31,7 +39,7 @@ public static void initTerminalWidth(CommandLine.Model.CommandSpec spec) {
31
39
try {
32
40
terminalWidth = spec .usageMessage ().width ();
33
41
} catch (Exception e ) {
34
- terminalWidth = 0 ;
42
+ terminalWidth = 80 ;
35
43
}
36
44
}
37
45
@@ -92,18 +100,6 @@ public static String getShell() {
92
100
return Optional .ofNullable (System .getenv ("SHELL" )).orElse ("unknown" );
93
101
}
94
102
95
- /**
96
- * Get the width of the terminal.
97
- * <p>
98
- * Size is cached, so it won't reach to width changes during run.
99
- *
100
- * @param defaultWidth The default width to use if unable to determine the terminal size.
101
- * @return The width of the terminal.
102
- */
103
- public static int getTerminalWidth (int defaultWidth ) {
104
- return terminalWidth == 0 ? defaultWidth : terminalWidth ;
105
- }
106
-
107
103
/**
108
104
* Render a progress row on the same console row.
109
105
*
@@ -143,8 +139,7 @@ public static void renderNewRow(String path, double percentage) {
143
139
*/
144
140
public static void renderRow (String prefix , String path , String rightTextToRender ) {
145
141
String withoutAnsi = ANSI_REMOVE_PATTERN .matcher (path ).replaceAll ("" );
146
- int consoleWidth = getTerminalWidth (80 );
147
- int dots = Math .max (consoleWidth - withoutAnsi .length () - rightTextToRender .length () - 2 , 1 );
142
+ int dots = Math .max (terminalWidth - withoutAnsi .length () - rightTextToRender .length () - 2 , 1 );
148
143
String firstPart = path .substring (0 , path .indexOf (" " ));
149
144
String secondPart = path .substring (path .indexOf (" " ) + 1 );
150
145
String toPrint = Ansi .ansi ().bold ().a (prefix + firstPart + " " + "." .repeat (dots ) + secondPart + " " + rightTextToRender ).reset ().toString ();
@@ -158,9 +153,7 @@ public static void renderRow(String prefix, String path, String rightTextToRende
158
153
* @return The adjusted number of columns.
159
154
*/
160
155
public static int getConsoleColumns (int toSubtract ) {
161
- int columns = getTerminalWidth (120 );
162
-
163
- return columns - toSubtract ;
156
+ return terminalWidth - toSubtract ;
164
157
}
165
158
166
159
/**
@@ -170,7 +163,7 @@ public static int getConsoleColumns(int toSubtract) {
170
163
*/
171
164
public static void renderHeader (String header ) {
172
165
LOGGER .noFormat (" " );
173
- int equalsNo = (getTerminalWidth ( 80 ) - header .length ()) / 2 ;
166
+ int equalsNo = (terminalWidth - header .length ()) / 2 ;
174
167
LOGGER .noFormat (Ansi .ansi ().bold ().a ("=" .repeat (equalsNo ) + header + "=" .repeat (equalsNo )).reset ().toString ());
175
168
}
176
169
0 commit comments