Skip to content

Commit 56707fc

Browse files
committed
💬style: update CLI style and fix some bugs for executing cmd
1 parent 6cb8b3e commit 56707fc

File tree

10 files changed

+274
-41
lines changed

10 files changed

+274
-41
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ dependencies {
7575
compile group: 'io.atomix.copycat', name: 'copycat-client', version: '1.1.4'
7676
compile group: 'io.atomix.catalyst', name: 'catalyst-netty', version: '1.1.1'
7777
compile group: 'net.jcip', name: 'jcip-annotations', version: '1.0'
78+
79+
compile group: 'org.fusesource.jansi', name: 'jansi', version: '1.16'
7880
}
7981

8082
tasks.matching { it instanceof Test }.all {

src/main/java/org/tron/command/AccountCommand.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.tron.peer.Peer;
1818
import org.tron.utils.ByteArray;
1919

20+
import static org.fusesource.jansi.Ansi.ansi;
21+
2022
public class AccountCommand extends Command {
2123
public AccountCommand() {
2224
}
@@ -29,9 +31,17 @@ public void execute(Peer peer, String[] parameters) {
2931
@Override
3032
public void usage() {
3133
System.out.println("");
32-
System.out.println("USAGE [account]:");
33-
System.out.println("Command: account");
34-
System.out.println("Description: Get your account.");
34+
35+
System.out.println( ansi().eraseScreen().render(
36+
"@|magenta,bold USAGE|@\n\t@|bold account|@"
37+
) );
38+
39+
System.out.println("");
40+
41+
System.out.println( ansi().eraseScreen().render(
42+
"@|magenta,bold DESCRIPTION|@\n\t@|bold The command 'account' get your wallet address.|@"
43+
) );
44+
3545
System.out.println("");
3646
}
3747

src/main/java/org/tron/command/Cli.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,24 @@ public void run(Peer peer) {
3232

3333
String[] cmdArray = cmd.split("\\s+");
3434

35-
if (cmdArray.length == 0) {
35+
if (cmdArray.length == 0 || cmdArray[0].equals("")) {
3636
continue;
3737
}
3838

3939
String[] cmdParameters = Arrays.copyOfRange(cmdArray, 1, cmdArray.length);
4040

4141
switch (cmdArray[0]) {
42-
case "exit":
43-
case "quit":
44-
case "bye":
45-
new ExitCommand().execute(peer, cmdParameters);
42+
case "version":
43+
new VersionCommand().execute(peer, cmdParameters);
4644
break;
47-
case "send":
48-
new SendCommand().execute(peer, cmdParameters);
45+
case "account":
46+
new AccountCommand().execute(peer, cmdParameters);
4947
break;
5048
case "getbalance":
5149
new GetBalanceCommand().execute(peer, cmdParameters);
5250
break;
53-
case "account":
54-
new AccountCommand().execute(peer, cmdParameters);
51+
case "send":
52+
new SendCommand().execute(peer, cmdParameters);
5553
break;
5654
case "printblockchain":
5755
new PrintBlockchainCommand().execute(peer, cmdParameters);
@@ -65,6 +63,11 @@ public void run(Peer peer) {
6563
case "putmessage":
6664
new ConsensusCommand().putClient(cmdParameters);
6765
break;
66+
case "exit":
67+
case "quit":
68+
case "bye":
69+
new ExitCommand().execute(peer, cmdParameters);
70+
break;
6871
case "help":
6972
default:
7073
new HelpCommand().execute(peer, cmdParameters);

src/main/java/org/tron/command/ConsensusCommand.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.tron.consensus.client.Client;
1818
import org.tron.consensus.server.Server;
1919

20+
import static org.fusesource.jansi.Ansi.ansi;
21+
2022
public class ConsensusCommand {
2123

2224
public ConsensusCommand() {
@@ -37,21 +39,41 @@ public void getClient(String[] args) {
3739

3840
public void usage() {
3941
System.out.println("");
40-
System.out.println("consensus server");
41-
System.out.println("Command: consensus");
42-
System.out.println("Description: Create a server.");
42+
43+
System.out.println( ansi().eraseScreen().render(
44+
"@|magenta,bold USAGE|@\n\t@|bold consensus|@"
45+
) );
46+
47+
System.out.println("");
48+
49+
System.out.println( ansi().eraseScreen().render(
50+
"@|magenta,bold DESCRIPTION|@\n\t@|bold The command 'consensus' create a server.|@"
51+
) );
52+
4353
System.out.println("");
4454

55+
System.out.println( ansi().eraseScreen().render(
56+
"@|magenta,bold USAGE|@\n\t@|bold getmessage [key]|@"
57+
) );
58+
4559
System.out.println("");
46-
System.out.println("getData Message");
47-
System.out.println("Command: getmessage [key]");
48-
System.out.println("Description: Get consensus Message");
60+
61+
System.out.println( ansi().eraseScreen().render(
62+
"@|magenta,bold DESCRIPTION|@\n\t@|bold The command 'getmessage' get a consensus message.|@"
63+
) );
64+
4965
System.out.println("");
5066

67+
System.out.println( ansi().eraseScreen().render(
68+
"@|magenta,bold USAGE|@\n\t@|bold putmessage [key] [value]|@"
69+
) );
70+
5171
System.out.println("");
52-
System.out.println("putData Message");
53-
System.out.println("Command: putmessage [key] [value]");
54-
System.out.println("Description: Put a consensus Message");
72+
73+
System.out.println( ansi().eraseScreen().render(
74+
"@|magenta,bold DESCRIPTION|@\n\t@|bold The command 'putmessage' put a consensus message.|@"
75+
) );
76+
5577
System.out.println("");
5678
}
5779

src/main/java/org/tron/command/ExitCommand.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
import org.tron.peer.Peer;
1818

19+
import static org.fusesource.jansi.Ansi.Color.MAGENTA;
20+
import static org.fusesource.jansi.Ansi.Color.RED;
21+
import static org.fusesource.jansi.Ansi.ansi;
22+
1923
public class ExitCommand extends Command {
2024
public ExitCommand() {
2125
}
@@ -28,9 +32,17 @@ public void execute(Peer peer, String[] parameters) {
2832
@Override
2933
public void usage() {
3034
System.out.println("");
31-
System.out.println("USAGE [exit]:");
32-
System.out.println("Command: exit | quit | bye");
33-
System.out.println("Description: Exit the program.");
35+
36+
System.out.println( ansi().eraseScreen().render(
37+
"@|magenta,bold USAGE|@\n\t@|bold exit|@"
38+
) );
39+
40+
System.out.println("");
41+
42+
System.out.println( ansi().eraseScreen().render(
43+
"@|magenta,bold DESCRIPTION|@\n\t@|bold The command 'exit' exit java-tron application.|@"
44+
) );
45+
3446
System.out.println("");
3547
}
3648

src/main/java/org/tron/command/GetBalanceCommand.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.util.ArrayList;
2121

22+
import static org.fusesource.jansi.Ansi.ansi;
23+
2224
public class GetBalanceCommand extends Command {
2325
public GetBalanceCommand() {
2426
}
@@ -40,9 +42,17 @@ public void execute(Peer peer, String[] parameters) {
4042
@Override
4143
public void usage() {
4244
System.out.println("");
43-
System.out.println("USAGE [getbalance]:");
44-
System.out.println("Command: getbalance");
45-
System.out.println("Description: Get your balance.");
45+
46+
System.out.println( ansi().eraseScreen().render(
47+
"@|magenta,bold USAGE|@\n\t@|bold getbalance|@"
48+
) );
49+
50+
System.out.println("");
51+
52+
System.out.println( ansi().eraseScreen().render(
53+
"@|magenta,bold DESCRIPTION|@\n\t@|bold The command 'getbalance' get your balance.|@"
54+
) );
55+
4656
System.out.println("");
4757
}
4858

src/main/java/org/tron/command/HelpCommand.java

Lines changed: 97 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,115 @@
1414
*/
1515
package org.tron.command;
1616

17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
1719
import org.tron.peer.Peer;
1820

21+
import static org.fusesource.jansi.Ansi.ansi;
22+
1923
public class HelpCommand extends Command {
2024
public HelpCommand() {
2125
}
2226

2327
@Override
2428
public void execute(Peer peer, String[] parameters) {
25-
new ExitCommand().usage();
26-
new SendCommand().usage();
27-
new GetBalanceCommand().usage();
28-
new AccountCommand().usage();
29-
new PrintBlockchainCommand().usage();
30-
new ConsensusCommand().usage();
29+
if (parameters.length == 0) {
30+
usage();
31+
return;
32+
}
33+
34+
switch (parameters[0]) {
35+
case "version":
36+
new VersionCommand().usage();
37+
break;
38+
case "account":
39+
new AccountCommand().usage();
40+
break;
41+
case "getbalance":
42+
new GetBalanceCommand().usage();
43+
break;
44+
case "send":
45+
new SendCommand().usage();
46+
break;
47+
case "printblockchain":
48+
new PrintBlockchainCommand().usage();
49+
break;
50+
case "consensus":
51+
case "getmessage":
52+
case "putmessage":
53+
new ConsensusCommand().usage();
54+
break;
55+
case "exit":
56+
case "quit":
57+
case "bye":
58+
new ExitCommand().usage();
59+
break;
60+
case "help":
61+
default:
62+
new HelpCommand().usage();
63+
break;
64+
}
3165
}
3266

3367
@Override
3468
public void usage() {
35-
System.out.printf("USAGE [help]");
36-
System.out.println("help");
69+
System.out.println("");
70+
71+
System.out.println( ansi().eraseScreen().render(
72+
"@|magenta,bold USAGE|@\n\t@|bold help [arguments]|@"
73+
) );
74+
75+
System.out.println("");
76+
77+
System.out.println( ansi().eraseScreen().render(
78+
"@|magenta,bold AVAILABLE COMMANDS|@"
79+
) );
80+
81+
System.out.println("");
82+
83+
System.out.println( ansi().eraseScreen().render(
84+
"\t@|bold version\t\tPrint the current java-tron version|@"
85+
) );
86+
87+
System.out.println( ansi().eraseScreen().render(
88+
"\t@|bold account\t\tGet your wallet address|@"
89+
) );
90+
91+
System.out.println( ansi().eraseScreen().render(
92+
"\t@|bold getbalance\t\tGet your balance|@"
93+
) );
94+
95+
System.out.println( ansi().eraseScreen().render(
96+
"\t@|bold send\t\tSend balance to receiver address|@"
97+
) );
98+
99+
System.out.println( ansi().eraseScreen().render(
100+
"\t@|bold printblockchain\t\tPrint blockchain|@"
101+
) );
102+
103+
System.out.println( ansi().eraseScreen().render(
104+
"\t@|bold consensus\t\tCreate a server|@"
105+
) );
106+
107+
System.out.println( ansi().eraseScreen().render(
108+
"\t@|bold getmessage\t\tGet a consensus message|@"
109+
) );
110+
111+
System.out.println( ansi().eraseScreen().render(
112+
"\t@|bold putmessage\t\tPut a consensus message|@"
113+
) );
114+
115+
System.out.println( ansi().eraseScreen().render(
116+
"\t@|bold exit\t\tExit java-tron application|@"
117+
) );
118+
119+
System.out.println("");
120+
121+
System.out.println( ansi().eraseScreen().render(
122+
"Use @|bold help [topic] for more information about that topic.|@"
123+
) );
124+
125+
System.out.println("");
37126
}
38127

39128
@Override

src/main/java/org/tron/command/PrintBlockchainCommand.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.tron.peer.Peer;
2121
import org.tron.protos.core.TronBlock;
2222

23+
import static org.fusesource.jansi.Ansi.ansi;
24+
2325
public class PrintBlockchainCommand extends Command {
2426
public PrintBlockchainCommand() {
2527
}
@@ -37,9 +39,17 @@ public void execute(Peer peer, String[] parameters) {
3739
@Override
3840
public void usage() {
3941
System.out.println("");
40-
System.out.println("USAGE [printblockchain]:");
41-
System.out.println("Command: printblockchain");
42-
System.out.println("Description: Print Blockchain.");
42+
43+
System.out.println( ansi().eraseScreen().render(
44+
"@|magenta,bold USAGE|@\n\t@|bold printblockchain|@"
45+
) );
46+
47+
System.out.println("");
48+
49+
System.out.println( ansi().eraseScreen().render(
50+
"@|magenta,bold DESCRIPTION|@\n\t@|bold The command 'printblockchain' print blockchain.|@"
51+
) );
52+
4353
System.out.println("");
4454
}
4555

0 commit comments

Comments
 (0)