Step 1: Define an enum to represent colors.
ShellPromptColor.java
package com.sample.app.enums;
public enum ShellPromptColor {
BLACK(0), RED(1), GREEN(2), YELLOW(3), BLUE(4), MAGENTA(5), CYAN(6), WHITE(7), BRIGHT(8);
private final int value;
ShellPromptColor(int value) {
this.value = value;
}
public int toJlineAttributedStyle() {
return this.value;
}
}
Step 2: Add following properties in ‘application.properties’ file.
shell.out.info=CYAN shell.out.success=GREEN shell.out.warning=YELLOW shell.out.error=RED
Step 3: Define ShellOutputHelper class.
ShellOutputHelper.java
package com.sample.app.customizations;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedStringBuilder;
import org.jline.utils.AttributedStyle;
import org.springframework.beans.factory.annotation.Value;
import com.sample.app.enums.ShellPromptColor;
public class ShellOutputHelper {
@Value("${shell.out.info}")
public String infoColor;
@Value("${shell.out.success}")
public String successColor;
@Value("${shell.out.warning}")
public String warningColor;
@Value("${shell.out.error}")
public String errorColor;
private Terminal terminal;
public ShellOutputHelper(Terminal terminal) {
this.terminal = terminal;
}
public String getColored(String message, ShellPromptColor color) {
return (new AttributedStringBuilder())
.append(message, AttributedStyle.DEFAULT.foreground(color.toJlineAttributedStyle())).toAnsi();
}
public String getInfoMessage(String message) {
return getColored(message, ShellPromptColor.valueOf(infoColor));
}
public String getSuccessMessage(String message) {
return getColored(message, ShellPromptColor.valueOf(successColor));
}
public String getWarningMessage(String message) {
return getColored(message, ShellPromptColor.valueOf(warningColor));
}
public String getErrorMessage(String message) {
return getColored(message, ShellPromptColor.valueOf(errorColor));
}
}
Step 4: Define a bean of ShellHelper class.
SpringShellConfig.java
package com.sample.app.config;
import org.jline.terminal.Terminal;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import com.sample.app.customizations.ShellOutputHelper;
@Configuration
public class SpringShellConfig {
@Bean
public ShellOutputHelper shellHelper(@Lazy Terminal terminal) {
return new ShellOutputHelper(terminal);
}
}
Step 5: Define ‘CustomizeColorCommand’ like below.
CustomizeColorCommand.java
package com.sample.app.commmands;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import org.springframework.shell.standard.ShellOption;
import com.sample.app.customizations.ShellOutputHelper;
@ShellComponent
public class CustomizeColorCommand {
@Autowired
ShellOutputHelper shellHelper;
@ShellMethod("Displays greeting message to the user whose name is supplied")
public String beautifyMe(@ShellOption({ "-N", "--name" }) String name) {
String output = shellHelper.getSuccessMessage(String.format("Hello %s", name));
return output.concat(", How are you?");
}
}
Run the application and execute the command ‘beautify-me Krishna’, you will see that the message ‘Hello Krishna’ is beautified in green color.
No comments:
Post a Comment