Skip to content

Commit d917823

Browse files
committed
Make default console width fixed to 80
1 parent 332d929 commit d917823

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/main/java/com/endava/cats/command/InfoCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void run() {
5454
String osName = System.getProperty("os.name");
5555
String osVersion = System.getProperty("os.version");
5656
String osArch = System.getProperty("os.arch");
57-
String terminalWidth = String.valueOf(ConsoleUtils.getTerminalWidth(1));
57+
String terminalWidth = String.valueOf(ConsoleUtils.getTerminalWidth());
5858
String terminalType = ConsoleUtils.getTerminalType();
5959
String shell = ConsoleUtils.getShell();
6060

src/main/java/com/endava/cats/util/ConsoleUtils.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.github.ludovicianul.prettylogger.PrettyLogger;
44
import io.github.ludovicianul.prettylogger.PrettyLoggerFactory;
5+
import lombok.Getter;
56
import org.apache.commons.lang3.StringUtils;
67
import org.fusesource.jansi.Ansi;
78
import picocli.CommandLine;
@@ -21,7 +22,14 @@ public abstract class ConsoleUtils {
2122

2223
private static final Pattern ANSI_REMOVE_PATTERN = Pattern.compile("\u001B\\[[;\\d]*m");
2324

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;
2533

2634
private ConsoleUtils() {
2735
//ntd
@@ -31,7 +39,7 @@ public static void initTerminalWidth(CommandLine.Model.CommandSpec spec) {
3139
try {
3240
terminalWidth = spec.usageMessage().width();
3341
} catch (Exception e) {
34-
terminalWidth = 0;
42+
terminalWidth = 80;
3543
}
3644
}
3745

@@ -92,18 +100,6 @@ public static String getShell() {
92100
return Optional.ofNullable(System.getenv("SHELL")).orElse("unknown");
93101
}
94102

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-
107103
/**
108104
* Render a progress row on the same console row.
109105
*
@@ -143,8 +139,7 @@ public static void renderNewRow(String path, double percentage) {
143139
*/
144140
public static void renderRow(String prefix, String path, String rightTextToRender) {
145141
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);
148143
String firstPart = path.substring(0, path.indexOf(" "));
149144
String secondPart = path.substring(path.indexOf(" ") + 1);
150145
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
158153
* @return The adjusted number of columns.
159154
*/
160155
public static int getConsoleColumns(int toSubtract) {
161-
int columns = getTerminalWidth(120);
162-
163-
return columns - toSubtract;
156+
return terminalWidth - toSubtract;
164157
}
165158

166159
/**
@@ -170,7 +163,7 @@ public static int getConsoleColumns(int toSubtract) {
170163
*/
171164
public static void renderHeader(String header) {
172165
LOGGER.noFormat(" ");
173-
int equalsNo = (getTerminalWidth(80) - header.length()) / 2;
166+
int equalsNo = (terminalWidth - header.length()) / 2;
174167
LOGGER.noFormat(Ansi.ansi().bold().a("=".repeat(equalsNo) + header + "=".repeat(equalsNo)).reset().toString());
175168
}
176169

0 commit comments

Comments
 (0)