Skip to content

Commit

Permalink
Modernize code (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo committed Oct 2, 2023
1 parent 3c6f950 commit 764a422
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 84 deletions.
9 changes: 3 additions & 6 deletions src/main/java/org/fusesource/jansi/Ansi.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,14 @@ public int value() {
}
}

@FunctionalInterface
public interface Consumer {
void apply(Ansi ansi);
}

public static final String DISABLE = Ansi.class.getName() + ".disable";

private static Callable<Boolean> detector = new Callable<Boolean>() {
public Boolean call() throws Exception {
return !Boolean.getBoolean(DISABLE);
}
};
private static Callable<Boolean> detector = () -> !Boolean.getBoolean(DISABLE);

public static void setDetector(final Callable<Boolean> detector) {
if (detector == null) throw new IllegalArgumentException();
Expand Down Expand Up @@ -374,7 +371,7 @@ public Ansi reset() {
}

private final StringBuilder builder;
private final ArrayList<Integer> attributeOptions = new ArrayList<Integer>(5);
private final ArrayList<Integer> attributeOptions = new ArrayList<>(5);

public Ansi() {
this(new StringBuilder(80));
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,19 +276,13 @@ private static AnsiPrintStream ansiStream(boolean stdout) {
getKernel32().setConsoleMode(console, mode[0]);
processor = null;
type = AnsiType.VirtualTerminal;
installer = new AnsiOutputStream.IoRunnable() {
@Override
public void run() throws IOException {
virtualProcessing++;
getKernel32().setConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
installer = () -> {
virtualProcessing++;
getKernel32().setConsoleMode(console, mode[0] | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
};
uninstaller = new AnsiOutputStream.IoRunnable() {
@Override
public void run() throws IOException {
if (--virtualProcessing == 0) {
getKernel32().setConsoleMode(console, mode[0]);
}
uninstaller = () -> {
if (--virtualProcessing == 0) {
getKernel32().setConsoleMode(console, mode[0]);
}
};
} else if ((IS_CONEMU || IS_CYGWIN || IS_MSYSTEM) && !isConsole) {
Expand Down Expand Up @@ -427,8 +421,7 @@ static boolean getBoolean(String name) {
try {
String val = System.getProperty(name);
result = val.isEmpty() || Boolean.parseBoolean(val);
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
} catch (IllegalArgumentException | NullPointerException ignored) {
}
return result;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/fusesource/jansi/AnsiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import org.fusesource.jansi.Ansi.Attribute;
import org.fusesource.jansi.internal.JansiLoader;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.fusesource.jansi.Ansi.ansi;

/**
Expand Down Expand Up @@ -294,7 +296,7 @@ private static String getPomPropertiesVersion(String path) throws IOException {

private static void printJansiLogoDemo() throws IOException {
BufferedReader in =
new BufferedReader(new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), "UTF-8"));
new BufferedReader(new InputStreamReader(AnsiMain.class.getResourceAsStream("jansi.txt"), UTF_8));
try {
String l;
while ((l = in.readLine()) != null) {
Expand Down
29 changes: 4 additions & 25 deletions src/main/java/org/fusesource/jansi/internal/JansiLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -198,9 +199,7 @@ private static boolean extractAndLoadLibraryFile(
if (!extractedLckFile.exists()) {
new FileOutputStream(extractedLckFile).close();
}
try (OutputStream out = new FileOutputStream(extractedLibFile)) {
copy(in, out);
}
Files.copy(in, extractedLibFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
} finally {
// Delete the extracted lib file on JVM exit.
extractedLibFile.deleteOnExit();
Expand Down Expand Up @@ -239,14 +238,6 @@ private static String randomUUID() {
return Long.toHexString(new Random().nextLong());
}

private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int n;
while ((n = in.read(buf)) > 0) {
out.write(buf, 0, n);
}
}

/**
* Loads native library using the given path and name of the library.
*
Expand Down Expand Up @@ -358,7 +349,7 @@ private static void loadJansiNativeLibrary() throws Exception {

throw new Exception(String.format(
"No native library found for os.name=%s, os.arch=%s, paths=[%s]",
OSInfo.getOSName(), OSInfo.getArchName(), join(triedPaths, File.pathSeparator)));
OSInfo.getOSName(), OSInfo.getArchName(), String.join(File.pathSeparator, triedPaths)));
}

private static boolean hasResource(String path) {
Expand Down Expand Up @@ -401,16 +392,4 @@ public static String getVersion() {
}
return version;
}

private static String join(List<String> list, String separator) {
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String item : list) {
if (first) first = false;
else sb.append(separator);

sb.append(item);
}
return sb.toString();
}
}
20 changes: 9 additions & 11 deletions src/main/java/org/fusesource/jansi/internal/OSInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Locale;

Expand Down Expand Up @@ -132,19 +134,15 @@ public static boolean isAndroid() {

public static boolean isAlpine() {
try {
Process p = Runtime.getRuntime().exec("cat /etc/os-release | grep ^ID");
p.waitFor();

InputStream in = p.getInputStream();
try {
return readFully(in).toLowerCase(Locale.ROOT).contains("alpine");
} finally {
in.close();
for (String line : Files.readAllLines(Paths.get("/etc/os-release"))) {
if (line.startsWith("ID") && line.toLowerCase(Locale.ROOT).contains("alpine")) {
return true;
}
}

} catch (Throwable e) {
return false;
} catch (Throwable ignored) {
}

return false;
}

static String getHardwareName() {
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/fusesource/jansi/io/AnsiOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.fusesource.jansi.AnsiMode;
import org.fusesource.jansi.AnsiType;

import static java.nio.charset.StandardCharsets.US_ASCII;

/**
* A ANSI print stream extracts ANSI escape codes written to
* an output stream and calls corresponding <code>AnsiProcessor.process*</code> methods.
Expand All @@ -38,12 +40,14 @@
*/
public class AnsiOutputStream extends FilterOutputStream {

public static final byte[] RESET_CODE = "\033[0m".getBytes();
public static final byte[] RESET_CODE = "\033[0m".getBytes(US_ASCII);

@FunctionalInterface
public interface IoRunnable {
void run() throws IOException;
}

@FunctionalInterface
public interface WidthSupplier {
int getTerminalWidth();
}
Expand Down Expand Up @@ -79,7 +83,7 @@ public int getTerminalWidth() {
private final byte[] buffer = new byte[MAX_ESCAPE_SEQUENCE_LENGTH];
private int pos = 0;
private int startOfValue;
private final ArrayList<Object> options = new ArrayList<Object>();
private final ArrayList<Object> options = new ArrayList<>();
private int state = LOOKING_FOR_FIRST_ESC_CHAR;
private final Charset cs;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class FastBufferedOutputStream extends FilterOutputStream {

protected final byte buf[] = new byte[8192];
protected final byte[] buf = new byte[8192];
protected int count;

public FastBufferedOutputStream(OutputStream out) {
Expand Down
22 changes: 5 additions & 17 deletions src/test/java/org/fusesource/jansi/AnsiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.jupiter.params.provider.CsvSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for the {@link Ansi} class.
Expand All @@ -30,20 +32,10 @@ public class AnsiTest {
@Test
public void testSetEnabled() throws Exception {
Ansi.setEnabled(false);
new Thread() {
@Override
public void run() {
assertEquals(false, Ansi.isEnabled());
}
}.run();
new Thread(() -> assertFalse(Ansi.isEnabled())).run();

Ansi.setEnabled(true);
new Thread() {
@Override
public void run() {
assertEquals(true, Ansi.isEnabled());
}
}.run();
new Thread(() -> assertTrue(Ansi.isEnabled())).run();
}

@Test
Expand All @@ -59,11 +51,7 @@ public void testApply() {
assertEquals(
"test",
Ansi.ansi()
.apply(new Ansi.Consumer() {
public void apply(Ansi ansi) {
ansi.a("test");
}
})
.apply(ansi -> ansi.a("test"))
.toString());
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/fusesource/jansi/EncodingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;

import org.fusesource.jansi.io.AnsiOutputStream;
Expand All @@ -32,7 +32,7 @@ public class EncodingTest {
@Test
public void testEncoding8859() throws UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final AtomicReference<String> newLabel = new AtomicReference<String>();
final AtomicReference<String> newLabel = new AtomicReference<>();
PrintStream ansi = new AnsiPrintStream(
new AnsiOutputStream(
baos,
Expand All @@ -46,7 +46,7 @@ protected void processChangeWindowTitle(String label) {
},
AnsiType.Emulation,
AnsiColors.TrueColor,
Charset.forName("ISO-8859-1"),
StandardCharsets.ISO_8859_1,
null,
null,
false),
Expand All @@ -61,7 +61,7 @@ protected void processChangeWindowTitle(String label) {
@Test
public void testEncodingUtf8() throws UnsupportedEncodingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final AtomicReference<String> newLabel = new AtomicReference<String>();
final AtomicReference<String> newLabel = new AtomicReference<>();
PrintStream ansi = new PrintStream(
new AnsiOutputStream(
baos,
Expand All @@ -75,7 +75,7 @@ protected void processChangeWindowTitle(String label) {
},
AnsiType.Emulation,
AnsiColors.TrueColor,
Charset.forName("UTF-8"),
StandardCharsets.UTF_8,
null,
null,
false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.fusesource.jansi.AnsiColors;
import org.fusesource.jansi.AnsiMode;
Expand All @@ -38,7 +38,7 @@ void canHandleSgrsWithMultipleOptions() throws IOException {
null,
AnsiType.Emulation,
AnsiColors.TrueColor,
Charset.forName("UTF-8"),
StandardCharsets.UTF_8,
null,
null,
false);
Expand Down

0 comments on commit 764a422

Please sign in to comment.