Skip to content

Commit b3d402d

Browse files
committed
📺 Release 2.0.6
1. File output 2. Last line fix
1 parent 0988615 commit b3d402d

File tree

8 files changed

+87
-12
lines changed

8 files changed

+87
-12
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
This project offers way to seamlessly add coloured log messages in academic, study or research projects. This is not a production ready module and shouldn't be used in a production setting. It is designed also for use in tutorials, classes, teaching and knowledge sharing.
2020

21+
## Release notes for version 2.0.6 - 2021/03/13
22+
23+
1. File output
24+
2. Last line fix
25+
2126
## Release notes for version 2.0.5 - 2021/03/13
2227

2328
1. Long Title fix
@@ -75,7 +80,7 @@ Consolerizer for Java 11 and above:
7580
<dependency>
7681
<groupId>org.jesperancinha.console</groupId>
7782
<artifactId>consolerizer</artifactId>
78-
<version>2.0.5</version>
83+
<version>2.0.6</version>
7984
</dependency>
8085
```
8186

@@ -94,9 +99,9 @@ Consolerizer for Java 8 (Discontinued):
9499
### Tools
95100

96101
```bash
97-
git push --delete origin 2.0.5
98-
git tag --delete 2.0.5
99-
git tag 2.0.5
102+
git push --delete origin 2.0.6
103+
git tag --delete 2.0.6
104+
git tag 2.0.6
100105
git push --tags
101106
```
102107

consolerizer-spring-boot-tester/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>org.jesperancinha.console.consolerizer.spring.boot.tester</groupId>
1212
<artifactId>consolerizer-spring-boot-tester</artifactId>
13-
<version>2.0.5</version>
13+
<version>2.0.6</version>
1414
<name>consolerizer-spring-boot-tester</name>
1515
<description>Consolerizer Spring Boot Tester</description>
1616
<properties>
@@ -28,7 +28,7 @@
2828
<dependency>
2929
<groupId>org.jesperancinha.console</groupId>
3030
<artifactId>consolerizer</artifactId>
31-
<version>2.0.5</version>
31+
<version>2.0.6</version>
3232
</dependency>
3333
<dependency>
3434
<groupId>org.jesperancinha.console</groupId>

consolerizer/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>consolerizer-root</artifactId>
77
<groupId>org.jesperancinha.console</groupId>
8-
<version>2.0.5</version>
8+
<version>2.0.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

consolerizer/src/main/consolerizer/org/jesperancinha/console/consolerizer/common/Composer.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jesperancinha.console.consolerizer.common;
22

3+
import java.io.File;
34
import java.util.Arrays;
45

56
public abstract class Composer<T> {
@@ -12,6 +13,8 @@ public abstract class Composer<T> {
1213

1314
protected final StringBuilder sb = new StringBuilder();
1415

16+
protected File file;
17+
1518
protected Composer() {
1619
this.splitter = "";
1720
this.appender = "";
@@ -29,6 +32,16 @@ public T out(Object text) {
2932
return (T) this;
3033
}
3134

35+
public T file(final String fileName) {
36+
this.file = new File(fileName);
37+
return (T) this;
38+
}
39+
40+
public T file() {
41+
this.file = null;
42+
return (T) this;
43+
}
44+
3245
public T autoWrite() {
3346
this.autoWrite = true;
3447
return (T) this;

consolerizer/src/main/consolerizer/org/jesperancinha/console/consolerizer/console/Consolerizer.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class Consolerizer {
2424
private final static int RAINBOW_LINE_CHARS = 10;
2525
private final static int TITLE_SPREAD = 150;
2626
private final static ConsolerizerColor CON_COLOR_DEFAULT = BRIGHT_WHITE;
27+
public static final String COLOR_REGEX = "(\033|\u001b)\\[(\\d)*((;|:)*(\\d)*){0,5}m";
2728
public static ConsolerizerColor currentColor;
2829

2930
private int typingWait;
@@ -231,7 +232,9 @@ private static void printPerLine(String printText, int typingWait, int maxLineCh
231232
.map(ConsolerizerTexts::trim)
232233
.collect(Collectors.toList()))
233234
.collect(Collectors.toList());
234-
for (List<String> list : collect) {
235+
final var maxListI = collect.size() - 1;
236+
for (int k = 0; k < collect.size(); k++) {
237+
final var list = collect.get(k);
235238
for (String line : list) {
236239
final var split = line.split("\n");
237240
for (int j = 0; j < split.length; j++) {
@@ -252,7 +255,7 @@ private static void printPerLine(String printText, int typingWait, int maxLineCh
252255
printColor(currentColor);
253256
}
254257
}
255-
if (newLineLimit && j == maxIndex || j < maxIndex) {
258+
if (newLineLimit && j == maxIndex && k == maxListI || j < maxIndex || k < maxListI) {
256259
printNewLine();
257260
}
258261
}
@@ -446,12 +449,12 @@ public void printInstanceLn(Object text) {
446449
}
447450

448451
public static int getCalculatedStringSize(String test) {
449-
final var newTest = test.replaceAll("(\033|\u001b)\\[(\\d)*((;|:)*(\\d)*){0,5}m", "");
452+
final var newTest = test.replaceAll(COLOR_REGEX, "");
450453
return newTest.length();
451454
}
452455

453456
public static int getCalculatedOffSetSize(String test) {
454-
final var newTest = test.replaceAll("(\033|\u001b)\\[(\\d)*((;|:)*(\\d)*){0,5}m", "");
457+
final var newTest = test.replaceAll(COLOR_REGEX, "");
455458
return test.length() - newTest.length();
456459
}
457460
}

consolerizer/src/main/consolerizer/org/jesperancinha/console/consolerizer/console/ConsolerizerComposer.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
import org.jesperancinha.console.consolerizer.common.Composer;
44
import org.jesperancinha.console.consolerizer.common.ConsolerizerColor;
55

6+
import java.io.BufferedOutputStream;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.io.ObjectOutputStream;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.Objects;
13+
614
import static org.jesperancinha.console.consolerizer.common.ConsolerizerColor.RESET;
15+
import static org.jesperancinha.console.consolerizer.console.Consolerizer.COLOR_REGEX;
716
import static org.jesperancinha.console.consolerizer.console.Consolerizer.printRawGeneric;
817
import static org.jesperancinha.console.consolerizer.console.Consolerizer.printRawGenericLn;
918

@@ -58,6 +67,16 @@ public ConsolerizerComposer out(ConsolerizerColor consolerizerColor, Object text
5867
if (!text.toString().endsWith("\n")) {
5968
sb.append(splitter);
6069
}
70+
if (Objects.nonNull(file)) {
71+
try (final var fos = new FileOutputStream(file, true);
72+
final var oos = new BufferedOutputStream(fos)) {
73+
oos.write(sb.toString().replaceAll(COLOR_REGEX, "").getBytes(StandardCharsets.UTF_8));
74+
} catch (FileNotFoundException e) {
75+
e.printStackTrace();
76+
} catch (IOException e) {
77+
e.printStackTrace();
78+
}
79+
}
6180
if (autoWrite) {
6281
toConsole();
6382
sb.setLength(0);

consolerizer/src/test/java/org/jesperancinha/console/consolerizer/console/ConsolerizerComposerTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.jesperancinha.console.consolerizer.console;
22

33
import org.jesperancinha.console.consolerizer.console.utils.LeafyGreen;
4+
import org.junit.jupiter.api.BeforeAll;
45
import org.junit.jupiter.api.Test;
56

7+
import java.io.File;
8+
69
import static org.jesperancinha.console.consolerizer.common.ConsolerizerColor.BLUE;
710
import static org.jesperancinha.console.consolerizer.common.ConsolerizerColor.MAGENTA;
811
import static org.jesperancinha.console.consolerizer.common.ConsolerizerColor.RED;
@@ -11,6 +14,14 @@
1114

1215
class ConsolerizerComposerTest {
1316

17+
@BeforeAll
18+
public static void setUp() {
19+
final var file = new File("/tmp/out.txt");
20+
if (file.exists()) {
21+
file.delete();
22+
}
23+
}
24+
1425
@Test
1526
void testConsolerizerComposer_whenNoSplitter_thenNiceLog() {
1627
ConsolerizerComposer.out().white("The").blue("quick").red("brown").white("fox").orange("jumps").yellow("over")
@@ -172,6 +183,7 @@ void testConsolerizerComposerAutoWrite_whenNewLineAndString_thenNiceLog() {
172183
.outSpace()
173184
.ln()
174185
.autoWrite()
186+
.file("/tmp/out.txt")
175187
.green(title("Eating leafy greens is awesome. Let's eat a %s!", "org.springframework.boot.autoconfigure.web.embedded.EmbeddedWebServerFactoryCustomizerAutoConfiguration$TomcatWebServerFactoryCustomizerConfiguration"))
176188
.none()
177189
.blue("Gotcha!")
@@ -184,4 +196,27 @@ void testConsolerizerComposerAutoWrite_whenNewLineAndString_thenNiceLog() {
184196
.black()
185197
.bgYellow("Tomato Soup!");
186198
}
199+
200+
@Test
201+
void testConsolerizerComposerAutoWriteTitle_whenNewLineAndString_thenNiceLog() {
202+
ConsolerizerComposer.outSpace()
203+
.ln()
204+
.black()
205+
.bgOrange("1. postProcessBeanFactory")
206+
.red(title("This is phase BeanFactoryPostProcessor"))
207+
.blue("This is bean %s", "beanFactory")
208+
.toConsoleLn();
209+
}
210+
211+
@Test
212+
void testConsolerizerComposerAutoWriteFile_whenNewLineAndString_thenNiceLog() {
213+
ConsolerizerComposer.outSpace()
214+
.file("/tmp/out.txt")
215+
.ln()
216+
.black()
217+
.bgOrange("1. postProcessBeanFactory")
218+
.red(title("This is phase BeanFactoryPostProcessor"))
219+
.blue("This is bean %s", "beanFactory")
220+
.toConsoleLn();
221+
}
187222
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<artifactId>consolerizer-root</artifactId>
88
<groupId>org.jesperancinha.console</groupId>
9-
<version>2.0.5</version>
9+
<version>2.0.6</version>
1010

1111
<name>Consolerizer parent POM</name>
1212
<description>Consolerizer parent POM: This project offers way to seamlessly add coloured log messages in academic,

0 commit comments

Comments
 (0)