Browse Source

Core support for lines with arbitrary timestamps

Fixed echo command not respecting silence characters when using --target or
--active(!). Added --ts command to echo command to fake the timestamp.

mIRC tab completion and /echo --ts <tab> ftw.

Fixes issue 4090

Change-Id: Ia236ae81e38f0387d15e8798128dbb1dd846c5af
Reviewed-on: http://gerrit.dmdirc.com/1203
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4rc1
Chris Smith 14 years ago
parent
commit
2897dcca5a

+ 45
- 6
src/com/dmdirc/FrameContainer.java View File

@@ -501,11 +501,13 @@ public abstract class FrameContainer<T extends Window> {
501 501
      * reason, the line is silently discarded.
502 502
      *
503 503
      * @param type The message type to use
504
+     * @param timestamp The timestamp to use for this line
504 505
      * @param args The message's arguments
506
+     * @since 0.6.4
505 507
      */
506
-    public void addLine(final String type, final Object ... args) {
508
+    public void addLine(final String type, final Date timestamp, final Object ... args) {
507 509
         if (type != null && !type.isEmpty()) {
508
-            addLine(Formatter.formatMessage(getConfigManager(), type, args), true);
510
+            addLine(Formatter.formatMessage(getConfigManager(), type, args), timestamp);
509 511
         }
510 512
     }
511 513
 
@@ -516,12 +518,36 @@ public abstract class FrameContainer<T extends Window> {
516 518
      * @param type The message type to use
517 519
      * @param args The message's arguments
518 520
      */
519
-    public void addLine(final StringBuffer type, final Object ... args) {
521
+    public void addLine(final String type, final Object ... args) {
522
+        addLine(type, new Date(), args);
523
+    }
524
+
525
+    /**
526
+     * Adds a line to this container's window. If the window is null for some
527
+     * reason, the line is silently discarded.
528
+     *
529
+     * @param type The message type to use
530
+     * @param timestamp The timestamp to use for this line
531
+     * @param args The message's arguments
532
+     * @since 0.6.4
533
+     */
534
+    public void addLine(final StringBuffer type, final Date timestamp, final Object ... args) {
520 535
         if (type != null) {
521
-            addLine(type.toString(), args);
536
+            addLine(type.toString(), timestamp, args);
522 537
         }
523 538
     }
524 539
 
540
+    /**
541
+     * Adds a line to this container's window. If the window is null for some
542
+     * reason, the line is silently discarded.
543
+     *
544
+     * @param type The message type to use
545
+     * @param args The message's arguments
546
+     */
547
+    public void addLine(final StringBuffer type, final Object ... args) {
548
+        addLine(type, new Date(), args);
549
+    }
550
+
525 551
     /**
526 552
      * Adds the specified raw line to the window, without using a formatter.
527 553
      *
@@ -529,12 +555,25 @@ public abstract class FrameContainer<T extends Window> {
529 555
      * @param timestamp Whether or not to display the timestamp for this line
530 556
      */
531 557
     public void addLine(final String line, final boolean timestamp) {
558
+        addLine(line, timestamp ? new Date() : null);
559
+    }
560
+
561
+    /**
562
+     * Adds the specified raw line to the window, without using a formatter,
563
+     * and using the specified timestamp. If the timestamp is <code>null</code>,
564
+     * no timestamp is added.
565
+     *
566
+     * @param line The line to be added
567
+     * @param timestamp The timestamp to use for the line
568
+     * @since 0.6.4
569
+     */
570
+    public void addLine(final String line, final Date timestamp) {
532 571
         final String encodedLine = transcoder.decode(line);
533 572
         final List<String[]> lines = new LinkedList<String[]>();
534 573
         for (final String myLine : encodedLine.split("\n")) {
535
-            if (timestamp) {
574
+            if (timestamp != null) {
536 575
                 lines.add(new String[]{
537
-                    Formatter.formatMessage(getConfigManager(), "timestamp", new Date()),
576
+                    Formatter.formatMessage(getConfigManager(), "timestamp", timestamp),
538 577
                     myLine,
539 578
                 });
540 579
             } else {

+ 41
- 18
src/com/dmdirc/commandparser/commands/global/Echo.java View File

@@ -33,7 +33,7 @@ import com.dmdirc.ui.WindowManager;
33 33
 import com.dmdirc.ui.input.AdditionalTabTargets;
34 34
 
35 35
 import java.util.ArrayList;
36
-import java.util.Arrays;
36
+import java.util.Date;
37 37
 import java.util.List;
38 38
 
39 39
 /**
@@ -54,34 +54,51 @@ public final class Echo extends GlobalCommand implements IntelligentCommand {
54 54
 
55 55
     /** {@inheritDoc} */
56 56
     @Override
57
-    public void execute(final FrameContainer origin, final boolean isSilent,
57
+    public void execute(final FrameContainer<?> origin, final boolean isSilent,
58 58
             final CommandArguments args) {
59
-        if (args.getArguments().length > 0
60
-                && args.getArguments()[0].equalsIgnoreCase("--active")) {
61
-            final FrameContainer frame = WindowManager.getActiveWindow();
62
-            frame.addLine(FORMAT_OUTPUT, args.getArgumentsAsString(1));
63
-        } else if (args.getArguments().length > 1
64
-                && args.getArguments()[0].equalsIgnoreCase("--target")) {
65
-            FrameContainer frame = null;
66
-            FrameContainer target = origin;
59
+        int offset = 0;
60
+        Date time = new Date();
61
+
62
+        if (args.getArguments().length > 1
63
+                && args.getArguments()[offset].equalsIgnoreCase("--ts")) {
64
+            try {
65
+                time = new Date(Long.parseLong(args.getWordsAsString(2, 2)));
66
+            } catch (NumberFormatException ex) {
67
+                sendLine(origin, isSilent, FORMAT_ERROR, "Unable to process timestamp");
68
+                return;
69
+            }
70
+
71
+            offset = 2;
72
+        }
73
+
74
+        if (args.getArguments().length > offset
75
+                && args.getArguments()[offset].equalsIgnoreCase("--active")) {
76
+            if (!isSilent) {
77
+                final FrameContainer<?> frame = WindowManager.getActiveWindow();
78
+                frame.addLine(FORMAT_OUTPUT, time, args.getArgumentsAsString(offset + 1));
79
+            }
80
+        } else if (args.getArguments().length > offset + 1
81
+                && args.getArguments()[offset].equalsIgnoreCase("--target")) {
82
+            FrameContainer<?> frame = null;
83
+            FrameContainer<?> target = origin;
67 84
 
68 85
             while (frame == null && target != null) {
69
-                frame = WindowManager.findCustomWindow(target, args.getArguments()[1]);
86
+                frame = WindowManager.findCustomWindow(target, args.getArguments()[offset + 1]);
70 87
             }
71 88
 
72 89
             if (frame == null) {
73
-                frame = WindowManager.findCustomWindow(args.getArguments()[1]);
90
+                frame = WindowManager.findCustomWindow(args.getArguments()[offset + 1]);
74 91
             }
75 92
 
76 93
             if (frame == null) {
77 94
                 sendLine(origin, isSilent, FORMAT_ERROR,
78 95
                         "Unable to find target window");
79
-            } else {
80
-                frame.addLine(FORMAT_OUTPUT, args.getArgumentsAsString(2));
96
+            } else if (!isSilent) {
97
+                frame.addLine(FORMAT_OUTPUT, time, args.getArgumentsAsString(offset + 2));
81 98
             }
82 99
 
83
-        } else {
84
-            sendLine(origin, isSilent, FORMAT_OUTPUT, args.getArgumentsAsString());
100
+        } else if (origin != null && !isSilent) {
101
+            origin.addLine(FORMAT_OUTPUT, time, args.getArgumentsAsString(offset));
85 102
         }
86 103
     }
87 104
 
@@ -100,7 +117,7 @@ public final class Echo extends GlobalCommand implements IntelligentCommand {
100 117
     /** {@inheritDoc} */
101 118
     @Override
102 119
     public String getHelp() {
103
-        return "echo [--active|--target <window>] <line> "
120
+        return "echo [--ts <timestamp>] [--active|--target <window>] <line> "
104 121
                 + "- echos the specified line to the window";
105 122
     }
106 123
 
@@ -113,7 +130,10 @@ public final class Echo extends GlobalCommand implements IntelligentCommand {
113 130
         if (arg == 0) {
114 131
             targets.add("--active");
115 132
             targets.add("--target");
116
-        } else if (arg == 1 && context.getPreviousArgs().get(0).equals("--target")) {
133
+            targets.add("--ts");
134
+        } else if ((arg == 1 && context.getPreviousArgs().get(0).equals("--target"))
135
+                || (arg == 3 && context.getPreviousArgs().get(2).equals("--target")
136
+                && context.getPreviousArgs().get(0).equals("--ts"))) {
117 137
 
118 138
             final List<FrameContainer<?>> windowList = new ArrayList<FrameContainer<?>>();
119 139
             final Server currentServer = context.getWindow().getContainer()
@@ -135,6 +155,9 @@ public final class Echo extends GlobalCommand implements IntelligentCommand {
135 155
                 }
136 156
             }
137 157
 
158
+            targets.excludeAll();
159
+        } else if (arg == 1 && context.getPreviousArgs().get(0).equals("--ts")) {
160
+            targets.add(String.valueOf(new Date().getTime()));
138 161
             targets.excludeAll();
139 162
         }
140 163
 

Loading…
Cancel
Save