Skip to content

Commit

Permalink
Add classes for Style operators to improve toString behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGregory084 committed May 22, 2024
1 parent 3c3255a commit 65a5d13
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/opencastsoftware/prettier4j/Doc.java
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ static Deque<Map.Entry<Integer, Doc>> normalize(int width, int indent, int posit
// Eliminate Styled
Styled styledDoc = (Styled) entryDoc;
// Note reverse order
inQueue.addFirst(new SimpleEntry<>(entryIndent, new Reset()));
inQueue.addFirst(new SimpleEntry<>(entryIndent, Reset.getInstance()));
inQueue.addFirst(new SimpleEntry<>(entryIndent, styledDoc.doc()));
inQueue.addFirst(new SimpleEntry<>(entryIndent, new Escape(styledDoc.styles())));
} else if (entryDoc instanceof Indent) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/opencastsoftware/prettier4j/ansi/Attrs.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,31 @@ public Color bgColor() {
return color(colorType, BG_COLOR_SHIFT);
}

public Boolean isBold() {
public boolean isBold() {
return (attrs & 1) > 0;
}

public Boolean isFaint() {
public boolean isFaint() {
return (attrs & (1L << 1)) > 0;
}

public Boolean isItalic() {
public boolean isItalic() {
return (attrs & (1L << 2)) > 0;
}

public Boolean isUnderline() {
public boolean isUnderline() {
return (attrs & (1L << 3)) > 0;
}

public Boolean isBlink() {
public boolean isBlink() {
return (attrs & (1L << 4)) > 0;
}

public Boolean isInverse() {
public boolean isInverse() {
return (attrs & (1L << 5)) > 0;
}

public Boolean isStrikethrough() {
public boolean isStrikethrough() {
return (attrs & (1L << 6)) > 0;
}

Expand Down
220 changes: 211 additions & 9 deletions src/main/java/com/opencastsoftware/prettier4j/ansi/Styles.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,47 @@
*/
package com.opencastsoftware.prettier4j.ansi;


import java.util.Objects;
import java.util.function.LongUnaryOperator;

import static com.opencastsoftware.prettier4j.ansi.Attrs.*;

public class Styles {
public static LongUnaryOperator fg(Color color) {
return attrs -> withFgColor(attrs, color);
return new Fg(color);
}

public static LongUnaryOperator bg(Color color) {
return attrs -> withBgColor(attrs, color);
return new Bg(color);
}

public static LongUnaryOperator bold() {
return attrs -> attrs | 1;
return Bold.getInstance();
}

public static LongUnaryOperator faint() {
return attrs -> attrs | (1 << 1);
return Faint.getInstance();
}

public static LongUnaryOperator italic() {
return attrs -> attrs | (1 << 2);
return Italic.getInstance();
}

public static LongUnaryOperator underline() {
return attrs -> attrs | (1 << 3);
return Underline.getInstance();
}

public static LongUnaryOperator blink() {
return attrs -> attrs | (1 << 4);
return Blink.getInstance();
}

public static LongUnaryOperator inverse() {
return attrs -> attrs | (1 << 5);
return Inverse.getInstance();
}

public static LongUnaryOperator strikethrough() {
return attrs -> attrs | (1 << 6);
return Strikethrough.getInstance();
}

private static long withFgColor(long attrs, Color color) {
Expand Down Expand Up @@ -83,4 +85,204 @@ private static long withColor(long attrs, Color color, long maskValue, long shif

return noColor | (newColor << shiftValue);
}

interface StylesOperator extends LongUnaryOperator {}

static class Fg implements StylesOperator {
private final Color color;

public Fg(Color color) {
this.color = color;
}

@Override
public long applyAsLong(long attrs) {
return withFgColor(attrs, color);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Fg fg = (Fg) o;
return Objects.equals(color, fg.color);
}

@Override
public int hashCode() {
return Objects.hashCode(color);
}

@Override
public String toString() {
return "Fg [" +
"color=" + color +
']';
}
}

static class Bg implements StylesOperator {
private final Color color;

public Bg(Color color) {
this.color = color;
}

@Override
public long applyAsLong(long attrs) {
return withBgColor(attrs, color);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bg fg = (Bg) o;
return Objects.equals(color, fg.color);
}

@Override
public int hashCode() {
return Objects.hashCode(color);
}

@Override
public String toString() {
return "Bg [" +
"color=" + color +
']';
}
}

static abstract class DisplayStylesOperator implements StylesOperator {
int shiftValue;

DisplayStylesOperator(int shiftValue) {
this.shiftValue = shiftValue;
}

@Override
public long applyAsLong(long attrs) {
return attrs | (1L << shiftValue);
}
}

static class Bold extends DisplayStylesOperator {
private static final Bold INSTANCE = new Bold();

static Bold getInstance() {
return INSTANCE;
}

Bold() {
super(0);
}

@Override
public String toString() {
return "Bold []";
}
}

static class Faint extends DisplayStylesOperator {
private static final Faint INSTANCE = new Faint();

static Faint getInstance() {
return INSTANCE;
}

Faint() {
super(1);
}

@Override
public String toString() {
return "Faint []";
}
}

static class Italic extends DisplayStylesOperator {
private static final Italic INSTANCE = new Italic();

static Italic getInstance() {
return INSTANCE;
}

Italic() {
super(2);
}

@Override
public String toString() {
return "Italic []";
}
}

static class Underline extends DisplayStylesOperator {
private static final Underline INSTANCE = new Underline();

static Underline getInstance() {
return INSTANCE;
}

Underline() {
super(3);
}

@Override
public String toString() {
return "Underline []";
}
}

static class Blink extends DisplayStylesOperator {
private static final Blink INSTANCE = new Blink();

static Blink getInstance() {
return INSTANCE;
}

Blink() {
super(4);
}

@Override
public String toString() {
return "Blink []";
}
}

static class Inverse extends DisplayStylesOperator {
private static final Inverse INSTANCE = new Inverse();

static Inverse getInstance() {
return INSTANCE;
}

Inverse() {
super(5);
}

@Override
public String toString() {
return "Inverse []";
}
}

static class Strikethrough extends DisplayStylesOperator {
private static final Strikethrough INSTANCE = new Strikethrough();

static Strikethrough getInstance() {
return INSTANCE;
}

Strikethrough() {
super(6);
}

@Override
public String toString() {
return "Strikethrough []";
}
}
}

0 comments on commit 65a5d13

Please sign in to comment.