Skip to content

Commit

Permalink
Add Ansi.apply method
Browse files Browse the repository at this point in the history
  • Loading branch information
romge committed Dec 25, 2020
1 parent d386c6a commit 2f93859
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/fusesource/jansi/Ansi.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ public int value() {
}
}

public interface Consumer {
void apply(Ansi ansi);
}

public static final String DISABLE = Ansi.class.getName() + ".disable";

private static Callable<Boolean> detector = new Callable<Boolean>() {
Expand Down Expand Up @@ -737,6 +741,17 @@ public Ansi format(String pattern, Object... args) {
return this;
}

/**
* Applies another function to this Ansi instance.
*
* @param fun the function to apply
* @return this Ansi instance
*/
public Ansi apply(Consumer fun) {
fun.apply(this);
return this;
}

@Override
public String toString() {
flushAttributes();
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/fusesource/jansi/AnsiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,13 @@ public void testClone() throws CloneNotSupportedException {

assertEquals(ansi.a("test").reset().toString(), clone.a("test").reset().toString());
}

@Test
public void testApply() {
assertEquals("test", Ansi.ansi().apply(new Ansi.Consumer() {
public void apply(Ansi ansi) {
ansi.a("test");
}
}).toString());
}
}

0 comments on commit 2f93859

Please sign in to comment.