Skip to content

Commit 7c6d15f

Browse files
committed
Support Colored Output to Console
1 parent f39945c commit 7c6d15f

File tree

7 files changed

+67
-187
lines changed

7 files changed

+67
-187
lines changed

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ add_library(jsonParserLib
1919
core/types/JsonArray.c
2020
core/types/JsonObject.c
2121
core/types/JsonString.c
22-
core/utils/utf2gbk/UTF2GBK.c
23-
core/utils/utf2gbk/UTF2GBK.h
2422
)
2523
target_link_libraries(jsonParserLib PRIVATE Iconv::Iconv)
2624

cli/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ int main(const int argc, char* argv[]) {
3030

3131
//解析Json
3232
const struct JsonVal* json = parseValue();
33-
if (args.compress) { printJsonVal(json); }
34-
else if (args.format) { printfJsonVal(json, 0); }
33+
if (args.compress) { printJsonVal(json, args.output == stdout); }
34+
else if (args.format) { printfJsonVal(json, 0, args.output == stdout); }
3535

3636
//destoryJsonVal(json);
3737
if (args.input != stdin) { fclose(args.input); }

core/Json.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55
#include "types/JsonArray.h"
66
#include "utils/outputer.h"
77
#include "parser/parser.h"
8-
#include "utils/utf2gbk/UTF2GBK.h"

core/utils/outputer.c

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,45 +16,46 @@ void indent(int hierarchy) {
1616
// XXX: 实现setIndentation // XXX vs无高亮?
1717
}
1818

19-
void printfJsonVal(const struct JsonVal* val, const int hierarchy) {
19+
void printfJsonVal(const struct JsonVal* val, const int hierarchy, const int color) {
2020
switch (val->type){
2121
case NUMBER:
22-
printNumber(val->val); break;
22+
printNumber(val->val, color); break;
2323
case STRING:
24-
printString(val->val); break;
24+
printString(val->val, color); break;
2525
case NONE:
26-
printNONE(); break;
26+
printNONE(color); break;
2727
case BOOL:
28-
printBool(val->val); break;
28+
printBool(val->val, color); break;
2929
case ARRAY:
30-
printfArray(val->arr, hierarchy); break;
30+
printfArray(val->arr, hierarchy, color); break;
3131
case OBJECT:
32-
printfObject(val->obj, hierarchy); break;
32+
printfObject(val->obj, hierarchy, color); break;
3333
default:
3434
break;
3535
}
3636
}
3737

38-
void printJsonVal(const struct JsonVal* val) {
38+
void printJsonVal(const struct JsonVal* val, const int color) {
3939
switch (val->type) {
4040
case NUMBER:
41-
printNumber(val->val); break;
41+
printNumber(val->val, color); break;
4242
case STRING:
43-
printString(val->val); break;
43+
printString(val->val, color); break;
4444
case NONE:
45-
printNONE(); break;
45+
printNONE(color); break;
4646
case BOOL:
47-
printBool(val->val); break;
47+
printBool(val->val, color); break;
4848
case ARRAY:
49-
printArray(val->arr); break;
49+
printArray(val->arr, color); break;
5050
case OBJECT:
51-
printObject(val->obj); break;
51+
printObject(val->obj, color); break;
5252
default:
5353
break;
5454
}
5555
}
5656

57-
void printString(const struct JsonString* str) {
57+
void printString(const struct JsonString* str, const int color) {
58+
if(color) fputs(ANSI_COLOR_GREEN, f);
5859
fputc('"', f);
5960
char * reader = str->str;
6061
while (*reader) {
@@ -67,15 +68,20 @@ void printString(const struct JsonString* str) {
6768

6869
reader++;
6970
}
71+
fputc('"', f);if(color) fputs(ANSI_COLOR_RESET, f);
7072

71-
fputc('"', f);
7273
}
7374

74-
void printfObject(const struct JsonObj* obj, const int hierarchy) {
75+
void printfObject(const struct JsonObj* obj, const int hierarchy, const int color) {
7576
fputs("{\n", f);
7677
for (int i = 0; i < obj->size; i++) {
77-
indent(hierarchy + 1); printString(obj->key + i);
78-
fputs(": ", f); printfJsonVal(obj->value + i, hierarchy + 1);
78+
indent(hierarchy + 1);
79+
if(color) {
80+
fputs(ANSI_COLOR_MAGENTA, f);
81+
printString(obj->key + i, 0);
82+
fputs(ANSI_COLOR_RESET, f);
83+
}else printString(obj->key + i, 0);
84+
fputs(": ", f); printfJsonVal(obj->value + i, hierarchy + 1, color);
7985
if (i != obj->size - 1) fputs(",", f);
8086
fputs("\n", f);
8187
}
@@ -84,41 +90,53 @@ void printfObject(const struct JsonObj* obj, const int hierarchy) {
8490

8591

8692

87-
void printfArray(const struct JsonArray* array, int hierarchy) {
93+
void printfArray(const struct JsonArray* array, int hierarchy, const int color) {
8894
fputs("[\n", f);
8995
for (int i = 0; i < array->length; i++) {
90-
indent(hierarchy + 1); printfJsonVal(&array->arr[i], hierarchy + 1);
96+
indent(hierarchy + 1); printfJsonVal(&array->arr[i], hierarchy + 1, color);
9197
if (i != array->length - 1) fputs(",", f);
9298
fputs("\n", f);
9399
}
94100
indent(hierarchy); fputs("]", f);
95101
}
96102

97103

98-
void printNumber(const struct JsonString* num) {
104+
void printNumber(const struct JsonString* num, const int color) {
105+
if(color) fputs(ANSI_COLOR_CYAN, f);
99106
fputs(num->str, f);
107+
if(color) fputs(ANSI_COLOR_RESET, f);
100108
}
101109

102-
void printBool(const struct JsonString* bl) {
110+
void printBool(const struct JsonString* bl, const int color) {
111+
if(color) fputs(ANSI_COLOR_YELLOW, f);
103112
fputs(bl->str, f);
113+
if(color) fputs(ANSI_COLOR_RESET, f);
114+
104115
}
105116

106-
void printNONE() {
117+
void printNONE(const int color) {
118+
if(color) fputs(ANSI_COLOR_YELLOW, f);
107119
fputs("null", f);
120+
if(color) fputs(ANSI_COLOR_RESET, f);
108121
}
109122

110-
void printObject(const struct JsonObj* obj){
123+
void printObject(const struct JsonObj* obj, const int color){
111124
fputc('{', f);
112125
for (int i = 0; i < obj->size; i++) {
113-
printString(obj->key + i); putc(':', f);
114-
printJsonVal(obj->value + i);
126+
if(color) {
127+
fputs(ANSI_COLOR_MAGENTA, f);
128+
printString(obj->key + i, color);
129+
fputs(ANSI_COLOR_RESET, f);
130+
}else printString(obj->key + i, color);
131+
putc(':', f);
132+
printJsonVal(obj->value + i, color);
115133
if (i != obj->size - 1) fputc(',', f);
116134
} fputc('}', f);
117135
}
118-
void printArray(const struct JsonArray* array){
136+
void printArray(const struct JsonArray* array, const int color){
119137
fputc('[', f);
120138
for (int i = 0; i < array->length; i++) {
121-
printJsonVal(array->arr + i);
139+
printJsonVal(array->arr + i, color);
122140
if (i != array->length - 1) fputc(',', f);
123141
}fputc(']', f);
124142
}

core/utils/outputer.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
#pragma once
2-
#include "../types/JsonObject.h"
32
#include "../types/JsonString.h"
43
#include "../types/JsonValue.h"
54
#include "../types/JsonArray.h"
65

6+
#define ANSI_COLOR_RED "\x1b[31m"
7+
#define ANSI_COLOR_GREEN "\x1b[32m"
8+
#define ANSI_COLOR_YELLOW "\x1b[33m"
9+
#define ANSI_COLOR_BLUE "\x1b[34m"
10+
#define ANSI_COLOR_MAGENTA "\x1b[35m"
11+
#define ANSI_COLOR_CYAN "\x1b[36m"
12+
#define ANSI_COLOR_RESET "\x1b[0m"
13+
714
void setOutputStream(FILE* stream);
8-
void printString(const struct JsonString* str);
9-
void printfJsonVal(const struct JsonVal* val, int hierarchy);
10-
void printJsonVal(const struct JsonVal* val);
11-
void printfObject(const struct JsonObj* obj, int hierarchy);
12-
void printfArray(const struct JsonArray* array, int hierarchy);
13-
void printNONE();
14-
void printBool(const struct JsonString* bl);
15-
void printNumber(const struct JsonString* num);
16-
void printObject(const struct JsonObj* obj);
17-
void printArray(const struct JsonArray* array);
15+
void printString(const struct JsonString* str, const int color);
16+
void printfJsonVal(const struct JsonVal* val, int hierarchy, const int color);
17+
void printJsonVal(const struct JsonVal* val, const int color);
18+
void printfObject(const struct JsonObj* obj, int hierarchy, const int color);
19+
void printfArray(const struct JsonArray* array, int hierarchy, const int color);
20+
void printNONE(const int color);
21+
void printBool(const struct JsonString* bl, const int color);
22+
void printNumber(const struct JsonString* num, const int color);
23+
void printObject(const struct JsonObj* obj, const int color);
24+
void printArray(const struct JsonArray* array, const int color);

core/utils/utf2gbk/UTF2GBK.c

Lines changed: 0 additions & 133 deletions
This file was deleted.

core/utils/utf2gbk/UTF2GBK.h

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)