Skip to content

Commit

Permalink
merge: support ansi16 (#1026)
Browse files Browse the repository at this point in the history
pretty: support ansi16
  • Loading branch information
ice1000 authored Feb 21, 2024
2 parents 6c5ec7f + 286e718 commit 1324440
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cli-console/src/main/java/org/aya/cli/console/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private int doFakeLiterate() throws IOException {
new FlclParser(reporter, file).computeAst());
// Garbage code
var setup = info.backendOpts(false);
var output = renderOptions.render(RenderOptions.OutputTarget.LaTeX, doc, setup);
var output = renderOptions.render(prettyFormat.target, doc, setup);
if (outputPath != null) FileUtil.writeString(outputPath, output);
else System.out.println(output);
return 0;
Expand Down
7 changes: 5 additions & 2 deletions cli-impl/src/main/java/org/aya/cli/render/RenderOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
public class RenderOptions {
public enum OutputTarget {
Unix(".txt"),
ANSI16(".txt"),
LaTeX(".tex"),
KaTeX(".katex"),
AyaMd(".md"),
Expand Down Expand Up @@ -119,6 +120,7 @@ public void invalidate() {
public static @NotNull StringStylist defaultStylist(@NotNull OutputTarget output) {
return switch (output) {
case Unix -> AdaptiveCliStylist.INSTANCE;
case ANSI16 -> AdaptiveCliStylist.INSTANCE_16;
case LaTeX -> TeXStylist.DEFAULT;
case KaTeX -> TeXStylist.DEFAULT_KATEX;
case AyaMd -> MdStylist.DEFAULT;
Expand All @@ -132,7 +134,7 @@ public void invalidate() {
final var c = buildColorScheme();
final var s = buildStyleFamily();
return switch (output) {
case Unix -> new UnixTermStylist(c, s);
case Unix, ANSI16 -> new UnixTermStylist(c, s);
case LaTeX -> new TeXStylist(c, s, false);
case KaTeX -> new TeXStylist(c, s, true);
case AyaMd -> new MdStylist(c, s);
Expand Down Expand Up @@ -188,7 +190,8 @@ public record DefaultSetup(
case KaTeX, LaTeX -> doc.render(new DocTeXPrinter(), setup.setup(new DocTeXPrinter.Config((TeXStylist) stylist)));
case HTML -> doc.render(new DocHtmlPrinter<>(), setup.setup(new DocHtmlPrinter.Config((Html5Stylist) stylist)));
case AyaMd -> doc.render(new DocMdPrinter(), setup.setup(new DocMdPrinter.Config((MdStylist) stylist)));
case Unix -> doc.render(new DocTermPrinter(), setup.setup(new DocTermPrinter.Config((UnixTermStylist) stylist)));
case Unix, ANSI16 ->
doc.render(new DocTermPrinter(), setup.setup(new DocTermPrinter.Config((UnixTermStylist) stylist)));
};
}

Expand Down
3 changes: 2 additions & 1 deletion cli-impl/src/main/java/org/aya/cli/utils/CliEnums.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ enum PrettyFormat {
latex(RenderOptions.OutputTarget.LaTeX),
katex(RenderOptions.OutputTarget.KaTeX),
markdown(RenderOptions.OutputTarget.AyaMd),
unix(RenderOptions.OutputTarget.Unix);
unix(RenderOptions.OutputTarget.Unix),
ansi16(RenderOptions.OutputTarget.ANSI16);

public final @NotNull RenderOptions.OutputTarget target;

Expand Down
1 change: 1 addition & 0 deletions cli-impl/src/test/java/org/aya/cli/RenderOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class RenderOptionsTest {
opt.checkDeserialization();
var opts = new RenderOptions.DefaultSetup(false, false, false, true, -1, false);
assertEquals("`hello'", opt.render(RenderOptions.OutputTarget.Unix, doc, opts));
assertEquals("`hello'", opt.render(RenderOptions.OutputTarget.ANSI16, doc, opts));
assertEquals("\\texttt{hello}", opt.render(RenderOptions.OutputTarget.LaTeX, doc, opts));
assertEquals("\\texttt{hello}", opt.render(RenderOptions.OutputTarget.KaTeX, doc, opts));
assertEquals("<code class=\"Aya\">hello</code>", opt.render(RenderOptions.OutputTarget.HTML, doc, opts));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

/** use colors from terminal instead of absolute colors to protect eyes */
public class AdaptiveCliStylist extends UnixTermStylist {
public static final @NotNull AdaptiveCliStylist INSTANCE = new AdaptiveCliStylist();
public static final @NotNull AdaptiveCliStylist INSTANCE = new AdaptiveCliStylist(true);
public static final @NotNull AdaptiveCliStylist INSTANCE_16 = new AdaptiveCliStylist(false);

private AdaptiveCliStylist() {
private AdaptiveCliStylist(boolean use256Colors) {
super(MutableMap::create, new AyaStyleFamily(MutableMap.ofEntries(
Tuple.of(AyaStyleKey.Keyword.key(), Style.color(ColorScheme.colorOf(1.0f, 0.43f, 0)).and()),
Tuple.of(AyaStyleKey.Prim.key(), Style.color(ColorScheme.colorOf(1.0f, 0.43f, 0)).and()),
Tuple.of(AyaStyleKey.Keyword.key(), (use256Colors ? Style.color(ColorScheme.colorOf(1.0f, 0.43f, 0)) : UnixTermStyle.TerminalRed).and()),
Tuple.of(AyaStyleKey.Prim.key(), (use256Colors ? Style.color(ColorScheme.colorOf(1.0f, 0.43f, 0)) : UnixTermStyle.TerminalRed).and()),
Tuple.of(AyaStyleKey.Fn.key(), UnixTermStyle.TerminalYellow.and()),
Tuple.of(AyaStyleKey.Data.key(), UnixTermStyle.TerminalGreen.and()),
Tuple.of(AyaStyleKey.Clazz.key(), UnixTermStyle.TerminalGreen.and()),
Expand Down

0 comments on commit 1324440

Please sign in to comment.