-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0287144
commit 1d9b03d
Showing
2 changed files
with
112 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/io/jenkins/plugins/checks/api/TruncatedString.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.jenkins.plugins.checks.api; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TruncatedString { | ||
|
||
private final List<CharSequence> chunks; | ||
private final String readMoreLink; | ||
|
||
/** | ||
* Create a {@link TruncatedString} with the provided limit and truncation message. | ||
* | ||
* @param truncatedMessage the message to be appended should maxSize be exceeded, e.g. | ||
* "Some output is not shown here, see more on [Jenkins](url)." | ||
*/ | ||
private TruncatedString(final String readMoreLink, final List<CharSequence> chunks) { | ||
this.readMoreLink = readMoreLink; | ||
this.chunks = chunks; | ||
} | ||
|
||
static TruncatedString fromString(String string) { | ||
return new TruncatedString.Builder().addText(string).build(); | ||
} | ||
|
||
public static class Builder { | ||
private String readMoreLink; | ||
private final List<CharSequence> chunks = new ArrayList<>(); | ||
|
||
public TruncatedString build() { | ||
return new TruncatedString(readMoreLink, chunks); | ||
} | ||
|
||
public Builder withReadMoreLink(String readMoreLink) { | ||
this.readMoreLink = readMoreLink; | ||
return this; | ||
} | ||
|
||
public Builder addText(CharSequence chunk) { | ||
this.chunks.add(chunk); | ||
return this; | ||
} | ||
|
||
} | ||
|
||
public static abstract class Truncator { | ||
|
||
protected final int maxSize; | ||
|
||
public Truncator(int maxSize) { | ||
this.maxSize = maxSize; | ||
} | ||
|
||
public abstract String getReadMoreText(TruncatedString truncatedString); | ||
|
||
public String truncate(TruncatedString truncatedString) { | ||
String readMoreText = getReadMoreText(truncatedString); | ||
if (truncatedString.chunks.size() == 0) { | ||
return null; | ||
} | ||
StringBuilder builder = new StringBuilder(); | ||
for (CharSequence chunk: truncatedString.chunks) { | ||
if (builder.length() + chunk.length() + readMoreText.length() < maxSize) { | ||
builder.append(chunk); | ||
} else { | ||
builder.append(readMoreText); | ||
break; | ||
} | ||
} | ||
return builder.toString(); | ||
} | ||
} | ||
|
||
/* example implementation */ | ||
public static class GitHubTruncator extends Truncator { | ||
|
||
public GitHubTruncator() { | ||
super(65536); | ||
} | ||
|
||
@Override | ||
public String getReadMoreText(TruncatedString truncatedString) { | ||
return String.format("%n%nOutput truncated - [more information](%s)", truncatedString.readMoreLink); | ||
} | ||
} | ||
|
||
public String build() { | ||
return StringUtils.join(chunks); | ||
} | ||
|
||
} |