Skip to content

Commit

Permalink
Support GrailsConsole to customize category and color of status mes…
Browse files Browse the repository at this point in the history
…sage

Closes gh-765
  • Loading branch information
rainboyan committed Nov 28, 2024
1 parent 454800d commit a112414
Showing 1 changed file with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2023 the original author or authors.
* Copyright 2011-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,6 +56,7 @@
* Utility class for delivering console output in a nicely formatted way.
*
* @author Graeme Rocher
* @author Michael Yan
* @since 2.0
*/
public class GrailsConsole implements ConsoleLogger {
Expand Down Expand Up @@ -618,16 +619,24 @@ public void updateStatus(String msg) {
}

private void outputMessage(String msg, int replaceCount) {
outputMessage(CATEGORY_SEPARATOR, msg, replaceCount);
}

private void outputMessage(String category, String msg, int replaceCount) {
outputMessage(category, msg, Color.YELLOW, replaceCount);
}

private void outputMessage(String category, String msg, Color color, int replaceCount) {
verifySystemOut();
if (msg == null || msg.trim().length() == 0) {
return;
}
try {
if (isAnsiEnabled()) {
if (replaceCount > 0) {
this.out.print(erasePreviousLine(CATEGORY_SEPARATOR));
this.out.print(erasePreviousLine(category));
}
this.lastStatus = outputCategory(Ansi.ansi(), CATEGORY_SEPARATOR)
this.lastStatus = outputCategory(Ansi.ansi(), color, category)
.fg(Color.DEFAULT).a(msg).reset();
this.out.println(this.lastStatus);
if (!this.userInputActive) {
Expand All @@ -643,7 +652,7 @@ private void outputMessage(String msg, int replaceCount) {
this.out.println();
}

this.out.print(CATEGORY_SEPARATOR);
this.out.print(category);
this.out.println(msg);
}
this.lastMessage = msg;
Expand Down Expand Up @@ -674,7 +683,29 @@ private void postPrintMessage() {
*/
@Override
public void addStatus(String msg) {
outputMessage(msg, 0);
addStatus(CATEGORY_SEPARATOR, msg);
}

/**
* Keeps doesn't replace the status message
*
* @param category The category
* @param msg The message
*/
public void addStatus(String category, String msg) {
outputMessage(category, msg, 0);
this.lastMessage = "";
}

/**
* Keeps doesn't replace the status message
*
* @param category The category
* @param msg The message
* @param color The color
*/
public void addStatus(String category, String msg, String color) {
outputMessage(category, msg, Color.valueOf(color), 0);
this.lastMessage = "";
}

Expand Down Expand Up @@ -1003,6 +1034,15 @@ private Ansi outputCategory(Ansi ansi, String categoryName) {
.a(Ansi.Attribute.INTENSITY_BOLD_OFF);
}

private Ansi outputCategory(Ansi ansi, Color color, String categoryName) {
return ansi
.a(Ansi.Attribute.INTENSITY_BOLD)
.fg(color)
.a(categoryName)
.a(SPACE)
.a(Ansi.Attribute.INTENSITY_BOLD_OFF);
}

private Ansi outputErrorLabel(Ansi ansi, String label) {
return ansi
.a(Ansi.Attribute.INTENSITY_BOLD)
Expand Down

0 comments on commit a112414

Please sign in to comment.