Skip to content
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
30 changes: 15 additions & 15 deletions twinkle-ansi/src/main/java/org/codejive/twinkle/ansi/Style.java
Original file line number Diff line number Diff line change
Expand Up @@ -503,81 +503,81 @@ public String toString() {
}

@Override
public @NonNull String toAnsiString(long currentStyleState) {
public @NonNull String toAnsiString(Style currentStyle) {
try {
return toAnsi(new StringBuilder(), currentStyleState).toString();
return toAnsi(new StringBuilder(), currentStyle).toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public @NonNull Appendable toAnsi(Appendable appendable, long currentStyleState)
public @NonNull Appendable toAnsi(Appendable appendable, Style currentStyle)
throws IOException {
if (state == F_UNKNOWN) {
// Do nothing, we keep the current state
return appendable;
}
if (currentStyleState == F_UNKNOWN) {
if (currentStyle.state() == F_UNKNOWN) {
appendable.append(Ansi.STYLE_RESET);
currentStyleState = F_UNSTYLED;
currentStyle = UNSTYLED;
}
List<Object> styles = new ArrayList<>();
if ((currentStyleState & (F_BOLD | F_FAINT)) != (state & (F_BOLD | F_FAINT))) {
if ((currentStyle.state() & (F_BOLD | F_FAINT)) != (state & (F_BOLD | F_FAINT))) {
// First we switch to NORMAL to clear both BOLD and FAINT
if ((currentStyleState & (F_BOLD | F_FAINT)) != 0) {
if (currentStyle.isBold() || currentStyle.isFaint()) {
styles.add(Ansi.NORMAL);
}
// Now we set the needed styles
if (isBold()) styles.add(Ansi.BOLD);
if (isFaint()) styles.add(Ansi.FAINT);
}
if ((currentStyleState & F_ITALIC) != (state & F_ITALIC)) {
if (currentStyle.isItalic() != isItalic()) {
if (isItalic()) {
styles.add(Ansi.ITALICIZED);
} else {
styles.add(Ansi.NOTITALICIZED);
}
}
if ((currentStyleState & F_UNDERLINED) != (state & F_UNDERLINED)) {
if (currentStyle.isUnderlined() != isUnderlined()) {
if (isUnderlined()) {
styles.add(Ansi.UNDERLINED);
} else {
styles.add(Ansi.NOTUNDERLINED);
}
}
if ((currentStyleState & F_BLINK) != (state & F_BLINK)) {
if (currentStyle.isBlink() != isBlink()) {
if (isBlink()) {
styles.add(Ansi.BLINK);
} else {
styles.add(Ansi.STEADY);
}
}
if ((currentStyleState & F_INVERSE) != (state & F_INVERSE)) {
if (currentStyle.isInverse() != isInverse()) {
if (isInverse()) {
styles.add(Ansi.INVERSE);
} else {
styles.add(Ansi.POSITIVE);
}
}
if ((currentStyleState & F_HIDDEN) != (state & F_HIDDEN)) {
if (currentStyle.isHidden() != isHidden()) {
if (isHidden()) {
styles.add(Ansi.INVISIBLE);
} else {
styles.add(Ansi.VISIBLE);
}
}
if ((currentStyleState & F_STRIKETHROUGH) != (state & F_STRIKETHROUGH)) {
if (currentStyle.isStrikethrough() != isStrikethrough()) {
if (isStrikethrough()) {
styles.add(Ansi.CROSSEDOUT);
} else {
styles.add(Ansi.NOTCROSSEDOUT);
}
}
if ((currentStyleState & MASK_FG_COLOR) != (state & MASK_FG_COLOR)) {
if ((currentStyle.state() & MASK_FG_COLOR) != (state & MASK_FG_COLOR)) {
styles.add(fgColor().toAnsiFgArgs());
}
if ((currentStyleState & MASK_BG_COLOR) != (state & MASK_BG_COLOR)) {
if ((currentStyle.state() & MASK_BG_COLOR) != (state & MASK_BG_COLOR)) {
styles.add(bgColor().toAnsiBgArgs());
}
return Ansi.style(appendable, styles.toArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface Printable {
* @return The ANSI string representation of the object.
*/
default @NonNull String toAnsiString() {
return toAnsiString(Style.F_UNKNOWN);
return toAnsiString(Style.of(Style.F_UNKNOWN));
}

/**
Expand All @@ -23,7 +23,7 @@ public interface Printable {
* @return The <code>Appendable</code> passed as parameter.
*/
default @NonNull Appendable toAnsi(Appendable appendable) throws IOException {
return toAnsi(appendable, Style.F_UNKNOWN);
return toAnsi(appendable, Style.of(Style.F_UNKNOWN));
}

/**
Expand All @@ -35,36 +35,10 @@ public interface Printable {
* @return The ANSI string representation of the object.
*/
default @NonNull String toAnsiString(Style currentStyle) {
return toAnsiString(currentStyle.state());
}

/**
* Outputs the object as an ANSI string, including ANSI escape codes for styles. This method
* takes into account the provided current style to generate a result that is as efficient as
* possible in terms of ANSI codes.
*
* @param appendable The <code>Appendable</code> to write the ANSI output to.
* @param currentStyle The current style to start with.
* @return The <code>Appendable</code> passed as parameter.
*/
default @NonNull Appendable toAnsi(Appendable appendable, Style currentStyle)
throws IOException {
return toAnsi(appendable, currentStyle.state());
}

/**
* Converts the object to an ANSI string, including ANSI escape codes for styles. This method
* takes into account the provided current style to generate a result that is as efficient as
* possible in terms of ANSI codes.
*
* @param currentStyleState The current style to start with.
* @return The ANSI string representation of the object.
*/
default @NonNull String toAnsiString(long currentStyleState) {
StringBuilder sb = new StringBuilder();
try {
return toAnsi(sb, currentStyleState).toString();
return toAnsi(new StringBuilder(), currentStyle).toString();
} catch (IOException e) {
// This should never happen since we're not actually doing any I/O here.
throw new RuntimeException(e);
}
}
Expand All @@ -75,8 +49,8 @@ public interface Printable {
* possible in terms of ANSI codes.
*
* @param appendable The <code>Appendable</code> to write the ANSI output to.
* @param currentStyleState The current style to start with.
* @param currentStyle The current style to start with.
* @return The <code>Appendable</code> passed as parameter.
*/
@NonNull Appendable toAnsi(Appendable appendable, long currentStyleState) throws IOException;
@NonNull Appendable toAnsi(Appendable appendable, Style currentStyle) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public class StyledIterator implements SequenceIterator {
private final SequenceIterator delegate;
private long currentStyleState;
private Style currentStyle;
private int nextCodePoint = -1;
private boolean primed = false;
private boolean exhausted = false;
Expand All @@ -21,16 +21,16 @@ public class StyledIterator implements SequenceIterator {
* initial style state.
*/
protected StyledIterator(SequenceIterator delegate) {
this(delegate, Style.F_UNKNOWN);
this(delegate, Style.of(Style.F_UNKNOWN));
}

/**
* Creates a StyledIterator that wraps the given SequenceIterator and starts with the given
* initial style state.
*/
public StyledIterator(SequenceIterator delegate, long currentStyleState) {
public StyledIterator(SequenceIterator delegate, Style currentStyle) {
this.delegate = delegate;
this.currentStyleState = currentStyleState;
this.currentStyle = currentStyle;
}

/** Returns true if there is still input to read. */
Expand Down Expand Up @@ -86,12 +86,7 @@ public String sequence() {

/** Returns the current style based on the ANSI escape sequences encountered so far. */
public Style style() {
return Style.of(currentStyleState);
}

/** Returns the current style state based on the ANSI escape sequences encountered so far. */
public long styleState() {
return currentStyleState;
return currentStyle;
}

private void primeNext() {
Expand All @@ -100,7 +95,7 @@ private void primeNext() {
if (cp == Ansi.ESC) {
String ansiSequence = delegate.sequence();
if (ansiSequence.startsWith(Ansi.CSI) && ansiSequence.endsWith("m")) {
currentStyleState = Style.parse(currentStyleState, ansiSequence);
currentStyle = Style.of(Style.parse(currentStyle.state(), ansiSequence));
}
} else {
nextCodePoint = cp;
Expand All @@ -113,26 +108,26 @@ private void primeNext() {
}

public static StyledIterator of(CharSequence text) {
return of(text, Style.F_UNKNOWN);
return of(text, Style.of(Style.F_UNKNOWN));
}

public static StyledIterator of(Reader input) {
return of(input, Style.F_UNKNOWN);
return of(input, Style.of(Style.F_UNKNOWN));
}

public static StyledIterator of(SequenceIterator iter) {
return of(iter, Style.F_UNKNOWN);
return of(iter, Style.of(Style.F_UNKNOWN));
}

public static StyledIterator of(CharSequence text, long currentStyleState) {
return of(SequenceIterator.of(text), currentStyleState);
public static StyledIterator of(CharSequence text, Style currentStyle) {
return of(SequenceIterator.of(text), currentStyle);
}

public static StyledIterator of(Reader input, long currentStyleState) {
return of(SequenceIterator.of(input), currentStyleState);
public static StyledIterator of(Reader input, Style currentStyle) {
return of(SequenceIterator.of(input), currentStyle);
}

public static StyledIterator of(SequenceIterator iter, long currentStyleState) {
return new StyledIterator(iter, currentStyleState);
public static StyledIterator of(SequenceIterator iter, Style currentStyle) {
return new StyledIterator(iter, currentStyle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ public void testToAnsiStringAllStylesWithCurrent() {
.inverse()
.hidden()
.strikethrough();
String ansiCode = style.toAnsiString(Style.F_BOLD | Style.F_UNDERLINED);
Style currentStyle = Style.UNSTYLED.bold().underlined();
String ansiCode = style.toAnsiString(currentStyle);
assertThat(ansiCode)
.isEqualTo(
Ansi.style(
Expand All @@ -178,7 +179,8 @@ public void testToAnsiStringAllStylesWithCurrent2() {
.inverse()
.hidden()
.strikethrough();
String ansiCode = style.toAnsiString(Style.F_BOLD | Style.F_FAINT | Style.F_UNDERLINED);
Style currentStyle = Style.UNSTYLED.bold().faint().underlined();
String ansiCode = style.toAnsiString(currentStyle);
assertThat(ansiCode)
.isEqualTo(
Ansi.style(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void testPlainSequence() {
assertThat(it.hasNext()).isTrue();
assertThat(it.next()).isEqualTo('a');
assertThat(it.sequence()).isEqualTo("a");
assertThat(it.styleState()).isEqualTo(Style.F_UNKNOWN);
assertThat(it.style()).isEqualTo(Style.of(Style.F_UNKNOWN));

assertThat(it.hasNext()).isTrue();
assertThat(it.next()).isEqualTo('b');
Expand All @@ -40,7 +40,7 @@ public void testStyledSequence() {
assertThat(it.sequence()).isEqualTo("a");

// Verify style state changed
assertThat(it.styleState()).isNotEqualTo(Style.F_UNSTYLED);
assertThat(it.style()).isNotEqualTo(Style.UNSTYLED);

assertThat(it.hasNext()).isFalse();
}
Expand All @@ -54,7 +54,7 @@ public void testSkipNonStyleAnsi() {
assertThat(it.hasNext()).isTrue();
assertThat(it.next()).isEqualTo('a');
// Style should NOT change for non-SGR sequences
assertThat(it.styleState()).isEqualTo(Style.F_UNKNOWN);
assertThat(it.style()).isEqualTo(Style.of(Style.F_UNKNOWN));

assertThat(it.hasNext()).isFalse();
}
Expand All @@ -68,14 +68,14 @@ public void testMixedSequences() {

// 'a'
assertThat(it.next()).isEqualTo('a');
assertThat(it.styleState()).isEqualTo(Style.F_UNKNOWN);
assertThat(it.style()).isEqualTo(Style.of(Style.F_UNKNOWN));

// 'b' (RED consumed)
assertThat(it.next()).isEqualTo('b');
assertThat(it.styleState()).isNotEqualTo(Style.F_UNSTYLED);
assertThat(it.style()).isNotEqualTo(Style.UNSTYLED);

// 'c' (RESET consumed)
assertThat(it.next()).isEqualTo('c');
assertThat(it.styleState()).isEqualTo(Style.F_UNSTYLED);
assertThat(it.style()).isEqualTo(Style.UNSTYLED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ public void render(Canvas canvas, Number value) {
int y = reversed || horizontal ? 0 : size.height() - 1;
// Place full blocks first
for (int i = 0; i < fullChunks; i++) {
canvas.setCharAt(x, y, Style.F_UNSTYLED, blocks[0]);
canvas.setCharAt(x, y, Style.UNSTYLED, blocks[0]);
x += direction.dx;
y += direction.dy;
}
// Append remainder partial block if any
if (remainder > 0) {
canvas.setCharAt(x, y, Style.F_UNSTYLED, blocks[remainder]);
canvas.setCharAt(x, y, Style.UNSTYLED, blocks[remainder]);
x += direction.dx;
y += direction.dy;
} else if (overflow) { // Or an overflow block
canvas.setCharAt(x, y, Style.F_UNSTYLED, BLOCK_OVERFLOW);
canvas.setCharAt(x, y, Style.UNSTYLED, BLOCK_OVERFLOW);
x += direction.dx;
y += direction.dy;
}
// Fill the rest with spaces
int sizeLeft = maxSize - fullChunks - (overflow || remainder > 0 ? 1 : 0);
for (int i = 0; i < sizeLeft; i++) {
canvas.setCharAt(x, y, Style.F_UNSTYLED, ' ');
canvas.setCharAt(x, y, Style.UNSTYLED, ' ');
x += direction.dx;
y += direction.dy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,29 +162,16 @@ public MathPlot ranges(Number minXValue, Number maxXValue, Number minYValue, Num
return plot.currentStyle();
}

public long currentStyleState() {
return plot.currentStyleState();
}

public MathPlot currentStyle(Style currentStyle) {
plot.currentStyle(currentStyle);
return this;
}

public MathPlot currentStyleState(long currentStyleState) {
plot.currentStyleState(currentStyleState);
return this;
}

public MathPlot plot(Function<Double, Double> func) {
return plot(func, currentStyleState());
return plot(func, currentStyle());
}

public MathPlot plot(Function<Double, Double> func, Style style) {
return plot(func, style.state());
}

public MathPlot plot(Function<Double, Double> func, long styleState) {
double xRange = maxXValue.doubleValue() - minXValue.doubleValue();
double yRange = maxYValue.doubleValue() - minYValue.doubleValue();
Size plotSize = plot.plotSize();
Expand Down Expand Up @@ -241,7 +228,7 @@ public MathPlot plot(Function<Double, Double> func, long styleState) {

while (true) {
if (x0 >= 0 && x0 < width && y0 >= 0 && y0 < height) {
plot.plot(x0, y0, styleState);
plot.plot(x0, y0, style);
}
if (x0 == x1 && y0 == y1) {
break;
Expand All @@ -260,7 +247,7 @@ public MathPlot plot(Function<Double, Double> func, long styleState) {
prevY = py;
} else if (currValid) {
// start new segment (or single point) and record as previous
plot.plot(px, py, styleState);
plot.plot(px, py, style);
prevX = px;
prevY = py;
prevValid = true;
Expand Down
Loading
Loading