ソースを参照

Merge pull request #616 from csmith/tidying

Add formatter support for before/after templates.
pull/617/head
Greg Holmes 8年前
コミット
ba5baf2aa9

+ 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

+ 33
- 3
test/com/dmdirc/ui/messages/EventFormatterTest.java ファイルの表示

@@ -56,20 +56,46 @@ public class EventFormatterTest {
56 56
 
57 57
         when(templateProvider.getFormat(ChannelMessageEvent.class))
58 58
                 .thenReturn(Optional.of(
59
-                        EventFormat.create("Template {{channel}} meep", Optional.empty())));
59
+                        EventFormat.create(
60
+                                "Template {{channel}} meep",
61
+                                Optional.empty(),
62
+                                Optional.empty(),
63
+                                Optional.empty())));
60 64
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
61 65
                 .thenReturn(Optional.of("MONKEY"));
62 66
 
63 67
         assertEquals("Template MONKEY meep", formatter.format(messageEvent).orElse(null));
64 68
     }
65 69
 
70
+    @Test
71
+    public void testBeforeAndAfterFormat() {
72
+        messageEvent = new ChannelMessageEvent(channel, null, null);
73
+
74
+        when(templateProvider.getFormat(ChannelMessageEvent.class))
75
+                .thenReturn(Optional.of(
76
+                        EventFormat.create(
77
+                                "Template {{channel}} meep",
78
+                                Optional.of("Before!"),
79
+                                Optional.of("After!"),
80
+                                Optional.empty())));
81
+        when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
82
+                .thenReturn(Optional.of("MONKEY"));
83
+
84
+        assertEquals(
85
+                "Before!\nTemplate MONKEY meep\nAfter!",
86
+                formatter.format(messageEvent).orElse(null));
87
+    }
88
+
66 89
     @Test
67 90
     public void testFormatWithFunction() {
68 91
         messageEvent = new ChannelMessageEvent(channel, null, null);
69 92
 
70 93
         when(templateProvider.getFormat(ChannelMessageEvent.class))
71 94
                 .thenReturn(Optional.of(
72
-                        EventFormat.create("Template {{channel|lowercase}} meep",
95
+                        EventFormat.create(
96
+                                "Template {{channel|lowercase}} meep",
97
+                                Optional.empty(),
98
+                                Optional.empty(),
73 99
                                 Optional.empty())));
74 100
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
75 101
                 .thenReturn(Optional.of("MONKEY"));
@@ -84,7 +110,11 @@ public class EventFormatterTest {
84 110
 
85 111
         when(templateProvider.getFormat(ChannelMessageEvent.class))
86 112
                 .thenReturn(Optional.of(
87
-                        EventFormat.create("Template {{message}} meep", Optional.empty())));
113
+                        EventFormat.create(
114
+                                "Template {{message}} meep",
115
+                                Optional.empty(),
116
+                                Optional.empty(),
117
+                                Optional.empty())));
88 118
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "message"))
89 119
                 .thenReturn(Optional.of("{{channel}}"));
90 120
 

読み込み中…
キャンセル
保存