Merge lp:~oppifjellet/cable/color-codes into lp:cable

Proposed by Daniele "OpenNingia" Simonetti
Status: Merged
Merged at revision: 128
Proposed branch: lp:~oppifjellet/cable/color-codes
Merge into: lp:cable
Prerequisite: lp:~oppifjellet/cable/message-history
Diff against target: 315 lines (+188/-13)
5 files modified
CMakeLists.txt (+3/-0)
src/Cable.vala (+7/-5)
src/Utils/Utils.vala (+141/-8)
tests/TestColors.vala (+36/-0)
tests/TestMain.vala (+1/-0)
To merge this branch: bzr merge lp:~oppifjellet/cable/color-codes
Reviewer Review Type Date Requested Status
Julien Spautz Pending
Review via email: mp+184209@code.launchpad.net

Description of the change

This branch is focused on implementing the support for color codes.
The current implementation parses color codes and visualize colored test as sent by other clients.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'CMakeLists.txt'
2--- CMakeLists.txt 2013-09-05 22:43:13 +0000
3+++ CMakeLists.txt 2013-09-05 22:43:13 +0000
4@@ -114,7 +114,10 @@
5 tests/TestCase.vala
6 tests/TestMain.vala
7 tests/TestExample.vala
8+ tests/TestColors.vala
9 tests/TestMessageHistory.vala
10+
11+ src/Utils/Utils.vala
12 src/Utils/RRStringList.vala
13 PACKAGES
14 gtk+-3.0
15
16=== modified file 'src/Cable.vala'
17--- src/Cable.vala 2013-08-20 17:24:19 +0000
18+++ src/Cable.vala 2013-09-05 22:43:13 +0000
19@@ -25,7 +25,7 @@
20 static bool option_reset = false;
21 static bool option_version = false;
22 static bool option_test = false;
23-
24+
25 static const OptionEntry[] option_entries = {
26 { "reset", 0, 0, OptionArg.NONE, ref option_reset,
27 N_("Reset all settings and configuration to default"), null },
28@@ -38,7 +38,7 @@
29
30 { null }
31 };
32-
33+
34 static Gee.ArrayList <Controller.Window> controllers {
35 get; set;
36 default = new Gee.ArrayList <Controller.Window> ();
37@@ -86,6 +86,8 @@
38 Launcher.init ();
39 Settings.init ();
40 Utils.init ();
41+ Utils.Network.init ();
42+ Utils.Identity.init ();
43 Irc.init ();
44 }
45
46@@ -96,7 +98,7 @@
47 add_window (window);
48 controllers.add (controller);
49 }
50-
51+
52 public static int main (string[] args) {
53 var context = new GLib.OptionContext ("");
54 context.set_help_enabled (true);
55@@ -117,11 +119,11 @@
56 if (option_reset) {
57 // XXX
58 }
59-
60+
61 if (option_test) {
62 return Tests.run (args);
63 }
64-
65+
66
67 var cable = new Cable.App ();
68 return cable.run (args);
69
70=== modified file 'src/Utils/Utils.vala'
71--- src/Utils/Utils.vala 2013-08-06 15:32:12 +0000
72+++ src/Utils/Utils.vala 2013-09-05 22:43:13 +0000
73@@ -10,7 +10,7 @@
74 but WITHOUT ANY WARRANTY; without even the implied warranty of
75 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
76 Lesser General Public License for more details.
77-
78+
79 You should have received a copy of the GNU Lesser General
80 Public License along with this library; if not, write to the
81 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
82@@ -19,9 +19,64 @@
83
84 namespace Cable.Utils {
85
86+ internal static string[] _palette;
87+
88+ /*
89+mIRC palette
90+------------
91+0 white
92+1 black
93+2 blue (navy)
94+3 green
95+4 red
96+5 brown (maroon)
97+6 purple
98+7 orange (olive)
99+8 yellow
100+9 light green (lime)
101+10 teal (a green/blue cyan)
102+11 light cyan (cyan) (aqua)
103+12 light blue (royal)
104+13 pink (light purple) (fuchsia)
105+14 grey
106+15 light grey (silver)
107+
108+0: #FFFFFF
109+1: #000000
110+2: #00007F
111+3: #009300
112+4: #FF0000
113+5: #7F0000
114+6: #9C009C
115+7: #FC7F00
116+8: #FFFF00
117+9: #00FC00
118+10: #009393
119+11: #00FFFF
120+12: #0000FC
121+13: #FF00FF
122+14: #7F7F7F
123+15: #D2D2D2
124+*/
125+
126 public static void init () {
127- Network.init ();
128- Identity.init ();
129+ _palette = new string[] {
130+ "#EEE",
131+ "#000",
132+ "#00007F",
133+ "#009300",
134+ "#FF0000",
135+ "#7F0000",
136+ "#9C009C",
137+ "#FC7F00",
138+ "#FFFF00",
139+ "#00FC00",
140+ "#009393",
141+ "#00FFFF",
142+ "#0000FC",
143+ "#FF00FF",
144+ "#D2D2D2"
145+ };
146 }
147
148 class Label : Gtk.Label {
149@@ -38,28 +93,106 @@
150 this.sensitive = sensitive;
151 }
152 }
153-
154+
155 // FIXME this sucks and only works for normal entries
156 public void filter (Gtk.Editable editable, string text, string[] needles) {
157 foreach (var needle in needles)
158 if (needle in text)
159 Signal.stop_emission_by_name (editable, "insert-text");
160 }
161-
162+
163+ public string parse_color_codes(string input) {
164+ const string token = "&#x3;";
165+ var builder = new StringBuilder ();
166+ var offset = 0;
167+ var should_close_tag = false;
168+
169+ // e.g.
170+ // &#x32,4hello &#x31,3world!
171+ // hello &#x32,4world!
172+
173+ while ( true ) {
174+ var idx = input.index_of(token, offset);
175+ if ( idx == -1 )
176+ break;
177+
178+ if ( idx != offset )
179+ builder.append(input[offset:idx]);
180+
181+ if ( should_close_tag ) {
182+ should_close_tag = false;
183+ builder.append("</span>");
184+ }
185+
186+ var ifg = -1;
187+ var ibg = -1;
188+ string? fg = null;
189+ string? bg = null;
190+
191+ offset = idx + token.length;
192+
193+ if ( offset >= input.length )
194+ break;
195+
196+ //debug("\npos: %s", input[offset:-1]);
197+
198+ var bld = new StringBuilder();
199+ while ( input[offset].isdigit() && offset < input.length )
200+ bld.append_c( input[offset++] );
201+ if (bld.str.length > 0)
202+ ifg = int.parse(bld.str);
203+
204+ if ((offset+1) < input.length && input[offset] == ',') {
205+ bld.assign("");
206+ offset++;
207+ while ( input[offset].isdigit() && offset < input.length )
208+ bld.append_c(input[offset++]);
209+ if (bld.str.length > 0)
210+ ibg = int.parse(bld.str);
211+ }
212+
213+ if ( ifg < _palette.length && ifg >= 0 )
214+ fg = _palette[ifg];
215+ if ( ibg < _palette.length && ibg >= 0 )
216+ bg = _palette[ibg];
217+
218+ //debug("\npalette len: %d ifg: %d, ibg: %d, fg: %s, bg: %s", _palette.length, ifg, ibg, fg, bg);
219+
220+ should_close_tag = (fg != null) || (bg != null);
221+ if ( fg != null && bg != null ) {
222+ builder.append("<span color=\"%s\" background=\"%s\">".printf(fg,bg));
223+ } else if ( fg != null ) {
224+ builder.append("<span color=\"%s\">".printf(fg));
225+ } else if ( bg != null ) {
226+ builder.append("<span background=\"%s\">".printf(bg));
227+ }
228+ }
229+
230+ if ( offset < input.length )
231+ builder.append(input[offset:input.length]);
232+
233+ if ( should_close_tag ) {
234+ builder.append("</span>");
235+ }
236+
237+ return builder.str;
238+ }
239+
240 /**
241 * Escapes critical characters and adds markup to make URLs clickable. To be used
242 * for messages and topics.
243 */
244 public string parse_string_to_markup (string input) {
245- var output = GLib.Markup.escape_text (input);
246+ var output = parse_color_codes (GLib.Markup.escape_text (input));
247+
248 var regex = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.\-]+|(?:www.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9.\-]+)((?:\/[\+~%\/.\w\-_]*)?\??(?:[!-\+=&;%@.\w_]*)#?(?:[,.!\/\-\+=&;%@\w_]*))?)/;
249-
250+
251 try {
252 return regex.replace (output, -1, 0, "<a href=\"\\1\">\\1</a>");
253 } catch (GLib.RegexError error) {
254 warning (error.message);
255 }
256-
257+
258 return output;
259 }
260 }
261
262=== added file 'tests/TestColors.vala'
263--- tests/TestColors.vala 1970-01-01 00:00:00 +0000
264+++ tests/TestColors.vala 2013-09-05 22:43:13 +0000
265@@ -0,0 +1,36 @@
266+using Cable.Utils;
267+
268+class TestColors : Gee.TestCase {
269+
270+ public TestColors() {
271+ // assign a name for this class
272+ base("TestColors");
273+ // add test methods
274+ add_test("[Chat] color codes", test_color_codes);
275+ }
276+
277+ public override void set_up () {
278+ Cable.Utils.init();
279+ }
280+
281+ public void test_color_codes() {
282+ // e.g.
283+ // &#x32,4hello &#x31,3world!
284+ // hello &#x32,4world!
285+ string text = "&#x3;2hello world!";
286+ string parsed_text = parse_color_codes(text);
287+ assert(parsed_text == "<span color=\"#00007F\">hello world!</span>");
288+
289+ text = "&#x3;2hello &#x3;2,3world!";
290+ parsed_text = parse_color_codes(text);
291+ assert(parsed_text == "<span color=\"#00007F\">hello </span><span color=\"#00007F\" background=\"#009300\">world!</span>");
292+
293+ text = "&#x3;2hello &#x3;world!";
294+ parsed_text = parse_color_codes(text);
295+ assert(parsed_text == "<span color=\"#00007F\">hello </span>world!");
296+ }
297+
298+ public override void tear_down () {
299+ // tear down your test
300+ }
301+}
302\ No newline at end of file
303
304=== modified file 'tests/TestMain.vala'
305--- tests/TestMain.vala 2013-09-05 22:43:13 +0000
306+++ tests/TestMain.vala 2013-09-05 22:43:13 +0000
307@@ -2,6 +2,7 @@
308 Test.init (ref args);
309 // add any of your test cases here
310 TestSuite.get_root().add_suite(new TestExample().get_suite());
311+ TestSuite.get_root().add_suite(new TestColors().get_suite());
312 TestSuite.get_root().add_suite(new TestMessageHistory().get_suite());
313 return Test.run ();
314 }
315\ No newline at end of file

Subscribers

People subscribed via source and target branches