Skip to content

Commit

Permalink
Merge 942b0f0 into f4d07ea
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored Feb 25, 2023
2 parents f4d07ea + 942b0f0 commit 261ccc7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,6 +105,9 @@ private Planetiler(Arguments arguments) {
stats = arguments.getStats();
overallTimer = stats.startStageQuietly("overall");
config = PlanetilerConfig.from(arguments);
if (config.color() != null) {
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);
downloadSources = onlyDownloadSources || arguments.getBoolean("download", "download sources", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getList(String key, String description, List<String> defaultValue) {
String value = getArg(key, String.join(",", defaultValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
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/)
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() {}

private static final String COLOR_RESET = "\u001B[0m";
Expand All @@ -13,7 +23,7 @@ private AnsiColors() {}
private static final String BOLD = "\u001B[1m";

private static String color(String fg, String string) {
return fg + string + COLOR_RESET;
return useColors.get() ? (fg + string + COLOR_RESET) : string;
}

public static String red(String string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,18 @@ void testDontAccessArgListUntilUsed() {
assertEquals("value1", args.getString("key1", ""));
assertThrows(ExpectedException.class, args::toMap);
}

@Test
void testBooleanObject() {
Map<String, String> 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"));
}
}

0 comments on commit 261ccc7

Please sign in to comment.