Skip to content

Commit b8c3f93

Browse files
committed
Add ColorCommand to change text and background colors using color
See #13
1 parent 15887b0 commit b8c3f93

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ private static void registerCommands(Map<String, Command> commands) {
5151
commands.put("help", new HelpCommand(commands));
5252
commands.put("exit", new ExitCommand());
5353
commands.put("ver", new VersionCommand());
54+
commands.put("color", new ColorCommand());
5455
}
5556
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
public class ColorCommand implements Command {
7+
@Override
8+
public void execute(String[] args, ShellContext context) {
9+
if (args.length == 1) {
10+
String color = args[0];
11+
String background = String.valueOf(color.charAt(0));
12+
String text = String.valueOf(color.charAt(1));
13+
14+
if (background == text) {
15+
return;
16+
}
17+
18+
int bgIndex = Integer.parseInt(background,16);
19+
int fgIndex = Integer.parseInt(text,16);
20+
21+
String[] ansiForeground = {"30","34","32","36","31","35","33","37","90","94","92","96","91","95","93","97"};
22+
String[] ansiBackground = {"40","44","42","46","41","45","43","47","100","104","102","106","101","105","103","107"};
23+
24+
String bg = "\033[" + ansiBackground[bgIndex] + "m";
25+
String fg = "\033[" + ansiForeground[fgIndex] + "m";
26+
27+
System.out.println(bg + fg);
28+
}
29+
30+
else {
31+
// set default color
32+
System.out.println("\033[0m");
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)