Browse Source

Merge pull request #722 from csmith/styliser

Move static styliser methods to new class.
pull/723/head
Greg Holmes 7 years ago
parent
commit
a84a298d96

+ 5
- 8
src/main/java/com/dmdirc/Channel.java View File

44
 import com.dmdirc.ui.core.components.WindowComponent;
44
 import com.dmdirc.ui.core.components.WindowComponent;
45
 import com.dmdirc.ui.input.TabCompletionType;
45
 import com.dmdirc.ui.input.TabCompletionType;
46
 import com.dmdirc.ui.messages.BackBufferFactory;
46
 import com.dmdirc.ui.messages.BackBufferFactory;
47
-import com.dmdirc.ui.messages.Styliser;
48
-
47
+import com.dmdirc.ui.messages.StyledMessageUtils;
49
 import com.google.common.collect.EvictingQueue;
48
 import com.google.common.collect.EvictingQueue;
50
-
51
 import java.util.ArrayList;
49
 import java.util.ArrayList;
52
 import java.util.Arrays;
50
 import java.util.Arrays;
53
 import java.util.Collection;
51
 import java.util.Collection;
56
 import java.util.Optional;
54
 import java.util.Optional;
57
 import java.util.Queue;
55
 import java.util.Queue;
58
 import java.util.stream.Collectors;
56
 import java.util.stream.Collectors;
59
-
60
 import javax.annotation.Nullable;
57
 import javax.annotation.Nullable;
61
 
58
 
62
 /**
59
 /**
98
             final GroupChatUserManager groupChatUserManager) {
95
             final GroupChatUserManager groupChatUserManager) {
99
         super("channel-inactive",
96
         super("channel-inactive",
100
                 newChannelInfo.getName(),
97
                 newChannelInfo.getName(),
101
-                Styliser.stipControlCodes(newChannelInfo.getName()),
98
+                new StyledMessageUtils().stripControlCodes(newChannelInfo.getName()), // TODO: Inject this
102
                 configMigrator.getConfigProvider(),
99
                 configMigrator.getConfigProvider(),
103
                 backBufferFactory,
100
                 backBufferFactory,
104
                 connection.getWindowModel().getEventBus(),
101
                 connection.getWindowModel().getEventBus(),
220
      * Updates the title of the channel window, and of the main window if appropriate.
217
      * Updates the title of the channel window, and of the main window if appropriate.
221
      */
218
      */
222
     private void updateTitle() {
219
     private void updateTitle() {
223
-        String temp = Styliser.stipControlCodes(channelInfo.getName());
220
+        final StyledMessageUtils styleUtils = new StyledMessageUtils();
224
 
221
 
222
+        String temp = styleUtils.stripControlCodes(channelInfo.getName());
225
         if (!channelInfo.getTopic().isEmpty()) {
223
         if (!channelInfo.getTopic().isEmpty()) {
226
-            temp += " - " + Styliser.stipControlCodes(channelInfo.getTopic());
224
+            temp += " - " + styleUtils.stripControlCodes(channelInfo.getTopic());
227
         }
225
         }
228
-
229
         setTitle(temp);
226
         setTitle(temp);
230
     }
227
     }
231
 
228
 

+ 2
- 1
src/main/java/com/dmdirc/ui/messages/IRCLine.java View File

35
     private final String timestamp;
35
     private final String timestamp;
36
     private final String text;
36
     private final String text;
37
     private final Styliser styliser;
37
     private final Styliser styliser;
38
+    private final StyledMessageUtils styleUtils = new StyledMessageUtils(); // TODO: Inject
38
     private final DisplayPropertyMap displayProperties;
39
     private final DisplayPropertyMap displayProperties;
39
     private int fontSize;
40
     private int fontSize;
40
     private String fontName;
41
     private String fontName;
94
 
95
 
95
     @Override
96
     @Override
96
     public String getText() {
97
     public String getText() {
97
-        return Styliser.stipControlCodes(timestamp + text);
98
+        return styleUtils.stripControlCodes(timestamp + text);
98
     }
99
     }
99
 
100
 
100
     @Override
101
     @Override

+ 100
- 0
src/main/java/com/dmdirc/ui/messages/StyledMessageUtils.java View File

