-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathXcodeColors.m
324 lines (281 loc) · 9.88 KB
/
XcodeColors.m
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
//
// XcodeColors.m
// XcodeColors
//
// Created by Uncle MiF on 9/13/10.
// Copyright 2010 Deep IT. All rights reserved.
//
#import "XcodeColors.h"
#import "MethodReplace.h"
#define XCODE_COLORS "XcodeColors"
#define LC_SEQ_MAC @"\033["
#define LC_SEQ_IOS @"\xC2\xA0["
static IMP imp_ts_fixAttributesInRange = nil;
@interface XcodeColors_NSTextStorage : NSTextStorage
-(void)fixAttributesInRange:(NSRange)aRange;
@end
@implementation XcodeColors_NSTextStorage
void ApplyANSIColors(NSTextStorage * textStorage, NSRange range, NSString * seq)
{
NSString * affectedString = [[textStorage string] substringWithRange:range];
if ([affectedString rangeOfString:seq].location != NSNotFound)
{
NSArray * components = [affectedString componentsSeparatedByString:seq];
NSRange componentRange = range;
componentRange.length = 0;
BOOL firstPass = YES;
NSMutableArray * clearSEQ = [NSMutableArray array];
NSMutableDictionary * attrs = [NSMutableDictionary dictionary];
for (NSString * component in components)
{
if (!firstPass)
{
NSString * realString = component;
static NSString * ctrlSEQ[] = {
// Foreground LCL_*
@"0;30m"/*black*/,@"0;31m"/*red*/,@"0;32m"/*green*/,@"0;33m"/*yellow*/,@"0;34m"/*blue*/,@"0;35m"/*magenta*/,@"0;36m"/*cyan*/,@"0;37m"/*white*/,
// Background LBCL_*
@"0;40m"/*black*/,@"0;41m"/*red*/,@"0;42m"/*green*/,@"0;43m"/*yellow*/,@"0;44m"/*blue*/,@"0;45m"/*magenta*/,@"0;46m"/*cyan*/,@"0;47m"/*white*/,
@"0m"/*nothing*/,@"00m"/*nothing*/};
int i;
for (i = 0; i < sizeof(ctrlSEQ)/sizeof(ctrlSEQ[0]); i++)
{
if ([component rangeOfString:ctrlSEQ[i]].location != NSNotFound)
{
switch(i)
{
case 0:
[attrs setObject:[XcodeColors blackColor] forKey:NSForegroundColorAttributeName];
break;
case 1:
[attrs setObject:[XcodeColors redColor] forKey:NSForegroundColorAttributeName];
break;
case 2:
[attrs setObject:[XcodeColors greenColor] forKey:NSForegroundColorAttributeName];
break;
case 3:
[attrs setObject:[XcodeColors yellowColor] forKey:NSForegroundColorAttributeName];
break;
case 4:
[attrs setObject:[XcodeColors blueColor] forKey:NSForegroundColorAttributeName];
break;
case 5:
[attrs setObject:[XcodeColors magentaColor] forKey:NSForegroundColorAttributeName];
break;
case 6:
[attrs setObject:[XcodeColors cyanColor] forKey:NSForegroundColorAttributeName];
break;
case 7:
[attrs setObject:[XcodeColors whiteColor] forKey:NSForegroundColorAttributeName];
break;
case 8:
[attrs setObject:[XcodeColors blackBackgroundColor] forKey:NSBackgroundColorAttributeName];
break;
case 9:
[attrs setObject:[XcodeColors redBackgroundColor] forKey:NSBackgroundColorAttributeName];
break;
case 10:
[attrs setObject:[XcodeColors greenBackgroundColor] forKey:NSBackgroundColorAttributeName];
break;
case 11:
[attrs setObject:[XcodeColors yellowBackgroundColor] forKey:NSBackgroundColorAttributeName];
break;
case 12:
[attrs setObject:[XcodeColors blueBackgroundColor] forKey:NSBackgroundColorAttributeName];
break;
case 13:
[attrs setObject:[XcodeColors magentaBackgroundColor] forKey:NSBackgroundColorAttributeName];
break;
case 14:
[attrs setObject:[XcodeColors cyanBackgroundColor] forKey:NSBackgroundColorAttributeName];
break;
case 15:
[attrs setObject:[XcodeColors whiteBackgroundColor] forKey:NSBackgroundColorAttributeName];
break;
default:
[attrs removeObjectForKey:NSForegroundColorAttributeName];
[attrs removeObjectForKey:NSBackgroundColorAttributeName];
}
realString = [component substringFromIndex:[ctrlSEQ[i] length]];
[clearSEQ addObject:[NSValue valueWithRange:NSMakeRange(componentRange.location - [seq length],[ctrlSEQ[i] length] + [seq length])]];
break;
}
}
}
componentRange.length = [component length];
[textStorage addAttributes:attrs range:componentRange];
componentRange.location += componentRange.length + [seq length];
firstPass = NO;
}
for (NSValue * clearValue in clearSEQ)
{
NSRange range = [clearValue rangeValue];
[textStorage addAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:0.001],NSFontAttributeName,
[NSColor clearColor],NSForegroundColorAttributeName,
nil] range:range];
}
}
}
-(void)fixAttributesInRange:(NSRange)aRange// NSTextStorage
{
imp_ts_fixAttributesInRange(self,_cmd,aRange);
if (getenv(XCODE_COLORS) && !strcmp(getenv(XCODE_COLORS),"YES"))
{
ApplyANSIColors(self,aRange,LC_SEQ_MAC);
ApplyANSIColors(self,aRange,LC_SEQ_IOS);
}
}
@end
@implementation XcodeColors
+(void)pluginDidLoad:(id)xcodeDirectCompatibility
{
/* nothing */
NSLog(@"%s",__PRETTY_FUNCTION__);
}
-(void)registerLaunchSystemDescriptions
{
/* nothing */
NSLog(@"%s",__PRETTY_FUNCTION__);
}
+(void)load
{
NSLog(@"%s,v,9",__PRETTY_FUNCTION__);
if (getenv(XCODE_COLORS) && !strcmp(getenv(XCODE_COLORS), "YES"))
return;
imp_ts_fixAttributesInRange = ReplaceInstanceMethod(NSTextStorage,fixAttributesInRange:,XcodeColors_NSTextStorage);
setenv(XCODE_COLORS, "YES", 0);
}
+(NSString*)defaultColorKeyByName:(NSString*)colorName
{
return [NSString stringWithFormat:@"ColorLog_%@",colorName];
}
+(NSColor*)defaultColorWithName:(NSString*)colorName defaultColor:(NSColor*)color
{
NSUserDefaults * pref = [NSUserDefaults standardUserDefaults];
id defColor = [pref objectForKey:[self defaultColorKeyByName:colorName]];
if (!defColor)
{
if (!color)
return nil;
[pref setObject:[NSArchiver archivedDataWithRootObject:color] forKey:[self defaultColorKeyByName:colorName]];
return color;
}
if ([defColor isKindOfClass:[NSData class]])
defColor = [NSUnarchiver unarchiveObjectWithData:defColor];
if ([defColor isKindOfClass:[NSColor class]])
return defColor;
if ([defColor isKindOfClass:[NSString class]])
{
NSArray * components = [defColor componentsSeparatedByString:@","];
if ([components count] == 4)
{
return [NSColor
colorWithDeviceRed:[[components objectAtIndex:0] floatValue]
green:[[components objectAtIndex:1] floatValue]
blue:[[components objectAtIndex:2] floatValue]
alpha:[[components objectAtIndex:3] floatValue]];
}
if ([components count] == 1)
{
SEL sel = NSSelectorFromString(defColor);
if (sel && [NSColor respondsToSelector:sel])
return [NSColor performSelector:sel];
}
}
return color;
}
// Foreground
+(NSColor*)blackColor
{
return [self defaultColorWithName:@"blackColor" defaultColor:[NSColor blackColor]];
}
+(NSColor*)redColor
{
return [self defaultColorWithName:@"redColor" defaultColor:
[NSColor colorWithCalibratedRed:0x89/255.0 green:0x2A/255.0 blue:0x27/255.0 alpha:0xFF/255.0]/* 892A27 */
];
}
+(NSColor*)greenColor
{
return [self defaultColorWithName:@"greenColor" defaultColor:
[NSColor colorWithCalibratedRed:0x1A/255.0 green:0x89/255.0 blue:0x3B/255.0 alpha:0xFF/255.0]/* 1A893B */
];
}
+(NSColor*)yellowColor
{
return [self defaultColorWithName:@"yellowColor" defaultColor:
[NSColor colorWithCalibratedRed:0xD8/255.0 green:0xD2/255.0 blue:0x53/255.0 alpha:0xFF/255.0]/* D8D253 */
];
}
+(NSColor*)blueColor
{
return [self defaultColorWithName:@"blueColor" defaultColor:
[NSColor colorWithCalibratedRed:0x41/255.0 green:0x87/255.0 blue:0xD8/255.0 alpha:0xFF/255.0]/* 4187D8 */
];
}
+(NSColor*)magentaColor
{
return [self defaultColorWithName:@"magentaColor" defaultColor:
[NSColor colorWithCalibratedRed:0xC2/255.0 green:0x5E/255.0 blue:0xD8/255.0 alpha:0xFF/255.0]/* C25ED8 */
];
}
+(NSColor*)cyanColor
{
return [self defaultColorWithName:@"cyanColor" defaultColor:
[NSColor colorWithCalibratedRed:0x3F/255.0 green:0xC6/255.0 blue:0xD8/255.0 alpha:0xFF/255.0]/* 3FC6D8 */
];
}
+(NSColor*)whiteColor
{
return [self defaultColorWithName:@"whiteColor" defaultColor:
[NSColor colorWithCalibratedRed:0xCC/255.0 green:0xCB/255.0 blue:0xC9/255.0 alpha:0xFF/255.0]/* CCCBC9 */
];
}
// Background
+(NSColor*)blackBackgroundColor
{
return [self defaultColorWithName:@"blackBackgroundColor" defaultColor:[NSColor blackColor]];
}
+(NSColor*)redBackgroundColor
{
return [self defaultColorWithName:@"redBackgroundColor" defaultColor:
[NSColor colorWithCalibratedRed:0xCC/255.0 green:0x8B/255.0 blue:0x8D/255.0 alpha:0xFF/255.0]/* CC8B8D */
];
}
+(NSColor*)greenBackgroundColor
{
return [self defaultColorWithName:@"greenBackgroundColor" defaultColor:
[NSColor colorWithCalibratedRed:0xAA/255.0 green:0xCC/255.0 blue:0xAD/255.0 alpha:0xFF/255.0]/* AACCAD */
];
}
+(NSColor*)yellowBackgroundColor
{
return [self defaultColorWithName:@"yellowBackgroundColor" defaultColor:
[NSColor colorWithCalibratedRed:0xCC/255.0 green:0xBC/255.0 blue:0x91/255.0 alpha:0xFF/255.0]/* CCBC91 */
];
}
+(NSColor*)blueBackgroundColor
{
return [self defaultColorWithName:@"blueBackgroundColor" defaultColor:
[NSColor colorWithCalibratedRed:0xA9/255.0 green:0xC0/255.0 blue:0xCC/255.0 alpha:0xFF/255.0]/* A9C0CC */
];
}
+(NSColor*)magentaBackgroundColor
{
return [self defaultColorWithName:@"magentaBackgroundColor" defaultColor:
[NSColor colorWithCalibratedRed:0xCC/255.0 green:0xB1/255.0 blue:0xC0/255.0 alpha:0xFF/255.0]/* CCB1C0 */
];
}
+(NSColor*)cyanBackgroundColor
{
return [self defaultColorWithName:@"cyanBackgroundColor" defaultColor:
[NSColor colorWithCalibratedRed:0xB3/255.0 green:0xCA/255.0 blue:0xCC/255.0 alpha:0xFF/255.0]/* B3CACC */
];
}
+(NSColor*)whiteBackgroundColor
{
return [self defaultColorWithName:@"whiteBackgroundColor" defaultColor:[NSColor whiteColor]];
}
@end