mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-24 06:20:25 -05:00
chore: first commit
This commit is contained in:
69 files changed
+3697
No files matched your search
@@ -0,0 +1,36 @@
|
||||
package io.floci.cli.output;
|
||||
|
||||
public final class Ansi {
|
||||
|
||||
public static final String RESET = "[0m";
|
||||
public static final String BOLD = "[1m";
|
||||
public static final String GREEN = "[32m";
|
||||
public static final String YELLOW = "[33m";
|
||||
public static final String RED = "[31m";
|
||||
public static final String CYAN = "[36m";
|
||||
public static final String GRAY = "[90m";
|
||||
public static final String GOLD = "[38;5;214m";
|
||||
|
||||
private static boolean enabled = true;
|
||||
|
||||
private Ansi() {}
|
||||
|
||||
public static void disable() {
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
public static String apply(String code, String text) {
|
||||
if (!enabled) return text;
|
||||
return code + text + RESET;
|
||||
}
|
||||
|
||||
public static String green(String text) { return apply(GREEN, text); }
|
||||
public static String yellow(String text) { return apply(YELLOW, text); }
|
||||
public static String red(String text) { return apply(RED, text); }
|
||||
public static String cyan(String text) { return apply(CYAN, text); }
|
||||
public static String gray(String text) { return apply(GRAY, text); }
|
||||
public static String bold(String text) { return apply(BOLD, text); }
|
||||
public static String gold(String text) { return apply(GOLD, text); }
|
||||
|
||||
public static boolean isEnabled() { return enabled; }
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.floci.cli.output;
|
||||
|
||||
public enum OutputFormat {
|
||||
text, json, yaml
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package io.floci.cli.output;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
public final class Printer {
|
||||
|
||||
private static final ObjectMapper JSON = new ObjectMapper()
|
||||
.enable(SerializationFeature.INDENT_OUTPUT);
|
||||
|
||||
private static final ObjectMapper YAML = new ObjectMapper(
|
||||
YAMLFactory.builder()
|
||||
.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)
|
||||
.build());
|
||||
|
||||
private final PrintStream out;
|
||||
private final PrintStream err;
|
||||
private final OutputFormat format;
|
||||
private final boolean quiet;
|
||||
|
||||
public Printer(PrintStream out, PrintStream err, OutputFormat format, boolean quiet) {
|
||||
this.out = out;
|
||||
this.err = err;
|
||||
this.format = format;
|
||||
this.quiet = quiet;
|
||||
}
|
||||
|
||||
public void println(String line) {
|
||||
if (!quiet) out.println(line);
|
||||
}
|
||||
|
||||
public void print(String text) {
|
||||
if (!quiet) out.print(text);
|
||||
}
|
||||
|
||||
public void error(String message) {
|
||||
err.println(Ansi.red("Error: ") + message);
|
||||
}
|
||||
|
||||
public void warn(String message) {
|
||||
err.println(Ansi.yellow("Warning: ") + message);
|
||||
}
|
||||
|
||||
public void structured(Object data) {
|
||||
try {
|
||||
switch (format) {
|
||||
case json -> out.println(JSON.writeValueAsString(data));
|
||||
case yaml -> out.print(YAML.writeValueAsString(data));
|
||||
default -> out.println(data.toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
error("Failed to serialize output: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public OutputFormat format() { return format; }
|
||||
public boolean isQuiet() { return quiet; }
|
||||
}
|
||||
Reference in new issue
Block a user