Skip to content

Commit 4a530b1

Browse files
authored
Merge pull request #188 from romge/apply-method
Add Ansi.apply method
2 parents 30cd5a9 + 2f93859 commit 4a530b1

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/main/java/org/fusesource/jansi/Ansi.java

+15
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ public int value() {
151151
}
152152
}
153153

154+
public interface Consumer {
155+
void apply(Ansi ansi);
156+
}
157+
154158
public static final String DISABLE = Ansi.class.getName() + ".disable";
155159

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

744+
/**
745+
* Applies another function to this Ansi instance.
746+
*
747+
* @param fun the function to apply
748+
* @return this Ansi instance
749+
*/
750+
public Ansi apply(Consumer fun) {
751+
fun.apply(this);
752+
return this;
753+
}
754+
740755
@Override
741756
public String toString() {
742757
flushAttributes();

src/test/java/org/fusesource/jansi/AnsiTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,13 @@ public void testClone() throws CloneNotSupportedException {
5252

5353
assertEquals(ansi.a("test").reset().toString(), clone.a("test").reset().toString());
5454
}
55+
56+
@Test
57+
public void testApply() {
58+
assertEquals("test", Ansi.ansi().apply(new Ansi.Consumer() {
59+
public void apply(Ansi ansi) {
60+
ansi.a("test");
61+
}
62+
}).toString());
63+
}
5564
}

0 commit comments

Comments
 (0)