Skip to content

Commit

Permalink
CLI: do not use unicode on --ascii-only
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Nov 12, 2021
1 parent e2d8c4d commit 9c49f87
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 18 deletions.
9 changes: 5 additions & 4 deletions api/src/main/java/org/aya/api/error/Problem.java
Expand Up @@ -35,7 +35,7 @@ enum Stage {
}

@NotNull SourcePos sourcePos();
/** @see Problem#computeFullErrorMessage(DistillerOptions) */
/** @see Problem#computeFullErrorMessage(DistillerOptions, boolean) */
@NotNull Doc describe(@NotNull DistillerOptions options);
@NotNull Severity level();
default @NotNull Stage stage() {
Expand Down Expand Up @@ -84,9 +84,10 @@ default boolean isError() {
);
}

default @NotNull String computeFullErrorMessage(@NotNull DistillerOptions options) {
if (sourcePos() == SourcePos.NONE) return describe(options).commonRender();
return toPrettyError(options).toDoc().commonRender();
int PAGE_WIDTH = 80;
default @NotNull String computeFullErrorMessage(@NotNull DistillerOptions options, boolean unicode) {
if (sourcePos() == SourcePos.NONE) return describe(options).renderWithPageWidth(PAGE_WIDTH, unicode);
return toPrettyError(options).toDoc().renderWithPageWidth(PAGE_WIDTH, unicode);
}

default @NotNull String computeBriefErrorMessage(@NotNull DistillerOptions options) {
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/org/aya/api/error/StreamReporter.java
Expand Up @@ -12,6 +12,6 @@
*/
public record StreamReporter(@NotNull PrintStream stream) implements Reporter {
@Override public void report(@NotNull Problem problem) {
stream.println(problem.computeFullErrorMessage(DistillerOptions.informative()));
stream.println(problem.computeFullErrorMessage(DistillerOptions.informative(), false));
}
}
2 changes: 1 addition & 1 deletion base/src/test/java/org/aya/test/ThrowingReporter.java
Expand Up @@ -17,7 +17,7 @@ private ThrowingReporter() {
}

@Override public void report(@NotNull Problem problem) {
var render = problem.computeFullErrorMessage(DistillerOptions.informative());
var render = problem.computeFullErrorMessage(DistillerOptions.informative(), false);
if (!problem.isError()) {
System.err.println(render);
return;
Expand Down
4 changes: 2 additions & 2 deletions cli/src/main/java/org/aya/cli/Main.java
Expand Up @@ -34,10 +34,10 @@ public Integer call() throws Exception {
var distillOptions = ReplConfig.loadFromDefault().distillerOptions;
if (action.compile.isLibrary) {
// TODO: move to a new tool
return LibraryCompiler.compile(filePath);
return LibraryCompiler.compile(filePath, !asciiOnly);
}
var traceBuilder = enableTrace ? new Trace.Builder() : null;
var compiler = new SingleFileCompiler(CliReporter.INSTANCE, null, traceBuilder, distillOptions);
var compiler = new SingleFileCompiler(CliReporter.stdio(!asciiOnly), null, traceBuilder, distillOptions);
var distillation = prettyStage != null ? new CompilerFlags.DistillInfo(
prettyStage,
prettyFormat,
Expand Down
8 changes: 4 additions & 4 deletions cli/src/main/java/org/aya/cli/library/LibraryCompiler.java
Expand Up @@ -29,10 +29,10 @@
/**
* @author kiva
*/
public record LibraryCompiler(@NotNull Path buildRoot) {
public static int compile(@NotNull Path libraryRoot) throws IOException {
public record LibraryCompiler(@NotNull Path buildRoot, boolean unicode) {
public static int compile(@NotNull Path libraryRoot, boolean unicode) throws IOException {
var config = LibraryConfigData.fromLibraryRoot(libraryRoot);
new LibraryCompiler(config.libraryBuildRoot()).make(config);
new LibraryCompiler(config.libraryBuildRoot(), unicode).make(config);
return 0;
}

Expand Down Expand Up @@ -88,7 +88,7 @@ private void callSingleFileCompiler(
System.out.println("UP-TO-DATE");
return;
}
var compiler = new SingleFileCompiler(CliReporter.INSTANCE, locator, null);
var compiler = new SingleFileCompiler(CliReporter.stdio(unicode), locator, null);
try {
compiler.compile(file, new CompilerFlags(
CompilerFlags.Message.EMOJI, false, null, compiledModulePath
Expand Down
6 changes: 3 additions & 3 deletions cli/src/main/java/org/aya/cli/repl/Repl.java
Expand Up @@ -40,9 +40,6 @@ private static Repl makeRepl(MainArgs.@NotNull ReplAction replAction, ReplConfig
};
}

public final @NotNull ReplCompiler replCompiler = new ReplCompiler(new CliReporter(this::println, this::errPrintln), null);
public final @NotNull CommandManager commandManager = makeCommand();

public CommandManager makeCommand() {
return new CommandManager(ImmutableSeq.of(
CommandArg.STRING,
Expand Down Expand Up @@ -71,9 +68,12 @@ public CommandManager makeCommand() {
public final @NotNull ReplConfig config;
public @NotNull Path cwd = Path.of("");
public int prettyPrintWidth = 80;
public final @NotNull ReplCompiler replCompiler;
public final @NotNull CommandManager commandManager = makeCommand();

public Repl(@NotNull ReplConfig config) {
this.config = config;
replCompiler = new ReplCompiler(new CliReporter(config.enableUnicode, this::println, this::errPrintln), null);
}

protected abstract void println(@NotNull String x);
Expand Down
14 changes: 11 additions & 3 deletions cli/src/main/java/org/aya/cli/single/CliReporter.java
Expand Up @@ -5,18 +5,26 @@
import org.aya.api.distill.DistillerOptions;
import org.aya.api.error.Problem;
import org.aya.api.error.Reporter;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

/**
* @author ice1000
*/
public record CliReporter(@NotNull Consumer<String> out, @NotNull Consumer<String> err) implements Reporter {
public static final CliReporter INSTANCE = new CliReporter(System.out::println, System.err::println);
public record CliReporter(
boolean unicode,
@NotNull Consumer<String> out,
@NotNull Consumer<String> err
) implements Reporter {
@Contract(pure = true, value = "_ -> new")
public static @NotNull CliReporter stdio(boolean unicode) {
return new CliReporter(unicode, System.out::println, System.err::println);
}

@Override public void report(@NotNull Problem problem) {
var errorMsg = problem.computeFullErrorMessage(DistillerOptions.informative());
var errorMsg = problem.computeFullErrorMessage(DistillerOptions.informative(), unicode);
var level = problem.level();
if (problem.isError() || level == Problem.Severity.WARN) err.accept(errorMsg);
else out.accept(errorMsg);
Expand Down
6 changes: 6 additions & 0 deletions gradle/upload-maven.cmd
@@ -0,0 +1,6 @@
./gradlew :api:publish
./gradlew :base:publish
./gradlew :cli:publish
./gradlew :pretty:publish
./gradlew :parser:publish
./gradlew :tools:publish

0 comments on commit 9c49f87

Please sign in to comment.