From 417e707cf1aec6427d6db618a956b5d18f2ec326 Mon Sep 17 00:00:00 2001 From: Philippe Charles Date: Tue, 9 Apr 2024 14:09:42 +0200 Subject: [PATCH] Fix stylish list formatting --- .../java/internal/heylogs/StylishFormat.java | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/heylogs-api/src/main/java/internal/heylogs/StylishFormat.java b/heylogs-api/src/main/java/internal/heylogs/StylishFormat.java index 4569e25..d3c2023 100644 --- a/heylogs-api/src/main/java/internal/heylogs/StylishFormat.java +++ b/heylogs-api/src/main/java/internal/heylogs/StylishFormat.java @@ -10,10 +10,8 @@ import nbbrd.service.ServiceProvider; import java.io.IOException; -import java.util.ArrayList; -import java.util.EnumSet; -import java.util.List; -import java.util.Set; +import java.util.*; +import java.util.function.Function; import static java.lang.System.lineSeparator; import static java.util.Locale.ROOT; @@ -47,16 +45,14 @@ public final class StylishFormat implements Format { @Override public void formatProblems(@NonNull Appendable appendable, @NonNull List list) throws IOException { - StylishWriter writer = StylishWriter + StylishWriter .builder() .column(getPositionFormatter(list)) .column(getRuleSeverityFormatter()) .column(Formatter.of(problem -> problem.getIssue().getMessage())) .column(Formatter.of(Problem::getId)) - .build(); - for (Check item : list) { - writer.write(appendable, item.getSource(), item.getProblems(), getProblemsSummary(item.getProblems())); - } + .build() + .writeAll(appendable, list, Check::getSource, Check::getProblems, item -> getProblemsSummary(item.getProblems())); } @MightBePromoted @@ -100,13 +96,11 @@ private String getProblemsSummary(List list) { @Override public void formatStatus(@NonNull Appendable appendable, @NonNull List list) throws IOException { - StylishWriter writer = StylishWriter + StylishWriter .builder() .column(Formatter.onString()) - .build(); - for (Scan item : list) { - writer.write(appendable, item.getSource(), getStatusBody(item.getSummary()), null); - } + .build() + .writeAll(appendable, list, Scan::getSource, item -> getStatusBody(item.getSummary()), ignore -> null); } private List getStatusBody(Summary summary) { @@ -165,6 +159,20 @@ private static class StylishWriter { @lombok.Singular List> columns; + public void writeAll(Appendable appendable, List list, + Function header, Function> body, Function footer) throws IOException { + Iterator iterator = list.iterator(); + if (iterator.hasNext()) { + final X first = iterator.next(); + write(appendable, header.apply(first), body.apply(first), footer.apply(first)); + while (iterator.hasNext()) { + appendable.append(separator); + X next = iterator.next(); + write(appendable, header.apply(next), body.apply(next), footer.apply(next)); + } + } + } + public void write(Appendable appendable, CharSequence header, List body, CharSequence footer) throws IOException { writeHeader(appendable, header); writeBody(appendable, body);