Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support coloring for 'gis st' #3

Merged
merged 1 commit into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/org/nqm/GisCommand.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.nqm;

import static java.lang.System.err;
import static org.nqm.GitWrapper.fetch;
import static org.nqm.GitWrapper.pull;
import static org.nqm.GitWrapper.status;
import static org.nqm.utils.StdOutUtils.errln;
import org.nqm.enums.GisAction;
import java.util.Optional;
import picocli.CommandLine;
Expand All @@ -18,6 +18,7 @@
version = "1.0.0-alpha")
public class GisCommand implements Runnable {

// TODO: replace action with picocli sub commands
@Parameters(index = "0", description = "Valid values: ${COMPLETION-CANDIDATES}")
GisAction action;

Expand All @@ -36,7 +37,7 @@ public static void main(String[] args) {
@Override
public void run() {
switch (action) {
case co -> value.ifPresentOrElse(GitWrapper::checkOut, () -> err.println("Please specify branch!"));
case co -> value.ifPresentOrElse(GitWrapper::checkOut, () -> errln("Please specify branch!"));
case fe -> fetch();
case pu -> pull();
case st -> status();
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/nqm/GitWrapper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.nqm;

import static java.lang.System.err;
import static java.lang.System.out;
import static org.nqm.utils.StdOutUtils.errln;
import static org.nqm.utils.StdOutUtils.warnln;
import org.nqm.vertx.CommandVerticle;
import org.nqm.vertx.GisVertx;
import java.nio.file.Path;
Expand All @@ -20,7 +21,7 @@ final class GitWrapper {
private GitWrapper() {}

public static void status() {
run(path -> call(path, "status -sb --ignore-submodules"), err::println);
run(path -> call(path, "status -sb --ignore-submodules", true), err::println);
}

public static void fetch() {
Expand All @@ -33,7 +34,7 @@ public static void pull() {

public static void checkOut(String branch) {
run(path -> call(path, "checkout %s".formatted(branch)),
() -> err.println("Could not checkout branch '%s'".formatted(branch)));
() -> errln("Could not checkout branch '%s'".formatted(branch)));
}

public static void checkOutNewBranch(String branch) {
Expand All @@ -44,7 +45,7 @@ public static void checkOutNewBranch(String branch) {
private static void run(Function<Path, Void> consume, Runnable errHandling) {
var gitModulesFilePath = Path.of(".", ".gitmodules");
if (!gitModulesFilePath.toFile().exists()) {
out.println("There is no git submodules under this directory!");
warnln("There is no git submodules under this directory!");
return;
}

Expand All @@ -58,20 +59,24 @@ private static void run(Function<Path, Void> consume, Runnable errHandling) {
vertx.executeBlocking((Promise<Void> p) -> p.complete(consume.apply(Path.of(CURRENT_DIR))));
}
else {
err.println("failed to read file");
errln("failed to read file");
System.exit(1);
}
});
}

private static Void call(Path path, String command) {
private static Void call(Path path, String command, boolean colorOutput) {
if (!path.toFile().exists()) {
return null;
}
GisVertx.instance().deployVerticle(new CommandVerticle(GIT, command, path));
GisVertx.instance().deployVerticle(new CommandVerticle(GIT, command, path, colorOutput));
return null;
}

private static Void call(Path path, String command) {
return call(path, command, false);
}

private static Stream<String> extractDirs(Buffer buffer) {
return Stream.of(buffer.toString().split("\n"))
.map(String::trim)
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/nqm/utils/StdOutUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.nqm.utils;

import static java.lang.System.err;

public class StdOutUtils {

private StdOutUtils() {}

public static final String CL_RESET = "\u001B[0m";
public static final String CL_BLACK = "\u001B[30m";
public static final String CL_RED = "\u001B[31m";
public static final String CL_GREEN = "\u001B[32m";
public static final String CL_YELLOW = "\u001B[33m";
public static final String CL_BLUE = "\u001B[34m";
public static final String CL_PURPLE = "\u001B[35m";
public static final String CL_CYAN = "\u001B[36m";
public static final String CL_WHITE = "\u001B[37m";

public static final String FONT_BOLD = "\u001B[1m";

public static void errln(String msg) {
err.println(CL_RED + "ERROR: " + msg + CL_RESET);
}

public static void warnln(String msg) {
err.println(CL_YELLOW + "WARNING: " + msg + CL_RESET);
}

public static String infof(String msgFormat, String word) {
return msgFormat.formatted(CL_CYAN + word + CL_RESET);
}

public static String coloringWord(String word, String color) {
return color + word + CL_RESET;
}

}
46 changes: 43 additions & 3 deletions src/main/java/org/nqm/vertx/CommandVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@

import static java.lang.System.out;
import static org.nqm.utils.ExceptionUtils.throwIf;
import static org.nqm.utils.StdOutUtils.CL_GREEN;
import static org.nqm.utils.StdOutUtils.CL_PURPLE;
import static org.nqm.utils.StdOutUtils.CL_RED;
import static org.nqm.utils.StdOutUtils.FONT_BOLD;
import static org.nqm.utils.StdOutUtils.coloringWord;
import static org.nqm.utils.StdOutUtils.infof;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;

public class CommandVerticle extends AbstractVerticle {

private static final String SPACES_NOT_INSIDE_SQUARE_BRACKETS = "\\s((?<!\\[.*)|(?!.*\\]))";

private final String git;
private final String command;
private final Path path;
private final boolean colorOutput;

public CommandVerticle(String git, String command, Path path) {
public CommandVerticle(String git, String command, Path path, boolean colorOutput) {
this.git = git;
this.command = command;
this.path = path;
this.colorOutput = colorOutput;
}

public CommandVerticle(String git, String command, Path path) {
this(git, command, path, false);
}

@Override
Expand All @@ -46,10 +63,10 @@ public void start() {
private void safelyPrint(Process pr) {
var line = "";
var input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
var sb = new StringBuilder("Entering '%s'".formatted(path.toString())).append('\n');
var sb = new StringBuilder(infof("Entering '%s'", "" + path.getFileName())).append('\n');
try {
while (isNotBlank(line = input.readLine())) {
sb.append(line).append('\n');
sb.append(colorOutput ? coloringOuput(line) : line).append('\n');
}
out.print(sb.toString());
Optional.of(pr.waitFor())
Expand All @@ -64,4 +81,27 @@ private void safelyPrint(Process pr) {
private static boolean isNotBlank(String s) {
return s != null && !s.isBlank();
}

private static String coloringOuput(String line) {
var words = Stream.of(line.split(SPACES_NOT_INSIDE_SQUARE_BRACKETS))
.filter(Predicate.not(String::isBlank))
.toArray(String[]::new);

try {
String startWord = words[0];
if (!startWord.startsWith("##")) {
words[0] = coloringWord(startWord, CL_RED);
}
else {
words[1] = coloringWord(FONT_BOLD + words[1], CL_PURPLE);
}

words[2] = coloringWord(words[2], CL_GREEN);
}
catch (ArrayIndexOutOfBoundsException e) {
// just ignore it
}
return Stream.of(words).collect(Collectors.joining(" "));
}

}