Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/main/java/de/rub/nds/scanner/core/report/ColorEncoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@
package de.rub.nds.scanner.core.report;

import de.rub.nds.scanner.core.probe.result.TestResult;
import de.rub.nds.scanner.core.report.markup.SemanticMarkup;
import java.util.HashMap;

/**
* Handles color encoding for test results in terminal output. Maps test results to ANSI colors for
* visual differentiation.
* Handles color encoding for test results in terminal output. Maps test results to Semantic colors
* for visual differentiation.
*/
public class ColorEncoding {

private final HashMap<TestResult, AnsiColor> colorMap;
private final HashMap<TestResult, SemanticMarkup> colorMap;

/**
* Creates a new ColorEncoding with the specified result-to-color mapping.
*
* @param colorMap a HashMap mapping TestResult instances to AnsiColor codes
* @param colorMap a HashMap mapping TestResult instances to SemanticMarkup codes
*/
public ColorEncoding(HashMap<TestResult, AnsiColor> colorMap) {
public ColorEncoding(HashMap<TestResult, SemanticMarkup> colorMap) {
this.colorMap = colorMap;
}

/**
* Returns the AnsiColor associated with a given test result.
* Returns the SemanticMarkup associated with a given test result.
*
* @param result the test result
* @return the associated AnsiColor, or null if no mapping exists
* @return the associated SemanticMarkup, or null if no mapping exists
*/
public AnsiColor getColor(TestResult result) {
public SemanticMarkup getColor(TestResult result) {
return colorMap.get(result);
}

Expand All @@ -47,9 +48,9 @@ public AnsiColor getColor(TestResult result) {
* @return the color-encoded text string
*/
public String encode(TestResult result, String encodedText) {
AnsiColor color = this.getColor(result);
if (color != null && color != AnsiColor.DEFAULT_COLOR) {
return color.getCode() + encodedText + AnsiColor.RESET.getCode();
SemanticMarkup color = this.getColor(result);
if (color != null && color != SemanticMarkup.NEUTRAL) {
return color.applyAnsi(encodedText);
} else {
return encodedText;
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/de/rub/nds/scanner/core/report/PrintingScheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import de.rub.nds.scanner.core.probe.AnalyzedProperty;
import de.rub.nds.scanner.core.probe.AnalyzedPropertyCategory;
import de.rub.nds.scanner.core.probe.result.TestResult;
import de.rub.nds.scanner.core.report.markup.SemanticMarkup;
import java.util.HashMap;

/**
Expand Down Expand Up @@ -139,9 +140,9 @@ public String getEncodedKeyText(ScanReport report, AnalyzedProperty property) {
*
* @param report the scan report containing the result
* @param property the property whose result color should be determined
* @return the ANSI color to use for the result value
* @return the color to use for the result value
*/
public AnsiColor getValueColor(ScanReport report, AnalyzedProperty property) {
public SemanticMarkup getValueColor(ScanReport report, AnalyzedProperty property) {
TestResult result = report.getResult(property);
ColorEncoding colorEncoding =
valueColorEncodings.getOrDefault(property, defaultColorEncoding);
Expand All @@ -154,9 +155,9 @@ public AnsiColor getValueColor(ScanReport report, AnalyzedProperty property) {
*
* @param report the scan report (currently unused)
* @param property the property (currently unused)
* @return the default ANSI color
* @return the default color
*/
public AnsiColor getKeyColor(ScanReport report, AnalyzedProperty property) {
return AnsiColor.DEFAULT_COLOR;
public SemanticMarkup getKeyColor(ScanReport report, AnalyzedProperty property) {
return SemanticMarkup.NEUTRAL;
}
}
12 changes: 7 additions & 5 deletions src/main/java/de/rub/nds/scanner/core/report/ReportCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import de.rub.nds.scanner.core.report.container.KeyValueContainer;
import de.rub.nds.scanner.core.report.container.ReportContainer;
import de.rub.nds.scanner.core.report.container.TextContainer;
import de.rub.nds.scanner.core.report.markup.Markup;
import de.rub.nds.scanner.core.report.markup.SemanticMarkup;

/**
* Base class for creating report containers from scan results. Provides utility methods for
Expand Down Expand Up @@ -47,8 +49,8 @@ public ReportCreator(ScannerDetail detail, PrintingScheme scheme) {
protected ReportContainer createKeyValueContainer(AnalyzedProperty property, ReportT report) {
String key = printingScheme.getEncodedKeyText(report, property);
String value = printingScheme.getEncodedValueText(report, property);
AnsiColor keyColour = printingScheme.getKeyColor(report, property);
AnsiColor valueColour = printingScheme.getValueColor(report, property);
Markup keyColour = printingScheme.getValueColor(report, property);
Markup valueColour = printingScheme.getValueColor(report, property);
return new KeyValueContainer(key, keyColour, value, valueColour);
}

Expand All @@ -60,7 +62,7 @@ protected ReportContainer createKeyValueContainer(AnalyzedProperty property, Rep
* @return a key-value container with default colors
*/
protected ReportContainer createDefaultKeyValueContainer(String key, String value) {
return new KeyValueContainer(key, AnsiColor.DEFAULT_COLOR, value, AnsiColor.DEFAULT_COLOR);
return new KeyValueContainer(key, SemanticMarkup.NEUTRAL, value, SemanticMarkup.NEUTRAL);
}

/**
Expand All @@ -72,7 +74,7 @@ protected ReportContainer createDefaultKeyValueContainer(String key, String valu
*/
protected ReportContainer createDefaultKeyHexValueContainer(String key, String value) {
return new KeyValueContainer(
key, AnsiColor.DEFAULT_COLOR, "0x" + value, AnsiColor.DEFAULT_COLOR);
key, SemanticMarkup.NEUTRAL, "0x" + value, SemanticMarkup.NEUTRAL);
}

/**
Expand All @@ -82,6 +84,6 @@ protected ReportContainer createDefaultKeyHexValueContainer(String key, String v
* @return a text container with default color
*/
protected TextContainer createDefaultTextContainer(String text) {
return new TextContainer(text, AnsiColor.DEFAULT_COLOR);
return new TextContainer(text, SemanticMarkup.NEUTRAL);
}
}
113 changes: 53 additions & 60 deletions src/main/java/de/rub/nds/scanner/core/report/ReportPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

import de.rub.nds.scanner.core.config.ScannerDetail;
import de.rub.nds.scanner.core.probe.AnalyzedProperty;
import de.rub.nds.scanner.core.report.markup.Markup;
import de.rub.nds.scanner.core.report.markup.MarkupUtil;
import de.rub.nds.scanner.core.report.markup.SemanticMarkup;

/**
* Abstract base class for generating formatted text representations of scan reports. Provides
Expand Down Expand Up @@ -72,9 +75,12 @@ protected String getBlackString(String value, String format) {
* @return the formatted string with optional green color, "Unknown" if value is null
*/
protected String getGreenString(String value, String format) {
return (printColorful ? AnsiColor.GREEN.getCode() : AnsiColor.RESET.getCode())
+ String.format(format, value == null ? "Unknown" : value)
+ AnsiColor.RESET.getCode();
String formatted = String.format(format, value == null ? "Unknown" : value);
if (printColorful) {
return SemanticMarkup.RESULT_GOOD.applyAnsi(formatted);
} else {
return formatted;
}
}

/**
Expand All @@ -85,9 +91,12 @@ protected String getGreenString(String value, String format) {
* @return the formatted string with optional yellow color, "Unknown" if value is null
*/
protected String getYellowString(String value, String format) {
return (printColorful ? AnsiColor.YELLOW.getCode() : AnsiColor.RESET.getCode())
+ String.format(format, value == null ? "Unknown" : value)
+ AnsiColor.RESET.getCode();
String formatted = String.format(format, value == null ? "Unknown" : value);
if (printColorful) {
return SemanticMarkup.RESULT_MEDIUM.applyAnsi(formatted);
} else {
return formatted;
}
}

/**
Expand All @@ -98,9 +107,12 @@ protected String getYellowString(String value, String format) {
* @return the formatted string with optional red color, "Unknown" if value is null
*/
protected String getRedString(String value, String format) {
return (printColorful ? AnsiColor.RED.getCode() : AnsiColor.RESET.getCode())
+ String.format(format, value == null ? "Unknown" : value)
+ AnsiColor.RESET.getCode();
String formatted = String.format(format, value == null ? "Unknown" : value);
if (printColorful) {
return SemanticMarkup.RESULT_BAD.applyAnsi(formatted);
} else {
return formatted;
}
}

/**
Expand All @@ -122,13 +134,12 @@ protected StringBuilder prettyAppend(StringBuilder builder, String value) {
* @param color the color to apply if colors are enabled
* @return the builder for method chaining
*/
protected StringBuilder prettyAppend(StringBuilder builder, String value, AnsiColor color) {
if (printColorful) {
builder.append(color.getCode());
}
builder.append(value);
protected StringBuilder prettyAppend(
StringBuilder builder, String value, SemanticMarkup color) {
if (printColorful) {
builder.append(AnsiColor.RESET.getCode());
color.applyAnsi(builder, value);
} else {
builder.append(value);
}
builder.append("\n");
return builder;
Expand Down Expand Up @@ -221,7 +232,7 @@ protected StringBuilder prettyAppend(
* @return the builder for method chaining
*/
protected StringBuilder prettyAppend(
StringBuilder builder, String name, Boolean value, AnsiColor color) {
StringBuilder builder, String name, Boolean value, SemanticMarkup color) {
return prettyAppend(builder, name, String.valueOf(value), color);
}

Expand All @@ -235,14 +246,12 @@ protected StringBuilder prettyAppend(
* @return the builder for method chaining
*/
protected StringBuilder prettyAppend(
StringBuilder builder, String name, String value, AnsiColor color) {
StringBuilder builder, String name, String value, SemanticMarkup color) {
builder.append(addIndentations(name)).append(": ");
if (printColorful) {
builder.append(color.getCode());
}
builder.append(value);
if (printColorful) {
builder.append(AnsiColor.RESET.getCode());
color.applyAnsi(builder, value);
} else {
builder.append(value);
}
builder.append("\n");
return builder;
Expand All @@ -258,14 +267,13 @@ protected StringBuilder prettyAppend(
protected StringBuilder prettyAppendHeading(StringBuilder builder, String value) {
depth = 0;

return builder.append(
printColorful
? AnsiColor.BOLD.getCode() + AnsiColor.BLUE.getCode()
: AnsiColor.RESET.getCode())
.append("\n------------------------------------------------------------\n")
.append(value)
.append("\n\n")
.append(AnsiColor.RESET.getCode());
Markup headingMarkup =
printColorful ? SemanticMarkup.REPORT_STRUCTURE_HEADING : MarkupUtil.NONE;
return headingMarkup.applyAnsi(
builder,
"\n------------------------------------------------------------\n",
value,
"\n\n");
}

/**
Expand All @@ -275,7 +283,9 @@ protected StringBuilder prettyAppendHeading(StringBuilder builder, String value)
* @param name the property name
* @param value the property value
* @return the builder for method chaining
* @deprecated No replacement currently planned, if you use this, contact us.
*/
@Deprecated(forRemoval = true)
protected StringBuilder prettyAppendUnderlined(
StringBuilder builder, String name, String value) {
return builder.append(addIndentations(name))
Expand All @@ -294,7 +304,9 @@ protected StringBuilder prettyAppendUnderlined(
* @param name the property name
* @param value the boolean value
* @return the builder for method chaining
* @deprecated No replacement currently planned, if you use this, contact us.
*/
@Deprecated(forRemoval = true)
protected StringBuilder prettyAppendUnderlined(
StringBuilder builder, String name, boolean value) {
return builder.append(addIndentations(name))
Expand All @@ -313,7 +325,9 @@ protected StringBuilder prettyAppendUnderlined(
* @param name the property name
* @param value the long value
* @return the builder for method chaining
* @deprecated No replacement currently planned, if you use this, contact us.
*/
@Deprecated(forRemoval = true)
protected StringBuilder prettyAppendUnderlined(StringBuilder builder, String name, long value) {
return builder.append(addIndentations(name))
.append(": ")
Expand All @@ -333,16 +347,9 @@ protected StringBuilder prettyAppendUnderlined(StringBuilder builder, String nam
*/
protected StringBuilder prettyAppendSubheading(StringBuilder builder, String name) {
depth = 1;
return builder.append("--|")
.append(
printColorful
? AnsiColor.BOLD.getCode()
+ AnsiColor.PURPLE.getCode()
+ AnsiColor.UNDERLINE.getCode()
+ name
+ "\n\n"
+ AnsiColor.RESET.getCode()
: name + "\n\n");
Markup markup =
printColorful ? SemanticMarkup.REPORT_STRUCTURE_SUBHEADINGS : MarkupUtil.NONE;
return markup.applyAnsi(builder, "--|", name, "\n\n");
}

/**
Expand All @@ -354,16 +361,9 @@ protected StringBuilder prettyAppendSubheading(StringBuilder builder, String nam
*/
protected StringBuilder prettyAppendSubSubheading(StringBuilder builder, String name) {
depth = 2;
return builder.append("----|")
.append(
printColorful
? AnsiColor.BOLD.getCode()
+ AnsiColor.PURPLE.getCode()
+ AnsiColor.UNDERLINE.getCode()
+ name
+ "\n\n"
+ AnsiColor.RESET.getCode()
: name + "\n\n");
Markup markup =
printColorful ? SemanticMarkup.REPORT_STRUCTURE_SUBHEADINGS : MarkupUtil.NONE;
return markup.applyAnsi(builder, "----|", name, "\n\n");
}

/**
Expand All @@ -375,16 +375,9 @@ protected StringBuilder prettyAppendSubSubheading(StringBuilder builder, String
*/
protected StringBuilder prettyAppendSubSubSubheading(StringBuilder builder, String name) {
depth = 3;
return builder.append("------|")
.append(
printColorful
? AnsiColor.BOLD.getCode()
+ AnsiColor.PURPLE.getCode()
+ AnsiColor.UNDERLINE.getCode()
+ name
+ "\n\n"
+ AnsiColor.RESET.getCode()
: name + "\n\n");
Markup markup =
printColorful ? SemanticMarkup.REPORT_STRUCTURE_SUBHEADINGS : MarkupUtil.NONE;
return markup.applyAnsi(builder, "------|", name, "\n\n");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import de.rub.nds.scanner.core.config.ScannerDetail;
import de.rub.nds.scanner.core.report.AnsiColor;
import de.rub.nds.scanner.core.report.markup.SemanticMarkup;

/**
* Container for displaying headlines in scanner reports. Supports different depth levels with
Expand Down Expand Up @@ -53,8 +54,7 @@ public HeadlineContainer(String headline, ScannerDetail detail) {
@Override
public void print(StringBuilder builder, int depth, boolean useColor) {
if (useColor) {
builder.append(AnsiColor.BOLD.getCode());
builder.append(getColorByDepth(depth).getCode());
builder.append(getColorByDepth(depth).getAnsiCode());
}
if (depth == 0) {
addHLine(builder);
Expand All @@ -68,18 +68,12 @@ public void print(StringBuilder builder, int depth, boolean useColor) {
builder.append("\n");
}

private static AnsiColor getColorByDepth(int depth) {
private static SemanticMarkup getColorByDepth(int depth) {
switch (depth) {
case 0:
return AnsiColor.PURPLE;
case 1:
return AnsiColor.BLUE;
case 2:
return AnsiColor.CYAN;
case 3:
return AnsiColor.WHITE;
return SemanticMarkup.REPORT_STRUCTURE_HEADING;
default:
return AnsiColor.YELLOW;
return SemanticMarkup.REPORT_STRUCTURE_SUBHEADINGS;
}
}

Expand Down
Loading