Skip to content

Commit f7c7181

Browse files
committed
chore: Some efficiencies in legend command
1 parent 30b67a1 commit f7c7181

2 files changed

Lines changed: 16 additions & 25 deletions

File tree

src/main/java/dev/dochia/cli/core/command/LegendCommand.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import picocli.CommandLine.ParentCommand;
66

77
import java.nio.charset.StandardCharsets;
8-
import java.time.Instant;
98
import java.util.Random;
109
import java.util.concurrent.Callable;
1110

@@ -36,11 +35,12 @@ public class LegendCommand implements Callable<Integer> {
3635
@ParentCommand
3736
Object parent; // optional: access parent config if needed later
3837

38+
public static final String LINE = "─────────────────────────────";
3939
private static final String[][] STORY_EN = new String[][]{
4040
{
41-
"─────────────────────────────",
41+
LINE,
4242
" Dochia’s Legend",
43-
"─────────────────────────────",
43+
LINE,
4444
"Baba Dochia climbed the mountain,",
4545
"wearing nine coats, one on top of another.",
4646
"She thought spring had come,",
@@ -53,10 +53,10 @@ public class LegendCommand implements Callable<Integer> {
5353
"Never trust the weather forecast,",
5454
"and always test your edge cases. 😉",
5555
"",
56-
"─────────────────────────────",
56+
LINE,
5757
"Tip: Run `dochia --chaos` to shed",
5858
"some coats of your own.",
59-
"─────────────────────────────"
59+
LINE
6060
}
6161
};
6262

@@ -93,16 +93,12 @@ private Variant chooseVariant() {
9393
if (ascii) return Variant.ASCII;
9494

9595
// Random choice for the easter egg when no explicit style is set
96-
Random rnd = (seed != null) ? new Random(seed) : seededRandom();
96+
Random rnd = (seed != null) ? new Random(seed) : new Random();
9797
int pick = rnd.nextInt(3);
98-
return pick == 0 ? Variant.STORY : (pick == 1 ? Variant.HAIKU : Variant.ASCII);
99-
}
100-
101-
private Random seededRandom() {
102-
// Derive a seed from current time, PID hash & some bytes to keep variety but reproducible per run
103-
long t = Instant.now().toEpochMilli();
104-
long mix = t ^ System.identityHashCode(this);
105-
return new Random(mix);
98+
if (pick == 0) {
99+
return Variant.STORY;
100+
}
101+
return pick == 1 ? Variant.HAIKU : Variant.ASCII;
106102
}
107103

108104
private String[][] resolveLines(Variant v) {
@@ -114,7 +110,7 @@ private String[][] resolveLines(Variant v) {
114110
}
115111

116112
private void printBoxed(String[][] content, boolean ansi) {
117-
final String top = repeat("─", Math.max(20, Math.min(120, width)));
113+
final String top = repeat("─", Math.clamp(width, 20, 120));
118114
// Only add colored title bars for STORY; keep HAIKU/ASCII clean
119115
boolean banner = content[0].length > 8; // crude but effective
120116

src/test/java/dev/dochia/cli/core/command/LegendCommandTest.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ void haiku_en_outputsExpectedThreeLines_andNoAnsiWhenNoColor() {
4545
assertThat(out)
4646
.contains("Nine coats on the hill,")
4747
.contains("Spring fooled her, winter returned —")
48-
.contains("Stone remembers all.");
49-
50-
// Ensure no ANSI escape sequences when --no-color is set
51-
assertThat(out).doesNotContain("\u001B[");
48+
.contains("Stone remembers all.")
49+
.doesNotContain("\u001B[");
5250
}
5351

5452
@Test
@@ -59,9 +57,8 @@ void ascii_en_containsAsciiArt_andPunchline_andNoAnsiWhenNoColor() {
5957
.contains(" /\\ Baba Dochia climbed high,")
6058
.contains(" / \\ dropping her nine coats…")
6159
.contains(" / ❄ \\ …then winter laughed.")
62-
.contains("/______\\ Moral: never trust inputs. 😉");
63-
64-
assertThat(out).doesNotContain("\u001B[");
60+
.contains("/______\\ Moral: never trust inputs. 😉")
61+
.doesNotContain("\u001B[");
6562
}
6663

6764
@Test
@@ -76,9 +73,7 @@ void defaultVariant_en_isOneOfKnownOutputs_andNoAnsiWhenNoColor() {
7673
.contains("Stone remembers all."),
7774
s -> assertThat(s).contains("Baba Dochia climbed high,")
7875
.contains("Moral: never trust inputs.")
79-
);
80-
81-
assertThat(out).doesNotContain("\u001B[");
76+
).doesNotContain("\u001B[");
8277
}
8378
}
8479

0 commit comments

Comments
 (0)