1
+package com.dmdirc.ui.messages;
2
+
3
+import javax.inject.Inject;
4
+import javax.inject.Singleton;
5
+
6
+import static com.dmdirc.ui.messages.Styliser.CODE_CHANNEL;
7
+import static com.dmdirc.ui.messages.Styliser.CODE_HYPERLINK;
8
+import static com.dmdirc.ui.messages.Styliser.CODE_NICKNAME;
9
+import static com.dmdirc.ui.messages.Styliser.CODE_SMILIE;
10
+import static com.dmdirc.ui.messages.Styliser.CODE_TOOLTIP;
11
+import static com.google.common.base.Preconditions.checkArgument;
12
+
13
+/**
14
+ * Utilities for dealing with styled messages.
15
+ */
16
+@Singleton
17
+public class StyledMessageUtils {
18
+
19
+    @Inject
20
+    public StyledMessageUtils() {
21
+    }
22
+
23
+    /**
24
+     * Strips all recognised control codes from the input string.
25
+     *
26
+     * @param input the String to be stripped
27
+     *
28
+     * @return a copy of the input with control codes removed
29
+     */
30
+    public String stripControlCodes(final String input) {
31
+        return input.replaceAll("[" + IRCControlCodes.BOLD + CODE_CHANNEL + IRCControlCodes.FIXED
32
+                + CODE_HYPERLINK + IRCControlCodes.ITALIC + IRCControlCodes.NEGATE + CODE_NICKNAME
33
+                + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + "]|"
34
+                + IRCControlCodes.COLOUR_HEX + "([A-Za-z0-9]{6}(,[A-Za-z0-9]{6})?)?|"
35
+                + IRCControlCodes.COLOUR + "([0-9]{1,2}(,[0-9]{1,2})?)?", "")
36
+                .replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)" + CODE_TOOLTIP, "$1");
37
+    }
38
+
39
+    /**
40
+     * Retrieves the styled String contained within the unstyled offsets specified. That is, the
41
+     * <code>from</code> and <code>to</code> arguments correspond to indexes in an unstyled version
42
+     * of the <code>styled</code> string. The unstyled indices are translated to offsets within the
43
+     * styled String, and the return value includes all text and control codes between those
44
+     * indices.
45
+     * <p>
46
+     * The index translation is left-biased; that is, the indices are translated to be as far left
47
+     * as they possibly can be. This means that the start of the string will include any control
48
+     * codes immediately preceding the desired text, and the end will not include any trailing
49
+     * codes.
50
+     * <p>
51
+     * This method will NOT include "internal" control codes in the output.
52
+     *
53
+     * @param styled The styled String to be operated on
54
+     * @param from   The starting index in the unstyled string
55
+     * @param to     The ending index in the unstyled string
56
+     *
57
+     * @return The corresponding text between the two indices
58
+     */
59
+    public String getStyledText(final String styled, final int from, final int to) {
60
+        checkArgument(from < to, "'from' (" + from + ") must be less than 'to' (" + to + ')');
61
+        checkArgument(from >= 0, "'from' (" + from + ") must be non-negative");
62
+
63
+        final String unstyled = stripControlCodes(styled);
64
+
65
+        checkArgument(to <= unstyled.length(), "'to' (" + to + ") must be less than or equal to "
66
+                + "the unstyled length (" + unstyled.length() + ')');
67
+
68
+        final String startBit = unstyled.substring(0, from);
69
+        final String middleBit = unstyled.substring(from, to);
70
+        final String sanitised = stripInternalControlCodes(styled);
71
+        int start = from;
72
+
73
+        while (!stripControlCodes(sanitised.substring(0, start)).equals(startBit)) {
74
+            start++;
75
+        }
76
+
77
+        int end = to + start - from;
78
+
79
+        while (!stripControlCodes(sanitised.substring(start, end)).equals(middleBit)) {
80
+            end++;
81
+        }
82
+
83
+        return sanitised.substring(start, end);
84
+    }
85
+
86
+    /**
87
+     * Strips all recognised internal control codes from the input string.
88
+     *
89
+     * @param input the String to be stripped
90
+     *
91
+     * @return a copy of the input with control codes removed
92
+     */
93
+    private String stripInternalControlCodes(final String input) {
94
+        return input.replaceAll("[" + CODE_CHANNEL + CODE_HYPERLINK + CODE_NICKNAME
95
+                + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + ']', "")
96
+                .replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)"
97
+                        + CODE_TOOLTIP, "$1");
98
+    }
99
+
100
+}

