소스 검색

Merge pull request #617 from csmith/tidying

Add ugly event-based whois output.
pull/618/head
Greg Holmes 8 년 전
부모
커밋
174ad646b3

+ 6
- 0
res/com/dmdirc/ui/messages/format.yml 파일 보기

@@ -208,6 +208,12 @@ CommandOutputEvent:
208 208
 UnknownCommandEvent:
209 209
   format: "Unknown command {{command}}."
210 210
   colour: 14
211
+UserInfoResponseEvent:
212
+  before: "---------- User info for {{user.nickname}} ----------"
213
+  after: "--------- End of info for {{user.nickname}} ---------"
214
+  iterate: "entries"
215
+  format: "{{key}}: {{value}}"
216
+  colour: 10
211 217
 
212 218
 ################## TODO ############################################################################
213 219
 #  selfCTCP=4->- [%1$s] %2$s

+ 12
- 1
src/com/dmdirc/events/UserInfoResponseEvent.java 파일 보기

@@ -26,6 +26,7 @@ import com.dmdirc.interfaces.Connection;
26 26
 import com.dmdirc.interfaces.User;
27 27
 import com.dmdirc.parser.events.UserInfoEvent;
28 28
 
29
+import java.util.Collection;
29 30
 import java.util.EnumMap;
30 31
 import java.util.Map;
31 32
 import java.util.Optional;
@@ -33,7 +34,7 @@ import java.util.Optional;
33 34
 /**
34 35
  * Event raised when detailed user info has been received for a user.
35 36
  */
