Skip to content

Commit

Permalink
[env-manager] basic color support
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed Feb 7, 2024
1 parent 89117a6 commit 40f0c15
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion env-manager/src/main/java/io/yupiik/dev/command/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")) :
"";

Expand Down
24 changes: 17 additions & 7 deletions env-manager/src/main/java/io/yupiik/dev/shared/MessageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down Expand Up @@ -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("#.*", "")
Expand All @@ -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("#.*", "")
Expand Down

0 comments on commit 40f0c15

Please sign in to comment.