From 277d1b980110d3022a2a8fb8d46f14a9bf3d59f2 Mon Sep 17 00:00:00 2001 From: Greg Schueler Date: Fri, 17 Nov 2017 12:32:57 -0800 Subject: [PATCH] log: bannerResource can supply replacement map --- .../com/simplifyops/toolbelt/ToolBelt.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/toolbelt/src/main/java/com/simplifyops/toolbelt/ToolBelt.java b/toolbelt/src/main/java/com/simplifyops/toolbelt/ToolBelt.java index 34ec4b4..b569d8c 100644 --- a/toolbelt/src/main/java/com/simplifyops/toolbelt/ToolBelt.java +++ b/toolbelt/src/main/java/com/simplifyops/toolbelt/ToolBelt.java @@ -8,6 +8,8 @@ import java.nio.file.Files; import java.util.*; import java.util.function.Supplier; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -221,13 +223,33 @@ public ToolBelt banner(String text) { * @return this */ public ToolBelt bannerResource(String resource) { + return bannerResource(resource, null); + } + + /** + * Display banner for top level help + * + * @param resource resource path + * @param replacements a map of Regex->replacement, to replace values in the loaded resource + * + * @return this + */ + public ToolBelt bannerResource(String resource, Map replacements) { this.commands.banner = () -> { InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(resource); if (null != resourceAsStream) { try { + String result; try (BufferedReader is = new BufferedReader(new InputStreamReader(resourceAsStream))) { - return is.lines().collect(Collectors.joining("\n")); + result = is.lines().collect(Collectors.joining("\n")); + } + if (replacements != null && !replacements.isEmpty()) { + for (String s : replacements.keySet()) { + String val = replacements.get(s); + result = result.replaceAll(s, Matcher.quoteReplacement(val)); + } } + return result; } catch (IOException e) { }