36
-public class UserInfoResponseEvent extends ServerEvent {
37
+public class UserInfoResponseEvent extends ServerDisplayableEvent {
37 38
 
38 39
     private final User user;
39 40
     private final Map<UserInfoEvent.UserInfoType, String> info;
@@ -64,4 +65,14 @@ public class UserInfoResponseEvent extends ServerEvent {
64 65
     public Optional<String> getInfo(final UserInfoEvent.UserInfoType type) {
65 66
         return Optional.ofNullable(info.get(type));
66 67
     }
68
+
69
+    /**
70
+     * Gets a collection of all info entries in the response.
71
+     *
72
+     * @return A collection of all user info entries.
73
+     */
74
+    public Collection<Map.Entry<UserInfoEvent.UserInfoType, String>> getEntries() {
75
+        return info.entrySet();
76
+    }
77
+
67 78
 }

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

@@ -40,6 +40,8 @@ public abstract class EventFormat {
40 40
     public abstract Optional<String> getBeforeTemplate();
41 41
     /** The template to use after finishing rendering the event. */
42 42
     public abstract Optional<String> getAfterTemplate();
43
+    /** The property that should be iterated over, if the event contains multiple lines. */
44
+    public abstract Optional<String> getIterateProperty();
43 45
 
44 46
     // TODO: This should probably be a generic set of properties.
45 47
     public abstract Optional<Colour> getDefaultForegroundColour();
@@ -48,8 +50,9 @@ public abstract class EventFormat {
48 50
             final String template,
49 51
             final Optional<String> beforeTemplate,
50 52
             final Optional<String> afterTemplate,
53
+            final Optional<String> iterateProperty,
51 54
             final Optional<Colour> defaultForegroundColour) {
52
-        return new AutoValue_EventFormat(template, beforeTemplate, afterTemplate,
55
+        return new AutoValue_EventFormat(template, beforeTemplate, afterTemplate, iterateProperty,
53 56
                 defaultForegroundColour);
54 57
     }
55 58
 

+ 45
- 20
src/com/dmdirc/ui/messages/EventFormatter.java 파일 보기

@@ -65,34 +65,59 @@ public class EventFormatter {
65 65
                     .ifPresent(c -> event.setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, c));
66 66
         }
67 67
 
68
-        return format.map(f -> {
69
-            final StringBuilder builder = getTemplate(f, event);
70
-            int tagStart = builder.indexOf("{{");
71
-            while (tagStart > -1) {
72
-                final int tagEnd = builder.indexOf("}}", tagStart);
73
-                final String tag = builder.substring(tagStart + 2, tagEnd);
74
-                final String replacement = getReplacement(event, tag);
75
-                builder.replace(tagStart, tagEnd + 2, replacement);
76
-                tagStart = builder.indexOf("{{", tagStart + replacement.length());
77
-            }
78
-
79
-            return builder.toString();
80
-        });
68
+        return format.map(f -> format(f, event));
81 69
     }
82 70
 
83
-    private StringBuilder getTemplate(final EventFormat format, final DisplayableEvent event) {
71
+    private String format(final EventFormat format, final DisplayableEvent event) {
84 72
         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;
73
+        format.getBeforeTemplate().ifPresent(
74
+                before -> builder.append(doSubstitutions(event, before)).append('\n'));
75
+        builder.append(
76
+                format.getIterateProperty()
77
+                        .map(iterate -> formatIterable(event, iterate, format.getTemplate()))
78
+                        .orElseGet(() -> doSubstitutions(event, format.getTemplate())));
79
+        format.getAfterTemplate().ifPresent(
80
+                after -> builder.append('\n').append(doSubstitutions(event, after)));
81
+        return builder.toString();
82
+    }
83
+
84
+    private String doSubstitutions(final Object dataSource, final String line) {
85
+        final StringBuilder builder = new StringBuilder(line);
86
+        int tagStart = builder.indexOf("{{");
87
+        while (tagStart > -1) {
88
+            final int tagEnd = builder.indexOf("}}", tagStart);
89
+            final String tag = builder.substring(tagStart + 2, tagEnd);
90
+            final String replacement = getReplacement(dataSource, tag);
91
+            builder.replace(tagStart, tagEnd + 2, replacement);
92
+            tagStart = builder.indexOf("{{", tagStart + replacement.length());
93
+        }
94
+        return builder.toString();
95
+    }
96
+
97
+    private String formatIterable(final DisplayableEvent event, final String property,
98
+            final String template) {
99
+        final Optional<Object> value
100
+                = propertyManager.getProperty(event, event.getClass(), property);
101
+        if (!value.isPresent() || !(value.get() instanceof Iterable<?>)) {
102
+            return ERROR_STRING;
103
+        }
104
+        @SuppressWarnings("unchecked")
105
+        final Iterable<Object> collection = (Iterable<Object>) value.get();
106
+        final StringBuilder res = new StringBuilder();
107
+        for (Object line : collection) {
108
+            if (res.length() > 0) {
109
+                res.append('\n');
110
+            }
111
+            res.append(doSubstitutions(line, template));
112
+        }
113
+        return res.toString();
89 114
     }
90 115
 
91
-    private String getReplacement(final DisplayableEvent event, final String tag) {
116
+    private String getReplacement(final Object dataSource, final String tag) {
92 117
         final String[] functionParts = tag.split("\\|");
93 118
         final String[] dataParts = functionParts[0].split("\\.");
94 119
 
95
-        Object target = event;
120
+        Object target = dataSource;
96 121
         for (String part : dataParts) {
97 122
             final Optional<Object> result =
98 123
                     propertyManager.getProperty(target, target.getClass(), part);

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

@@ -99,11 +99,15 @@ public class YamlEventFormatProvider implements EventFormatProvider {
99 99
         final Optional<String> afterTemplate = info.containsKey("after")
100 100
                 ? Optional.of(info.get("after").toString())
101 101
                 : Optional.empty();
102
+        final Optional<String> iterateProperty = info.containsKey("iterate")
103
+                ? Optional.of(info.get("iterate").toString())
104
+                : Optional.empty();
102 105
         final Optional<Colour> foregroundColour = info.containsKey("colour")
103 106
                 ? Optional.of(colourManager.getColourFromIrcCode(
104 107
                         Integer.parseInt(info.get("colour").toString())))
105 108
                 : Optional.empty();
106
-        return EventFormat.create(template, beforeTemplate, afterTemplate, foregroundColour);
109
+        return EventFormat.create(
110
+                template, beforeTemplate, afterTemplate, iterateProperty, foregroundColour);
107 111
     }
108 112
 
109 113
     @Override

+ 4
- 0
test/com/dmdirc/ui/messages/EventFormatterTest.java 파일 보기

@@ -60,6 +60,7 @@ public class EventFormatterTest {
60 60
                                 "Template {{channel}} meep",
61 61
                                 Optional.empty(),
62 62
                                 Optional.empty(),
63
+                                Optional.empty(),
63 64
                                 Optional.empty())));
64 65
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
65 66
                 .thenReturn(Optional.of("MONKEY"));
@@ -77,6 +78,7 @@ public class EventFormatterTest {
77 78
                                 "Template {{channel}} meep",
78 79
                                 Optional.of("Before!"),
79 80
                                 Optional.of("After!"),
81
+                                Optional.empty(),
80 82
                                 Optional.empty())));
81 83
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
82 84
                 .thenReturn(Optional.of("MONKEY"));
@@ -96,6 +98,7 @@ public class EventFormatterTest {
96 98
                                 "Template {{channel|lowercase}} meep",
97 99
                                 Optional.empty(),
98 100
                                 Optional.empty(),
101
+                                Optional.empty(),
99 102
                                 Optional.empty())));
100 103
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "channel"))
101 104
                 .thenReturn(Optional.of("MONKEY"));
@@ -114,6 +117,7 @@ public class EventFormatterTest {
114 117
                                 "Template {{message}} meep",
115 118
                                 Optional.empty(),
116 119
                                 Optional.empty(),
120
+                                Optional.empty(),
117 121
                                 Optional.empty())));
118 122
         when(propertyManager.getProperty(messageEvent, ChannelMessageEvent.class, "message"))
119 123
                 .thenReturn(Optional.of("{{channel}}"));

Loading…
취소
저장