Skip to content

Commit d54dc27

Browse files
authored
Merge pull request #105 from web3j/web3jCliFix
Removed JCDP and updated with Jansi
2 parents 819e1e9 + 6ffa700 commit d54dc27

File tree

7 files changed

+95
-130
lines changed

7 files changed

+95
-130
lines changed

build.gradle

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ ext {
3434
log4jVersion = '2.15.0'
3535
semverVersion = '0.9.0'
3636
commonsLangVersion = '3.9'
37-
jcdpVersion = '4.0.1'
37+
jansiVersion = '2.4.1'
3838
}
3939

4040

@@ -103,7 +103,8 @@ dependencies {
103103
"org.apache.logging.log4j:log4j-core:$log4jVersion",
104104
"io.github.microutils:kotlin-logging:$kotlinLoggin",
105105
"com.github.docker-java:docker-java:$dockerJavaVersion",
106-
"com.diogonunes:JCDP:$jcdpVersion"
106+
"org.fusesource.jansi:jansi:$jansiVersion"
107+
107108

108109
runtimeOnly "org.slf4j:slf4j-nop:$slf4jVersion"
109110

src/main/java/org/web3j/console/project/ProjectRunner.java

+15-33
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
import java.io.IOException;
1616

17-
import com.diogonunes.jcdp.color.ColoredPrinter;
18-
import com.diogonunes.jcdp.color.api.Ansi;
17+
import org.fusesource.jansi.Ansi;
1918

2019
import org.web3j.console.project.java.JavaProject;
2120
import org.web3j.console.project.java.JavaProjectRunner;
@@ -41,44 +40,27 @@ public ProjectRunner(final ProjectCreatorConfig projectCreatorConfig) {
4140
}
4241

4342
public static void onSuccess(Project project) {
44-
System.out.print(System.lineSeparator());
45-
ColoredPrinter cp =
46-
new ColoredPrinter.Builder(0, false)
47-
.foreground(Ansi.FColor.WHITE)
48-
.background(Ansi.BColor.GREEN)
49-
.attribute(Ansi.Attribute.BOLD)
50-
.build();
51-
ColoredPrinter instructionPrinter =
52-
new ColoredPrinter.Builder(0, false).foreground(Ansi.FColor.CYAN).build();
53-
ColoredPrinter commandPrinter =
54-
new ColoredPrinter.Builder(0, false).foreground(Ansi.FColor.GREEN).build();
55-
System.out.print(System.lineSeparator());
56-
cp.println("Project Created Successfully");
57-
System.out.print(System.lineSeparator());
43+
System.out.println(Ansi.ansi().reset().newline());
44+
45+
System.out.println(
46+
Ansi.ansi().fgGreen().bold().a("Project Created Successfully").reset().newline());
5847

5948
if (project.getProjectWallet() != null) {
60-
instructionPrinter.println(
61-
"Project information",
62-
Ansi.Attribute.LIGHT,
63-
Ansi.FColor.WHITE,
64-
Ansi.BColor.BLACK);
65-
instructionPrinter.print(
66-
String.format("%-20s", "Wallet Address"),
67-
Ansi.Attribute.CLEAR,
68-
Ansi.FColor.WHITE,
69-
Ansi.BColor.BLACK);
70-
instructionPrinter.println(
71-
project.getProjectWallet().getWalletAddress(),
72-
Ansi.Attribute.BOLD,
73-
Ansi.FColor.GREEN,
74-
Ansi.BColor.BLACK);
75-
System.out.print(System.lineSeparator());
49+
System.out.println(Ansi.ansi().fgCyan().a("Project information").reset());
50+
System.out.println(
51+
Ansi.ansi()
52+
.a(String.format("%-20s", "Wallet Address") + " ")
53+
.fgGreen()
54+
.bold()
55+
.a(project.getProjectWallet().getWalletAddress())
56+
.reset()
57+
.newline());
7658
}
7759

7860
InstructionsPrinter.initContextPrinter(null);
7961
InstructionsPrinter.getContextPrinterInstance()
8062
.getContextPrinter()
81-
.printInstructionsOnSuccess(instructionPrinter, commandPrinter);
63+
.printInstructionsOnSuccess();
8264
}
8365

8466
@Override

src/main/java/org/web3j/console/project/utils/printer/Printer.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@
1212
*/
1313
package org.web3j.console.project.utils.printer;
1414

15-
import com.diogonunes.jcdp.color.ColoredPrinter;
16-
1715
public abstract class Printer {
1816

19-
public abstract void printInstructionsOnSuccess(
20-
ColoredPrinter instructionPrinter, ColoredPrinter commandPrinter);
17+
// Remove the parameters since JColor directly uses System.out for printing
18+
public abstract void printInstructionsOnSuccess();
2119

22-
public abstract void printInstructionsOnSuccessOpenApi(
23-
ColoredPrinter instructionPrinter, ColoredPrinter commandPrinter);
20+
public abstract void printInstructionsOnSuccessOpenApi();
2421
}

src/main/java/org/web3j/console/project/utils/printer/Web3jPrinter.java

+17-21
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,38 @@
1212
*/
1313
package org.web3j.console.project.utils.printer;
1414

15-
import com.diogonunes.jcdp.color.ColoredPrinter;
16-
import com.diogonunes.jcdp.color.api.Ansi;
15+
import org.fusesource.jansi.Ansi;
1716

1817
public class Web3jPrinter extends Printer {
1918

2019
@Override
21-
public void printInstructionsOnSuccess(
22-
ColoredPrinter instructionPrinter, ColoredPrinter commandPrinter) {
20+
public void printInstructionsOnSuccess() {
2321
String gradleCommand =
2422
System.getProperty("os.name").toLowerCase().startsWith("windows")
2523
? "./gradlew.bat"
2624
: "./gradlew";
27-
instructionPrinter.println(
28-
"Commands", Ansi.Attribute.LIGHT, Ansi.FColor.YELLOW, Ansi.BColor.BLACK);
29-
instructionPrinter.print(String.format("%-40s", gradleCommand + " test"));
30-
commandPrinter.println("Test your application");
31-
instructionPrinter.print(String.format("%-40s", "web3j run"));
32-
commandPrinter.print("Runs your application\n");
33-
instructionPrinter.print(String.format("%-40s", "web3j docker run"));
34-
commandPrinter.print("Runs your application in docker");
25+
26+
System.out.println(Ansi.ansi().fgYellow().bold().a("Commands").reset());
27+
System.out.println(
28+
String.format("%-40s", gradleCommand + " test") + "Test your application");
29+
System.out.println(String.format("%-40s", "web3j run") + "Runs your application");
30+
System.out.println(
31+
String.format("%-40s", "web3j docker run") + "Runs your application in docker");
3532
}
3633

3734
@Override
38-
public void printInstructionsOnSuccessOpenApi(
39-
ColoredPrinter instructionPrinter, ColoredPrinter commandPrinter) {
35+
public void printInstructionsOnSuccessOpenApi() {
4036
String gradleCommand =
4137
System.getProperty("os.name").toLowerCase().startsWith("windows")
4238
? "./gradlew.bat"
4339
: "./gradlew";
44-
System.out.println(System.lineSeparator());
45-
commandPrinter.println("Project Created Successfully");
46-
System.out.println(System.lineSeparator());
4740

48-
instructionPrinter.println(
49-
"Commands", Ansi.Attribute.LIGHT, Ansi.FColor.YELLOW, Ansi.BColor.BLACK);
50-
instructionPrinter.print(String.format("%-40s", gradleCommand + " run"));
51-
commandPrinter.println("Run your application manually");
41+
System.out.println();
42+
System.out.println(Ansi.ansi().fgGreen().a("Project Created Successfully").reset());
43+
System.out.println();
44+
45+
System.out.println(Ansi.ansi().fgYellow().bold().a("Commands").reset());
46+
System.out.println(
47+
String.format("%-40s", gradleCommand + " run") + "Run your application manually");
5248
}
5349
}

src/main/java/org/web3j/console/run/RunCommand.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
import java.nio.file.Paths;
1818

1919
import com.google.common.annotations.VisibleForTesting;
20+
import org.fusesource.jansi.Ansi;
21+
import org.fusesource.jansi.AnsiConsole;
2022

2123
import org.web3j.codegen.Console;
2224
import org.web3j.console.Web3jVersionProvider;
2325

2426
import static org.web3j.console.EnvironmentVariablesProperties.WEB3J_OPENAPI_VAR_PREFIX;
2527
import static org.web3j.console.EnvironmentVariablesProperties.WEB3J_VAR_PREFIX;
26-
import static org.web3j.console.utils.PrinterUtilities.coloredPrinter;
2728
import static org.web3j.console.utils.PrinterUtilities.printErrorAndExit;
2829
import static picocli.CommandLine.Command;
2930
import static picocli.CommandLine.Parameters;
@@ -64,6 +65,10 @@ public class RunCommand implements Runnable {
6465
defaultValue = "")
6566
String walletPassword;
6667

68+
static {
69+
AnsiConsole.systemInstall();
70+
}
71+
6772
private Path workingDirectory = Paths.get(System.getProperty("user.dir"));
6873

6974
@VisibleForTesting
@@ -79,18 +84,22 @@ public void run() {
7984
try {
8085
deploy();
8186
} catch (Exception e) {
82-
throw new RuntimeException(e);
87+
e.printStackTrace();
88+
System.exit(1);
8389
}
8490
}
8591

8692
public void deploy() throws Exception {
87-
coloredPrinter.println("Preparing to run your Web3App");
88-
System.out.print(System.lineSeparator());
89-
coloredPrinter.println("Running your Web3App");
93+
printlnWithColor("Preparing to run your Web3App", Ansi.Color.GREEN);
94+
printlnWithColor("Running your Web3App", Ansi.Color.GREEN);
9095
System.out.print(System.lineSeparator());
9196
runGradle(workingDirectory);
9297
}
9398

99+
private void printlnWithColor(String message, Ansi.Color color) {
100+
System.out.println(Ansi.ansi().fg(color).a(message).reset());
101+
}
102+
94103
private void runGradle(Path runLocation) throws Exception {
95104
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
96105
executeProcess(

src/main/java/org/web3j/console/utils/PrinterUtilities.java

+25-27
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,41 @@
1212
*/
1313
package org.web3j.console.utils;
1414

15-
import com.diogonunes.jcdp.color.ColoredPrinter;
16-
import com.diogonunes.jcdp.color.api.Ansi;
15+
import org.fusesource.jansi.Ansi;
1716

1817
public class PrinterUtilities {
19-
private static final String CARRIAGE_RETURN = "\r";
20-
public static ColoredPrinter coloredPrinter =
21-
new ColoredPrinter.Builder(0, false)
22-
.foreground(Ansi.FColor.WHITE)
23-
.background(Ansi.BColor.GREEN)
24-
.attribute(Ansi.Attribute.BOLD)
25-
.build();
2618

2719
public static void printErrorAndExit(String errorMessage) {
28-
coloredPrinter.println(
29-
errorMessage, Ansi.Attribute.BOLD, Ansi.FColor.RED, Ansi.BColor.NONE);
20+
System.out.println(Ansi.ansi().fgBrightRed().bold().a(errorMessage).reset());
3021
System.exit(1);
3122
}
3223

3324
public static void printInformationPair(
34-
String firstText, int leftJustify, String secondText, Ansi.FColor informationColor) {
35-
coloredPrinter.print(
36-
String.format("%-" + leftJustify + "s", firstText),
37-
Ansi.Attribute.CLEAR,
38-
Ansi.FColor.WHITE,
39-
Ansi.BColor.BLACK);
40-
coloredPrinter.println(
41-
secondText, Ansi.Attribute.CLEAR, informationColor, Ansi.BColor.BLACK);
25+
String firstText, int leftJustify, String secondText, Ansi.Color informationColor) {
26+
System.out.print(
27+
Ansi.ansi()
28+
.fgBrightDefault()
29+
.a(String.format("%-" + leftJustify + "s", firstText))
30+
.reset());
31+
System.out.println(Ansi.ansi().fg(informationColor).a(secondText).reset());
4232
}
4333

4434
public static void printInformationPairWithStatus(
45-
String firstText, int leftJustify, String secondText, Ansi.FColor statusTextColor) {
46-
System.out.print(CARRIAGE_RETURN);
47-
coloredPrinter.print(
48-
String.format("%-" + leftJustify + "s", firstText),
49-
Ansi.Attribute.CLEAR,
50-
Ansi.FColor.WHITE,
51-
Ansi.BColor.BLACK);
52-
coloredPrinter.print(secondText, Ansi.Attribute.CLEAR, statusTextColor, Ansi.BColor.BLACK);
35+
String firstText, int leftJustify, String secondText, Ansi.Color statusTextColor) {
36+
System.out.print(
37+
Ansi.ansi()
38+
.eraseLine()
39+
.fgBrightDefault()
40+
.a(String.format("%-" + leftJustify + "s", firstText))
41+
.reset());
42+
System.out.println(Ansi.ansi().fg(statusTextColor).a(secondText).reset());
43+
}
44+
45+
public static void main(String[] args) {
46+
// Example usage
47+
printErrorAndExit("Error message with exit.");
48+
printInformationPair("Info:", 10, "This is an information message.", Ansi.Color.CYAN);
49+
printInformationPairWithStatus(
50+
"Status:", 10, "This is a status message.", Ansi.Color.GREEN);
5351
}
5452
}

src/main/kotlin/org/web3j/console/openapi/utils/PrettyPrinter.kt

+18-36
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,42 @@
1212
*/
1313
package org.web3j.console.openapi.utils
1414

15-
import com.diogonunes.jcdp.color.ColoredPrinter
16-
import com.diogonunes.jcdp.color.api.Ansi
1715
import org.web3j.console.project.utils.InstructionsPrinter
1816

17+
import org.fusesource.jansi.Ansi
18+
1919
object PrettyPrinter {
20-
private val cp = ColoredPrinter.Builder(0, false)
21-
.foreground(Ansi.FColor.WHITE)
22-
.background(Ansi.BColor.GREEN)
23-
.attribute(Ansi.Attribute.BOLD)
24-
.build()
25-
private val cpf = ColoredPrinter.Builder(0, false)
26-
.foreground(Ansi.FColor.RED)
27-
.background(Ansi.BColor.YELLOW)
28-
.attribute(Ansi.Attribute.BOLD)
29-
.build()
30-
private val instructionPrinter = ColoredPrinter.Builder(0, false).foreground(Ansi.FColor.CYAN).build()
31-
private val commandPrinter = ColoredPrinter.Builder(0, false).foreground(Ansi.FColor.GREEN).build()
3220

3321
fun onOpenApiProjectSuccess() {
3422
InstructionsPrinter.initContextPrinter(null)
35-
InstructionsPrinter.getContextPrinterInstance().contextPrinter.printInstructionsOnSuccessOpenApi(
36-
instructionPrinter, commandPrinter
37-
)
23+
InstructionsPrinter.getContextPrinterInstance().contextPrinter.printInstructionsOnSuccessOpenApi()
3824
}
3925

4026
fun onJarSuccess() {
41-
print(System.lineSeparator())
42-
cp.println("JAR generated Successfully")
43-
print(System.lineSeparator())
27+
println(System.lineSeparator())
28+
println(Ansi.ansi().fgGreen().bold().a("JAR generated Successfully").reset())
29+
println(System.lineSeparator())
4430

45-
instructionPrinter.println(
46-
"Commands", Ansi.Attribute.LIGHT, Ansi.FColor.YELLOW, Ansi.BColor.BLACK
47-
)
48-
instructionPrinter.print(String.format("%-45s", "java -jar <jar_name> <args>"))
49-
commandPrinter.println("Run your Jar")
50-
instructionPrinter.print(String.format("%-45s", "java -jar <jar_name> --help"))
51-
commandPrinter.println("See the available options")
31+
println(Ansi.ansi().fgCyan().a("Commands").reset())
32+
println(String.format("%-45s", "java -jar <jar_name> <args>") + Ansi.ansi().fgGreen().a(" Run your Jar").reset())
33+
println(String.format("%-45s", "java -jar <jar_name> --help") + Ansi.ansi().fgGreen().a(" See the available options").reset())
5234
}
5335

5436
fun onSuccess() {
55-
print(System.lineSeparator())
56-
cp.println("Project generated Successfully")
57-
print(System.lineSeparator())
37+
println(System.lineSeparator())
38+
println(Ansi.ansi().fgGreen().bold().a("Project generated Successfully").reset())
39+
println(System.lineSeparator())
5840
}
5941

6042
fun onFailed() {
61-
print(System.lineSeparator())
62-
cpf.println("Project generation Failed. Check log file for more information.")
63-
print(System.lineSeparator())
43+
println(System.lineSeparator())
44+
println(Ansi.ansi().fgRed().bold().a("Project generation Failed. Check log file for more information.").reset())
45+
println(System.lineSeparator())
6446
}
6547

6648
fun onWrongPath() {
67-
print(System.lineSeparator())
68-
cpf.println("No Solidity smart contracts found! Please enter a correct path containing Solidity code.")
69-
print(System.lineSeparator())
49+
println(System.lineSeparator())
50+
println(Ansi.ansi().fgRed().bold().a("No Solidity smart contracts found! Please enter a correct path containing Solidity code.").reset())
51+
println(System.lineSeparator())
7052
}
7153
}

0 commit comments

Comments
 (0)