+ 10
- 3
src/main/java/com/dmdirc/ui/messages/Styliser.java View File

22
 
22
 
23
 package com.dmdirc.ui.messages;
23
 package com.dmdirc.ui.messages;
24
 
24
 
25
-import static com.google.common.base.Preconditions.checkArgument;
26
-
27
 import com.dmdirc.interfaces.Connection;
25
 import com.dmdirc.interfaces.Connection;
28
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
26
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29
 import com.dmdirc.interfaces.config.ConfigChangeListener;
27
 import com.dmdirc.interfaces.config.ConfigChangeListener;
30
 import com.dmdirc.util.colours.Colour;
28
 import com.dmdirc.util.colours.Colour;
29
+import com.google.common.annotations.VisibleForTesting;
31
 import java.util.Locale;
30
 import java.util.Locale;
32
 import java.util.regex.Pattern;
31
 import java.util.regex.Pattern;
33
 import javax.annotation.Nullable;
32
 import javax.annotation.Nullable;
34
 
33
 
34
+import static com.google.common.base.Preconditions.checkArgument;
35
+
35
 /**
36
 /**
36
  * The styliser applies IRC styles to text. Styles are indicated by various control codes which are
37
  * The styliser applies IRC styles to text. Styles are indicated by various control codes which are
37
  * a de-facto IRC standard.
38
  * a de-facto IRC standard.
195
      * @return The corresponding text between the two indices
196
      * @return The corresponding text between the two indices
196
      *
197
      *
197
      * @since 0.6.3
198
      * @since 0.6.3
199
+     * @deprecated Use {@link StyledMessageUtils}
198
      */
200
      */
