@@ -16,45 +16,46 @@ void indent(int hierarchy) {
16
16
// XXX: 实现setIndentation // XXX vs无高亮?
17
17
}
18
18
19
- void printfJsonVal (const struct JsonVal * val , const int hierarchy ) {
19
+ void printfJsonVal (const struct JsonVal * val , const int hierarchy , const int color ) {
20
20
switch (val -> type ){
21
21
case NUMBER :
22
- printNumber (val -> val ); break ;
22
+ printNumber (val -> val , color ); break ;
23
23
case STRING :
24
- printString (val -> val ); break ;
24
+ printString (val -> val , color ); break ;
25
25
case NONE :
26
- printNONE (); break ;
26
+ printNONE (color ); break ;
27
27
case BOOL :
28
- printBool (val -> val ); break ;
28
+ printBool (val -> val , color ); break ;
29
29
case ARRAY :
30
- printfArray (val -> arr , hierarchy ); break ;
30
+ printfArray (val -> arr , hierarchy , color ); break ;
31
31
case OBJECT :
32
- printfObject (val -> obj , hierarchy ); break ;
32
+ printfObject (val -> obj , hierarchy , color ); break ;
33
33
default :
34
34
break ;
35
35
}
36
36
}
37
37
38
- void printJsonVal (const struct JsonVal * val ) {
38
+ void printJsonVal (const struct JsonVal * val , const int color ) {
39
39
switch (val -> type ) {
40
40
case NUMBER :
41
- printNumber (val -> val ); break ;
41
+ printNumber (val -> val , color ); break ;
42
42
case STRING :
43
- printString (val -> val ); break ;
43
+ printString (val -> val , color ); break ;
44
44
case NONE :
45
- printNONE (); break ;
45
+ printNONE (color ); break ;
46
46
case BOOL :
47
- printBool (val -> val ); break ;
47
+ printBool (val -> val , color ); break ;
48
48
case ARRAY :
49
- printArray (val -> arr ); break ;
49
+ printArray (val -> arr , color ); break ;
50
50
case OBJECT :
51
- printObject (val -> obj ); break ;
51
+ printObject (val -> obj , color ); break ;
52
52
default :
53
53
break ;
54
54
}
55
55
}
56
56
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 );
58
59
fputc ('"' , f );
59
60
char * reader = str -> str ;
60
61
while (* reader ) {
@@ -67,15 +68,20 @@ void printString(const struct JsonString* str) {
67
68
68
69
reader ++ ;
69
70
}
71
+ fputc ('"' , f );if (color ) fputs (ANSI_COLOR_RESET , f );
70
72
71
- fputc ('"' , f );
72
73
}
73
74
74
- void printfObject (const struct JsonObj * obj , const int hierarchy ) {
75
+ void printfObject (const struct JsonObj * obj , const int hierarchy , const int color ) {
75
76
fputs ("{\n" , f );
76
77
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 );
79
85
if (i != obj -> size - 1 ) fputs ("," , f );
80
86
fputs ("\n" , f );
81
87
}
@@ -84,41 +90,53 @@ void printfObject(const struct JsonObj* obj, const int hierarchy) {
84
90
85
91
86
92
87
- void printfArray (const struct JsonArray * array , int hierarchy ) {
93
+ void printfArray (const struct JsonArray * array , int hierarchy , const int color ) {
88
94
fputs ("[\n" , f );
89
95
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 );
91
97
if (i != array -> length - 1 ) fputs ("," , f );
92
98
fputs ("\n" , f );
93
99
}
94
100
indent (hierarchy ); fputs ("]" , f );
95
101
}
96
102
97
103
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 );
99
106
fputs (num -> str , f );
107
+ if (color ) fputs (ANSI_COLOR_RESET , f );
100
108
}
101
109
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 );
103
112
fputs (bl -> str , f );
113
+ if (color ) fputs (ANSI_COLOR_RESET , f );
114
+
104
115
}
105
116
106
- void printNONE () {
117
+ void printNONE (const int color ) {
118
+ if (color ) fputs (ANSI_COLOR_YELLOW , f );
107
119
fputs ("null" , f );
120
+ if (color ) fputs (ANSI_COLOR_RESET , f );
108
121
}
109
122
110
- void printObject (const struct JsonObj * obj ){
123
+ void printObject (const struct JsonObj * obj , const int color ){
111
124
fputc ('{' , f );
112
125
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 );
115
133
if (i != obj -> size - 1 ) fputc (',' , f );
116
134
} fputc ('}' , f );
117
135
}
118
- void printArray (const struct JsonArray * array ){
136
+ void printArray (const struct JsonArray * array , const int color ){
119
137
fputc ('[' , f );
120
138
for (int i = 0 ; i < array -> length ; i ++ ) {
121
- printJsonVal (array -> arr + i );
139
+ printJsonVal (array -> arr + i , color );
122
140
if (i != array -> length - 1 ) fputc (',' , f );
123
141
}fputc (']' , f );
124
142
}
0 commit comments