소스 검색

Add formatter support for before/after templates.

This allows formats that may be multi-line (whois results,
probably MOTD results at some point, etc...) to show a header
and footer around the actual content.
pull/616/head
Chris Smith 8 년 전
부모
커밋
1767fa4e99

+ 9
- 1
src/com/dmdirc/ui/messages/EventFormat.java 파일 보기

@@ -34,15 +34,23 @@ import java.util.Optional;
34 34
 @AutoValue
35 35
 public abstract class EventFormat {
36 36
 
37
+    /** The template to use when rendering the event. */
37 38
     public abstract String getTemplate();
39
+    /** The template to use before starting to render the event. */
40
+    public abstract Optional<String> getBeforeTemplate();
41
+    /** The template to use after finishing rendering the event. */
42
+    public abstract Optional<String> getAfterTemplate();
38 43
 
39 44
     // TODO: This should probably be a generic set of properties.
40 45
     public abstract Optional<Colour> getDefaultForegroundColour();
41 46
 
42 47
     public static EventFormat create(
43 48
             final String template,
49
+            final Optional<String> beforeTemplate,
50
+            final Optional<String> afterTemplate,
44 51
             final Optional<Colour> defaultForegroundColour) {
45
-        return new AutoValue_EventFormat(template, defaultForegroundColour);
52
+        return new AutoValue_EventFormat(template, beforeTemplate, afterTemplate,
53
+                defaultForegroundColour);
46 54
     }
47 55
 
48 56
 }

+ 9
- 1
src/com/dmdirc/ui/messages/EventFormatter.java 파일 보기

@@ -66,7 +66,7 @@ public class EventFormatter {
66 66
         }
67 67
 
68 68
         return format.map(f -> {
69
-            final StringBuilder builder = new StringBuilder(f.getTemplate());
69
+            final StringBuilder builder = getTemplate(f, event);
70 70
             int tagStart = builder.indexOf("{{");
71 71
             while (tagStart > -1) {
72 72
                 final int tagEnd = builder.indexOf("}}", tagStart);
@@ -80,6 +80,14 @@ public class EventFormatter {
80 80
         });
81 81
     }
82 82
 
83
+    private StringBuilder getTemplate(final EventFormat format, final DisplayableEvent event) {
84
+        final StringBuilder builder = new StringBuilder();
85
+        format.getBeforeTemplate().ifPresent(before -> builder.append(before).append('\n'));
86
+        builder.append(format.getTemplate());
87
+        format.getAfterTemplate().ifPresent(after -> builder.append('\n').append(after));
88
+        return builder;
89
+    }
90
+
83 91
     private String getReplacement(final DisplayableEvent event, final String tag) {
84 92
         final String[] functionParts = tag.split("\\|");
85 93
         final String[] dataParts = functionParts[0].split("\\.");

+ 7
- 1
src/com/dmdirc/ui/messages/YamlEventFormatProvider.java 파일 보기

@@ -93,11 +93,17 @@ public class YamlEventFormatProvider implements EventFormatProvider {
93 93
     private EventFormat readFormat(final Object format) {
94 94
         final Map<Object, Object> info = asMap(format);
95 95
         final String template = info.get("format").toString();
96
+        final Optional<String> beforeTemplate = info.containsKey("before")
97
+                ? Optional.of(info.get("before").toString())
98
+                : Optional.empty();
99
+        final Optional<String> afterTemplate = info.containsKey("after")
100
+                ? Optional.of(info.get("after").toString())
101
+                : Optional.empty();
96 102
         final Optional<Colour> foregroundColour = info.containsKey("colour")
97 103
                 ? Optional.of(colourManager.getColourFromIrcCode(
98 104
                         Integer.parseInt(info.get("colour").toString())))
99 105
                 : Optional.empty();
100
-        return EventFormat.create(template, foregroundColour);
106
+        return EventFormat.create(template, beforeTemplate, afterTemplate, foregroundColour);
101 107
     }
102 108
 
103 109
     @Override

Loading…
취소
저장