201
+    @Deprecated
199
     public static String getStyledText(final String styled, final int from, final int to) {
202
     public static String getStyledText(final String styled, final int from, final int to) {
200
         checkArgument(from < to, "'from' (" + from + ") must be less than 'to' (" + to + ')');
203
         checkArgument(from < to, "'from' (" + from + ") must be less than 'to' (" + to + ')');
201
         checkArgument(from >= 0, "'from' (" + from + ") must be non-negative");
204
         checkArgument(from >= 0, "'from' (" + from + ") must be non-negative");
289
      * @param input the String to be stripped
292
      * @param input the String to be stripped
290
      *
293
      *
291
      * @return a copy of the input with control codes removed
294
      * @return a copy of the input with control codes removed
295
+     * @deprecated Use {@link StyledMessageUtils}
292
      */
296
      */
297
+    @Deprecated
293
     public static String stipControlCodes(final String input) {
298
     public static String stipControlCodes(final String input) {
294
         return input.replaceAll("[" + IRCControlCodes.BOLD + CODE_CHANNEL + IRCControlCodes.FIXED
299
         return input.replaceAll("[" + IRCControlCodes.BOLD + CODE_CHANNEL + IRCControlCodes.FIXED
295
                 + CODE_HYPERLINK + IRCControlCodes.ITALIC + IRCControlCodes.NEGATE + CODE_NICKNAME
300
                 + CODE_HYPERLINK + IRCControlCodes.ITALIC + IRCControlCodes.NEGATE + CODE_NICKNAME
308
      *
313
      *
309
      * @since 0.6.5
314
      * @since 0.6.5
310
      */
315
      */
316
+    @Deprecated
311
     private static String stipInternalControlCodes(final String input) {
317
     private static String stipInternalControlCodes(final String input) {
312
         return input.replaceAll("[" + CODE_CHANNEL + CODE_HYPERLINK + CODE_NICKNAME
318
         return input.replaceAll("[" + CODE_CHANNEL + CODE_HYPERLINK + CODE_NICKNAME
313
                 + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + ']', "")
319
                 + CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + ']', "")
324
      *
330
      *
325
      * @return A substring of the input containing no control characters
331
      * @return A substring of the input containing no control characters
326
      */
332
      */
327
-    public static String readUntilControl(final String input) {
333
+    @VisibleForTesting
334
+    static String readUntilControl(final String input) {
328
         int pos = input.length();
335
         int pos = input.length();
329
 
336
 
330
         pos = checkChar(pos, input.indexOf(IRCControlCodes.BOLD));
337
         pos = checkChar(pos, input.indexOf(IRCControlCodes.BOLD));

+ 58
- 0
src/test/java/com/dmdirc/ui/messages/StyledMessageUtilsTest.java View File

1
+/*
2
+ * Copyright (c) 2006-2016 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.ui.messages;
24
+
25
+
26
+import org.junit.Before;
27
+import org.junit.Test;
28
+
29
+import static org.junit.Assert.assertEquals;
30
+
31
+public class StyledMessageUtilsTest {
32
+
33
+    private StyledMessageUtils styleUtils;
34
+
35
+    @Before
36
+    public void setUp() {
37
+        styleUtils = new StyledMessageUtils();
38
+    }
39
+
40
+    @Test
41
+    public void testStripControlCodes1() {
42
+        final String input = "This"+ (char) 2 +" is "+ (char) 17 +"a test";
43
+
44
+        final String expResult = "This is a test";
45
+        final String result = styleUtils.stripControlCodes(input);
46
+        assertEquals(expResult, result);
47
+    }
48
+
49
+    @Test
50
+    public void testStripControlCodes2() {
51
+        final String input = "This is "+ (char) 3 +"5a "+ (char) 4 +"FF0000test";
52
+
53
+        final String expResult = "This is a test";
54
+        final String result = styleUtils.stripControlCodes(input);
55
+        assertEquals(expResult, result);
56
+    }
57
+
58
+}

+ 3
- 3
src/test/java/com/dmdirc/ui/messages/StyliserIndicesTest.java View File

34
 @RunWith(Parameterized.class)
34
 @RunWith(Parameterized.class)
35
 public class StyliserIndicesTest {
35
 public class StyliserIndicesTest {
36
 
36
 
37
-    protected String input, output;
38
-    protected int start, end;
37
+    private String input, output;
38
+    private int start, end;
39
 
39
 
40
     public StyliserIndicesTest(final String input, final int start, final int end, final String output) {
40
     public StyliserIndicesTest(final String input, final int start, final int end, final String output) {
41
         this.input = input;
41
         this.input = input;
46
 
46
 
47
     @Test
47
     @Test
48
     public void testStyle() {
48
     public void testStyle() {
49
-        assertEquals(output, Styliser.getStyledText(input, start, end));
49
+        assertEquals(output, new StyledMessageUtils().getStyledText(input, start, end));
50
     }
50
     }
51
 
51
 
52
     @Parameterized.Parameters
52
     @Parameterized.Parameters

+ 0
- 18
src/test/java/com/dmdirc/ui/messages/StyliserTest.java View File

39
 
39
 
40
     @Mock private EventBus eventBus;
40
     @Mock private EventBus eventBus;
41
 
41
 
42
-    @Test
43
-    public void testStripControlCodes1() {
44
-        final String input = "This"+ (char) 2 +" is "+ (char) 17 +"a test";
45
-
46
-        final String expResult = "This is a test";
47
-        final String result = Styliser.stipControlCodes(input);
48
-        assertEquals(expResult, result);
49
-    }
50
-
51
-    @Test
52
-    public void testStripControlCodes2() {
53
-        final String input = "This is "+ (char) 3 +"5a "+ (char) 4 +"FF0000test";
54
-
55
-        final String expResult = "This is a test";
56
-        final String result = Styliser.stipControlCodes(input);
57
-        assertEquals(expResult, result);
58
-    }
59
-
60
     @Test
42
     @Test
61
     public void testReadUntilControl1() {
43
     public void testReadUntilControl1() {
62
         final String input = "This"+ (char) 2 +" is "+ (char) 17 +"a test";
44
         final String input = "This"+ (char) 2 +" is "+ (char) 17 +"a test";

Loading…
Cancel
Save