diff --git a/build.gradle b/build.gradle index a8d8e93..92f9e4a 100644 --- a/build.gradle +++ b/build.gradle @@ -49,6 +49,7 @@ dependencies { compile 'io.rudefox:vixen:0.0.3' compile 'info.picocli:picocli:4.5.1' compile 'com.google.zxing:core:3.4.0' + compile 'com.diogonunes:JColor:5.0.0' testCompile 'com.bjdweck.test:commons-test:0.0.1' testCompile "org.junit.jupiter:junit-jupiter-params:$junitVersion" testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion" diff --git a/src/main/java/io/rudefox/burrow/Dice8EntropyGenerator.java b/src/main/java/io/rudefox/burrow/Dice8EntropyGenerator.java index 6522826..d25b1e9 100644 --- a/src/main/java/io/rudefox/burrow/Dice8EntropyGenerator.java +++ b/src/main/java/io/rudefox/burrow/Dice8EntropyGenerator.java @@ -1,23 +1,32 @@ package io.rudefox.burrow; import com.bjdweck.bitcoin.mnemonic.Entropy; +import com.diogonunes.jcolor.AnsiFormat; import java.util.Scanner; import java.util.regex.Pattern; +import static com.diogonunes.jcolor.Attribute.*; + public class Dice8EntropyGenerator { + public static final AnsiFormat GREEN_STYLE = new AnsiFormat(BLACK_TEXT(), BACK_COLOR(84), BOLD()); + public static final AnsiFormat YELLOW_STYLE = new AnsiFormat(BLACK_TEXT(), BACK_COLOR(228), BOLD()); + public static final AnsiFormat RED_STYLE = new AnsiFormat(BLACK_TEXT(), BACK_COLOR(213), BOLD()); + public static final int DICE_PER_ROLL = 11; private final int targetBitsOfEntropy; private final Scanner inputScanner; + private final boolean ansiColorOutput; private Entropy entropy = new Entropy(); - public Dice8EntropyGenerator(int targetBitsOfEntropy, Scanner inputScanner) { + public Dice8EntropyGenerator(int targetBitsOfEntropy, Scanner inputScanner, boolean ansiColorOutput) { this.targetBitsOfEntropy = targetBitsOfEntropy; this.inputScanner = inputScanner; + this.ansiColorOutput = ansiColorOutput; } Entropy generate() { @@ -81,18 +90,38 @@ public class Dice8EntropyGenerator { int entropyBitIndex = entropyBitString.length() - currentRollSetBitString.length() + rollSetBitIndex; if (entropyBitIndex < targetBitsOfEntropy) - rollSetBitsLine.append(currentRollSetBitString.charAt(rollSetBitIndex)); + rollSetBitsLine.append(styleBit(rollSetBitIndex, "" + currentRollSetBitString.charAt(rollSetBitIndex))); else rollSetBitsLine.append("-"); if (rollSetBitIndex == 32) rollSetBitsLine.append("|"); - else if (rollSetBitIndex % 3 == 2) rollSetBitsLine.append(" "); + else if (rollSetBitIndex % 3 == 2) rollSetBitsLine.append(styleBit(rollSetBitIndex, " ")); else if (rollSetBitIndex % 11 == 10) rollSetBitsLine.append(" | "); } return rollSetBitsLine; } + private String styleBit(int rollSetBitIndex, String s) { + + if (!ansiColorOutput) return s; + + return getBitFormat(rollSetBitIndex).format(s); + } + + private AnsiFormat getBitFormat(int rollSetBitIndex) { + + int wordBitIndex = rollSetBitIndex % 11; + + if (wordBitIndex == 0) + return GREEN_STYLE; + + if (wordBitIndex <= 6) + return YELLOW_STYLE; + + return RED_STYLE; + } + private String getSeedWordsLine(int rollSet) { int baseIndex = rollSet * 3; diff --git a/src/main/java/io/rudefox/burrow/MnemonicCommand.java b/src/main/java/io/rudefox/burrow/MnemonicCommand.java index ad4c19b..94477cc 100644 --- a/src/main/java/io/rudefox/burrow/MnemonicCommand.java +++ b/src/main/java/io/rudefox/burrow/MnemonicCommand.java @@ -85,7 +85,7 @@ public class MnemonicCommand implements Runnable { return getDice6EntropyInteractive(diceEventBuffer); if (isDice8InteractiveMode()) - return new Dice8EntropyGenerator(targetBitsOfEntropy, new Scanner(System.in)).generate(); + return new Dice8EntropyGenerator(targetBitsOfEntropy, new Scanner(System.in), CommandLine.Help.Ansi.AUTO.enabled()).generate(); diceEventBuffer.appendEvents(entropyOptions.eventMethod.getEventString); return diceEventBuffer.toEntropy();