diff --git a/env-manager/src/main/java/io/yupiik/dev/command/Env.java b/env-manager/src/main/java/io/yupiik/dev/command/Env.java index 7c644473..4d341371 100644 --- a/env-manager/src/main/java/io/yupiik/dev/command/Env.java +++ b/env-manager/src/main/java/io/yupiik/dev/command/Env.java @@ -119,12 +119,14 @@ public void close() throws SecurityException { .map(r -> quoted(rc.toBin(r.path()))) .collect(joining(pathSeparator, "", pathSeparator)) + pathVar + "\";\n" : ""; + final var home = System.getProperty("user.home", ""); final var echos = Boolean.parseBoolean(tools.getProperty("echo", "true")) ? resolved.stream() // don't log too much, if it does not change, don't re-log it .filter(Predicate.not(it -> Objects.equals(it.path().toString(), System.getenv(it.properties().envPathVarName())))) .map(e -> "echo \"[yem] Resolved " + messageHelper.formatToolNameAndVersion( - e.candidate(), e.properties().toolName(), e.properties().version()) + " to '" + e.path() + "'\";") + e.candidate(), e.properties().toolName(), e.properties().version()) + " to '" + + e.path().toString().replace(home, "~") + "'\";") .collect(joining("\n", "", "\n")) : ""; diff --git a/env-manager/src/main/java/io/yupiik/dev/shared/MessageHelper.java b/env-manager/src/main/java/io/yupiik/dev/shared/MessageHelper.java index aed0aa15..b8a29980 100644 --- a/env-manager/src/main/java/io/yupiik/dev/shared/MessageHelper.java +++ b/env-manager/src/main/java/io/yupiik/dev/shared/MessageHelper.java @@ -26,8 +26,10 @@ @ApplicationScoped public class MessageHelper { + private final String colorPrefix = new String(new char[]{27, '['}); private final MessagesConfiguration configuration; private boolean supportsEmoji; + private boolean enableColors; public MessageHelper(final MessagesConfiguration configuration) { this.configuration = configuration; @@ -40,28 +42,36 @@ protected void init() { Files.exists(Path.of("/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf")); default -> !Boolean.parseBoolean(configuration.disableEmoji()); }; - } - - public boolean supportsEmoji() { - return supportsEmoji; + enableColors = switch (configuration.disableColors()) { + case "auto" -> !Boolean.parseBoolean(System.getenv("CI")); + default -> !Boolean.parseBoolean(configuration.disableColors()); + }; } public String formatToolNameAndVersion(final Candidate candidate, final String tool, final String version) { - final var base = tool + '@' + version; + final var base = format(configuration.toolColor(), tool) + " @ " + format(configuration.versionColor(), version); if (!supportsEmoji) { return base; } final var metadata = candidate.metadata(); - if (metadata.containsKey("emoji")) { - return metadata.get("emoji") + ' ' + base; + final var emoji = metadata.get("emoji"); + if (emoji != null) { + return emoji + ' ' + base; } return base; } + private String format(final String color, final String value) { + return (enableColors ? colorPrefix + color + 'm' : "") + value + (enableColors ? colorPrefix + "0m" : ""); + } + @RootConfiguration("messages") public record MessagesConfiguration( + @Property(documentation = "Are colors disabled for the terminal.", defaultValue = "\"auto\"") String disableColors, + @Property(documentation = "When color are enabled the tool name color.", defaultValue = "\"0;49;34\"") String toolColor, + @Property(documentation = "When color are enabled the version color.", defaultValue = "\"0;49;96\"") String versionColor, @Property(documentation = "If `false` emoji are totally disabled. " + "`auto` will test `/usr/share/fonts/truetype/noto/NotoColorEmoji.ttf` presence to enable emojis. " + "`true`/`false` disable/enable emoji whatever the available fonts.", defaultValue = "\"auto\"") String disableEmoji) { diff --git a/env-manager/src/test/java/io/yupiik/dev/command/CommandsTest.java b/env-manager/src/test/java/io/yupiik/dev/command/CommandsTest.java index d326f8b6..8a6595a2 100644 --- a/env-manager/src/test/java/io/yupiik/dev/command/CommandsTest.java +++ b/env-manager/src/test/java/io/yupiik/dev/command/CommandsTest.java @@ -87,7 +87,7 @@ void listLocal(@TempDir final Path work, final URI uri) { void resolve(@TempDir final Path work, final URI uri) { doInstall(work, uri); assertEquals( - "Resolved java@21.0.2: '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'" + "Resolved java @ 21.0.2: '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'" .replace("$work", work.toString()), captureOutput(work, uri, "resolve", "--tool", "java", "--version", "21.0.2")); } @@ -122,7 +122,7 @@ void env(@TempDir final Path work, final URI uri) throws IOException { export PATH="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded:$PATH"; export JAVA_HOME="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded"; export JAVA_VERSION="21.0.2"; - echo "[yem] Resolved java@21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'\";""") + echo "[yem] Resolved java @ 21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'\";""") .replace("$work", work.toString()), out .replaceAll("#.*", "") @@ -141,7 +141,7 @@ void envSdkManRc(@TempDir final Path work, final URI uri) throws IOException { export PATH="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded:$PATH"; export JAVA_HOME="$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded"; export JAVA_VERSION="21.0.2"; - echo "[yem] Resolved java@21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'\";""") + echo "[yem] Resolved java @ 21.0.2 to '$work/zulu/21.32.17-ca-jdk21.0.2/distribution_exploded'\";""") .replace("$work", work.toString()), out .replaceAll("#.*", "")