-
-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Probably Trailing new lines not supported/Line Break Issue #51
Comments
As assumed, the code did not work for both examples. It also has problems, if the newline to be output is not the newline of the current jre/os, which can occure in a multi-platform environment, where for example messages are logged into files with a fix new line for all os. The code below solves the three issues and can replace the existing method in class Ansi: // Windows, Unix or old Classic Mac OS
static Pattern newLinePattern = Pattern.compile("((\\r?\\n)|\\r)", Pattern.MULTILINE);
static StringBuilder output = new StringBuilder();
static public String colorize(String text, String ansiCode) {
output.setLength(0);
// assure at least one newline for logic simplicity
text = text + "\n";
Matcher newLineMatcher = newLinePattern.matcher(text);
int nextLineBegin = 0;
String line = null;
String newLine = null;
while (newLineMatcher.find()) {
line = text.substring(nextLineBegin, newLineMatcher.start());
// determine the exact new line (which can be different from line to line)
// to assure identical new lines output
newLine = text.substring(newLineMatcher.start(), newLineMatcher.end());
if (! line.isEmpty())
{
output.append(ansiCode);
output.append(line);
output.append(RESET);
// ELSE: no text => no ansiCode output => no RESET => keep effect of last RESET
}
// Not at last line?
if (newLineMatcher.end() != text.length()) {
// yes, there is more to come, output new line
output.append(newLine);
nextLineBegin = newLineMatcher.end();
}
}
return output.toString();
} If a line is empty, the code neither sets attributes nor resets the terminal. The last state is keept to reduce output, because last reset (of none-empty line output) still is active. If this assumption is wrong, just remove IF to make reset unconditional again. I assure this code was written by me and only by me and I contribute it under MIT License to the project. |
Apologies for my silence in the last days, I've been struggling with some personal issues. I'll try to review your issue and code asap. If you want, you can create a PR in the meantime, that will speed up the review process, you will also be listed as a contributor to this project :) |
I have a PR that solves the bug you found. I tested your code and it worked, however I found it too complex and hard to understand. I achieved the same thing just by changing a few lines. I'll push this in a new release (v5.0.1) |
The replace function always uses the same (plattform specific) NEWLINE. I think the original text passed to colorize should only be attributed by ansiCode, but kept as it is. If you are using different NEWLINEs in a string or consistently a fix NEWLINE, which is not the plattform-specific NEWLINE the result is not what I expect. The source of text to be attributed maybe is not under control of the caller and the text is just received as it is. |
I browsed the code on github and think colorize(String text, String ansiCode) will not handle trailing new lines correctly, because split will not return them. See https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)
" ... Trailing empty strings are therefore not included in the resulting array. ... "
I think the code gets in trouble with:
"a\n\n\n" because split will result in ["a"]
"a\n\n\b" because endsWithLine is set to false and therefore no new lines are added in the loop
The text was updated successfully, but these errors were encountered: