File: cut-console-diff-writer.c

package info (click to toggle)
cutter-testing-framework 1.1.7-1.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 18,556 kB
  • sloc: ansic: 79,864; xml: 38,680; sh: 10,266; makefile: 2,324; ruby: 845; cpp: 697
file content (396 lines) | stat: -rw-r--r-- 11,930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 *  Copyright (C) 2009  Kouhei Sutou <kou@clear-code.com>
 *
 *  This library is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif /* HAVE_CONFIG_H */

#include <glib.h>

#include "cut-console-diff-writer.h"
#include "cut-console.h"

#define DEFAULT_DELETED_MARK_COLOR CUT_CONSOLE_COLOR_RED
#define DEFAULT_INSERTED_MARK_COLOR CUT_CONSOLE_COLOR_GREEN
#define DEFAULT_DELETED_LINE_COLOR CUT_CONSOLE_COLOR_RED
#define DEFAULT_INSERTED_LINE_COLOR CUT_CONSOLE_COLOR_GREEN

#define DEFAULT_DELETED_SEGMENT_COLOR           \
    CUT_CONSOLE_COLOR_WHITE                     \
    CUT_CONSOLE_COLOR_RED_BACK
#define DEFAULT_INSERTED_SEGMENT_COLOR          \
    CUT_CONSOLE_COLOR_WHITE                     \
    CUT_CONSOLE_COLOR_GREEN_BACK

#define CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CUT_TYPE_CONSOLE_DIFF_WRITER, CutConsoleDiffWriterPrivate))

typedef struct _CutConsoleDiffWriterPrivate         CutConsoleDiffWriterPrivate;
struct _CutConsoleDiffWriterPrivate
{
    gboolean use_color;
    gchar *deleted_mark_color;
    gchar *inserted_mark_color;
    gchar *deleted_line_color;
    gchar *inserted_line_color;
    gchar *deleted_segment_color;
    gchar *inserted_segment_color;
};

enum {
    PROP_0,
    PROP_USE_COLOR
};

G_DEFINE_TYPE(CutConsoleDiffWriter, cut_console_diff_writer, CUT_TYPE_DIFF_WRITER)

static void dispose        (GObject         *object);
static void set_property   (GObject         *object,
                            guint            prop_id,
                            const GValue    *value,
                            GParamSpec      *pspec);
static void get_property   (GObject         *object,
                            guint            prop_id,
                            GValue          *value,
                            GParamSpec      *pspec);

static void write          (CutDiffWriter       *writer,
                            const gchar         *console,
                            CutDiffWriterTag     tag);
static void write_line     (CutDiffWriter       *writer,
                            const gchar         *line,
                            CutDiffWriterTag     tag);
static void finish         (CutDiffWriter       *writer);

static void
cut_console_diff_writer_class_init (CutConsoleDiffWriterClass *klass)
{
    GObjectClass *gobject_class;
    CutDiffWriterClass *diff_writer_class;
    GParamSpec *spec;

    gobject_class = G_OBJECT_CLASS(klass);
    diff_writer_class = CUT_DIFF_WRITER_CLASS(klass);

    gobject_class->dispose = dispose;
    gobject_class->set_property = set_property;
    gobject_class->get_property = get_property;

    diff_writer_class->write = write;
    diff_writer_class->write_line = write_line;
    diff_writer_class->finish = finish;

    spec = g_param_spec_boolean("use-color",
                                "Use color",
                                "Whether use color",
                                FALSE,
                                G_PARAM_READWRITE);
    g_object_class_install_property(gobject_class, PROP_USE_COLOR, spec);

    g_type_class_add_private(gobject_class, sizeof(CutConsoleDiffWriterPrivate));
}

static void
cut_console_diff_writer_init (CutConsoleDiffWriter *writer)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer);
    priv->deleted_mark_color = g_strdup(DEFAULT_DELETED_MARK_COLOR);
    priv->inserted_mark_color = g_strdup(DEFAULT_INSERTED_MARK_COLOR);
    priv->deleted_line_color = g_strdup(DEFAULT_DELETED_LINE_COLOR);
    priv->inserted_line_color = g_strdup(DEFAULT_INSERTED_LINE_COLOR);
    priv->deleted_segment_color = g_strdup(DEFAULT_DELETED_SEGMENT_COLOR);
    priv->inserted_segment_color = g_strdup(DEFAULT_INSERTED_SEGMENT_COLOR);
}

static void
dispose (GObject *object)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(object);

    if (priv->deleted_mark_color) {
        g_free(priv->deleted_mark_color);
        priv->deleted_mark_color = NULL;
    }

    if (priv->inserted_mark_color) {
        g_free(priv->inserted_mark_color);
        priv->inserted_mark_color = NULL;
    }

    if (priv->deleted_line_color) {
        g_free(priv->deleted_line_color);
        priv->deleted_line_color = NULL;
    }

    if (priv->inserted_line_color) {
        g_free(priv->inserted_line_color);
        priv->inserted_line_color = NULL;
    }

    if (priv->deleted_segment_color) {
        g_free(priv->deleted_segment_color);
        priv->deleted_segment_color = NULL;
    }

    if (priv->inserted_segment_color) {
        g_free(priv->inserted_segment_color);
        priv->inserted_segment_color = NULL;
    }

    G_OBJECT_CLASS(cut_console_diff_writer_parent_class)->dispose(object);
}

static void
set_property (GObject      *object,
              guint         prop_id,
              const GValue *value,
              GParamSpec   *pspec)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(object);
    switch (prop_id) {
    case PROP_USE_COLOR:
        priv->use_color = g_value_get_boolean(value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void
get_property (GObject    *object,
              guint       prop_id,
              GValue     *value,
              GParamSpec *pspec)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(object);
    switch (prop_id) {
    case PROP_USE_COLOR:
        g_value_set_boolean(value, priv->use_color);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

CutDiffWriter *
cut_console_diff_writer_new (gboolean use_color)
{
    return g_object_new(CUT_TYPE_CONSOLE_DIFF_WRITER,
                        "use-color", use_color,
                        NULL);
}

void
cut_console_diff_writer_set_deleted_mark_color (CutDiffWriter *writer,
                                                const gchar *color)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer);
    g_free(priv->deleted_mark_color);
    if (!color)
        color = DEFAULT_DELETED_MARK_COLOR;
    priv->deleted_mark_color = g_strdup(color);
}

const gchar *
cut_console_diff_writer_get_deleted_mark_color (CutDiffWriter *writer)
{
    return CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer)->deleted_mark_color;
}

void
cut_console_diff_writer_set_inserted_mark_color (CutDiffWriter *writer,
                                                 const gchar *color)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer);
    g_free(priv->inserted_mark_color);
    if (!color)
        color = DEFAULT_INSERTED_MARK_COLOR;
    priv->inserted_mark_color = g_strdup(color);
}

const gchar *
cut_console_diff_writer_get_inserted_mark_color (CutDiffWriter *writer)
{
    return CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer)->inserted_mark_color;
}

void
cut_console_diff_writer_set_deleted_line_color (CutDiffWriter *writer,
                                                const gchar *color)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer);
    g_free(priv->deleted_line_color);
    if (!color)
        color = DEFAULT_DELETED_LINE_COLOR;
    priv->deleted_line_color = g_strdup(color);
}

const gchar *
cut_console_diff_writer_get_deleted_line_color (CutDiffWriter *writer)
{
    return CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer)->deleted_line_color;
}

void
cut_console_diff_writer_set_inserted_line_color (CutDiffWriter *writer,
                                                 const gchar *color)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer);
    g_free(priv->inserted_line_color);
    if (!color)
        color = DEFAULT_INSERTED_LINE_COLOR;
    priv->inserted_line_color = g_strdup(color);
}

const gchar *
cut_console_diff_writer_get_inserted_line_color (CutDiffWriter *writer)
{
    return CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer)->inserted_line_color;
}

void
cut_console_diff_writer_set_deleted_segment_color (CutDiffWriter *writer,
                                                   const gchar *color)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer);
    g_free(priv->deleted_segment_color);
    if (!color)
        color = DEFAULT_DELETED_SEGMENT_COLOR;
    priv->deleted_segment_color = g_strdup(color);
}

const gchar *
cut_console_diff_writer_get_deleted_segment_color (CutDiffWriter *writer)
{
    return CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer)->deleted_segment_color;
}

void
cut_console_diff_writer_set_inserted_segment_color (CutDiffWriter *writer,
                                                    const gchar *color)
{
    CutConsoleDiffWriterPrivate *priv;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer);
    g_free(priv->inserted_segment_color);
    if (!color)
        color = DEFAULT_INSERTED_SEGMENT_COLOR;
    priv->inserted_segment_color = g_strdup(color);
}

const gchar *
cut_console_diff_writer_get_inserted_segment_color (CutDiffWriter *writer)
{
    return CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer)->inserted_segment_color;
}

static const gchar *
tag_to_color (CutConsoleDiffWriterPrivate *priv, CutDiffWriterTag tag)
{
    const gchar *color;

    switch (tag) {
    case CUT_DIFF_WRITER_TAG_SUMMARY:
        color = CUT_CONSOLE_COLOR_CYAN;
        break;
    case CUT_DIFF_WRITER_TAG_CONTEXT:
        color = CUT_CONSOLE_COLOR_WHITE CUT_CONSOLE_COLOR_CYAN_BACK;
        break;
    case CUT_DIFF_WRITER_TAG_DELETED_MARK:
        color = priv->deleted_mark_color;
        break;
    case CUT_DIFF_WRITER_TAG_INSERTED_MARK:
        color = priv->inserted_mark_color;
        break;
    case CUT_DIFF_WRITER_TAG_DIFFERENCE_MARK:
        color = CUT_CONSOLE_COLOR_CYAN;
        break;
    case CUT_DIFF_WRITER_TAG_DELETED_LINE:
        color = priv->deleted_line_color;
        break;
    case CUT_DIFF_WRITER_TAG_INSERTED_LINE:
        color = priv->inserted_line_color;
        break;
    case CUT_DIFF_WRITER_TAG_DIFFERENCE_LINE:
        color = CUT_CONSOLE_COLOR_CYAN;
        break;
    case CUT_DIFF_WRITER_TAG_DELETED_SEGMENT:
        color = priv->deleted_segment_color;
        break;
    case CUT_DIFF_WRITER_TAG_INSERTED_SEGMENT:
        color = priv->inserted_segment_color;
        break;
    default:
        color = NULL;
        break;
    }

    return color;
}

static void
write (CutDiffWriter *writer, const gchar *string, CutDiffWriterTag tag)
{
    CutConsoleDiffWriterPrivate *priv;
    const gchar *color = NULL;

    priv = CUT_CONSOLE_DIFF_WRITER_GET_PRIVATE(writer);
    if (priv->use_color)
        color = tag_to_color(priv, tag);

    if (color)
        g_print("%s%s%s", color, string, CUT_CONSOLE_COLOR_NORMAL);
    else
        g_print("%s", string);
}

static void
write_line (CutDiffWriter *writer, const gchar *line, CutDiffWriterTag tag)
{
    write(writer, line, tag);
    g_print("\n");
}

static void
finish (CutDiffWriter *writer)
{
}

/*
vi:ts=4:nowrap:ai:expandtab:sw=4
*/