From ef9169a9a668fbc8252850151292ae5bfc363931 Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Sat, 25 Feb 2023 06:53:51 -0500 Subject: [PATCH 1/2] Support disabling colors in terminal output --- .../java/com/onthegomap/planetiler/Planetiler.java | 4 ++++ .../onthegomap/planetiler/config/Arguments.java | 8 ++++++++ .../planetiler/config/PlanetilerConfig.java | 6 ++++-- .../com/onthegomap/planetiler/util/AnsiColors.java | 5 ++++- .../planetiler/config/ArgumentsTest.java | 14 ++++++++++++++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java index 718e0c1255..6d14fdba85 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java @@ -18,6 +18,7 @@ import com.onthegomap.planetiler.stats.ProcessInfo; import com.onthegomap.planetiler.stats.Stats; import com.onthegomap.planetiler.stats.Timers; +import com.onthegomap.planetiler.util.AnsiColors; import com.onthegomap.planetiler.util.BuildInfo; import com.onthegomap.planetiler.util.ByteBufferUtil; import com.onthegomap.planetiler.util.Downloader; @@ -104,6 +105,9 @@ private Planetiler(Arguments arguments) { stats = arguments.getStats(); overallTimer = stats.startStageQuietly("overall"); config = PlanetilerConfig.from(arguments); + if (config.color() != null) { + AnsiColors.NO_COLOR = config.color(); + } tmpDir = arguments.file("tmpdir", "temp directory", Path.of("data", "tmp")); onlyDownloadSources = arguments.getBoolean("only_download", "download source data then exit", false); downloadSources = onlyDownloadSources || arguments.getBoolean("download", "download sources", false); diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/config/Arguments.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/config/Arguments.java index 015543bfc7..02ad44eab6 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/config/Arguments.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/config/Arguments.java @@ -354,6 +354,14 @@ public boolean getBoolean(String key, String description, boolean defaultValue) return value; } + /** Returns a boolean parsed from {@code key} or {@code null} if not specified. */ + public Boolean getBooleanObject(String key, String description) { + var arg = getArg(key); + Boolean value = arg == null ? null : "true".equalsIgnoreCase(arg); + logArgValue(key, description, value); + return value; + } + /** Returns a {@link List} parsed from {@code key} argument where values are separated by commas. */ public List getList(String key, String description, List defaultValue) { String value = getArg(key, String.join(",", defaultValue)); diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/config/PlanetilerConfig.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/config/PlanetilerConfig.java index 860ab42013..ec4f0562bc 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/config/PlanetilerConfig.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/config/PlanetilerConfig.java @@ -49,7 +49,8 @@ public record PlanetilerConfig( boolean osmLazyReads, boolean compactDb, boolean skipFilledTiles, - int tileWarningSizeBytes + int tileWarningSizeBytes, + Boolean color ) { public static final int MIN_MINZOOM = 0; @@ -175,7 +176,8 @@ public static PlanetilerConfig from(Arguments arguments) { false), (int) (arguments.getDouble("tile_warning_size_mb", "Maximum size in megabytes of a tile to emit a warning about", - 1d) * 1024 * 1024) + 1d) * 1024 * 1024), + arguments.getBooleanObject("color", "Color the terminal output") ); } diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/AnsiColors.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/AnsiColors.java index a0c20d99fd..8ec5ebcc87 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/AnsiColors.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/AnsiColors.java @@ -2,6 +2,9 @@ /** Utilities for styling terminal output. */ public class AnsiColors { + // Support NO_COLOR env var (https://no-color.org/) + public static boolean NO_COLOR = System.getenv("NO_COLOR") != null && !"\0".equals(System.getenv("NO_COLOR")); + private AnsiColors() {} private static final String COLOR_RESET = "\u001B[0m"; @@ -13,7 +16,7 @@ private AnsiColors() {} private static final String BOLD = "\u001B[1m"; private static String color(String fg, String string) { - return fg + string + COLOR_RESET; + return NO_COLOR ? string : (fg + string + COLOR_RESET); } public static String red(String string) { diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/config/ArgumentsTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/config/ArgumentsTest.java index c38f938423..a4147f93e0 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/config/ArgumentsTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/config/ArgumentsTest.java @@ -279,4 +279,18 @@ void testDontAccessArgListUntilUsed() { assertEquals("value1", args.getString("key1", "")); assertThrows(ExpectedException.class, args::toMap); } + + @Test + void testBooleanObject() { + Map env = Map.of( + "BOOL_TRUE", "true", + "BOOL_FALSE", "false", + "BOOL_NO", "no" + ); + Arguments args = Arguments.of(env); + assertNull(args.getBooleanObject("BOOL_NULL", "test")); + assertEquals(true, args.getBooleanObject("BOOL_TRUE", "test")); + assertEquals(false, args.getBooleanObject("BOOL_FALSE", "test")); + assertEquals(false, args.getBooleanObject("BOOL_NO", "test")); + } } From 942b0f06a5ba38032c5cd08ce769580ef65fa8ba Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Sat, 25 Feb 2023 07:15:04 -0500 Subject: [PATCH 2/2] don't use field --- .../java/com/onthegomap/planetiler/Planetiler.java | 2 +- .../com/onthegomap/planetiler/util/AnsiColors.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java index 6d14fdba85..72e3cf4a76 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java @@ -106,7 +106,7 @@ private Planetiler(Arguments arguments) { overallTimer = stats.startStageQuietly("overall"); config = PlanetilerConfig.from(arguments); if (config.color() != null) { - AnsiColors.NO_COLOR = config.color(); + AnsiColors.setUseColors(config.color()); } tmpDir = arguments.file("tmpdir", "temp directory", Path.of("data", "tmp")); onlyDownloadSources = arguments.getBoolean("only_download", "download source data then exit", false); diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/AnsiColors.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/AnsiColors.java index 8ec5ebcc87..07e84c8eca 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/AnsiColors.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/AnsiColors.java @@ -1,9 +1,16 @@ package com.onthegomap.planetiler.util; +import java.util.concurrent.atomic.AtomicBoolean; + /** Utilities for styling terminal output. */ public class AnsiColors { // Support NO_COLOR env var (https://no-color.org/) - public static boolean NO_COLOR = System.getenv("NO_COLOR") != null && !"\0".equals(System.getenv("NO_COLOR")); + private static final AtomicBoolean useColors = + new AtomicBoolean(System.getenv("NO_COLOR") == null || "\0".equals(System.getenv("NO_COLOR"))); + + public static void setUseColors(boolean colors) { + useColors.set(colors); + } private AnsiColors() {} @@ -16,7 +23,7 @@ private AnsiColors() {} private static final String BOLD = "\u001B[1m"; private static String color(String fg, String string) { - return NO_COLOR ? string : (fg + string + COLOR_RESET); + return useColors.get() ? (fg + string + COLOR_RESET) : string; } public static String red(String string) {