-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathWLAnsiColorOperationManager.m
446 lines (407 loc) · 17.8 KB
/
WLAnsiColorOperationManager.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//
// WLAnsiColorOperationManager.m
// Welly
//
// Created by K.O.ed on 09-4-1.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "WLAnsiColorOperationManager.h"
#import "WLTerminal.h"
#import "WLConnection.h"
#import "WLSite.h"
#import "WLGlobalConfig.h"
#import "WLEncoder.h"
inline void clearNonANSIAttribute(cell *aCell);
void clearNonANSIAttribute(cell *aCell) {
/* Clear non-ANSI related properties. */
aCell->attr.f.doubleByte = 0;
aCell->attr.f.url = 0;
aCell->attr.f.nothing = 0;
}
unsigned short doubleByteToEncodingCode(unsigned char left, unsigned char right) {
return (left << 8) + right - 0x8000;
}
unsigned char encodingCodeToLeftByte(unsigned short code) {
return code >> 8;
}
unsigned char encodingCodeToRightByte(unsigned short code) {
return code & 0xFF;
}
void convertToUTF8(cell *buffer, int bufferLength, WLEncoding encoding) {
for (int i = 0; i < bufferLength; ++i) {
if (buffer[i].attr.f.doubleByte == 1) {
unsigned short code = doubleByteToEncodingCode(buffer[i].byte, buffer[i+1].byte);
unichar ch = [WLEncoder toUnicode:code encoding:encoding];
buffer[i].byte = encodingCodeToLeftByte(ch);
buffer[i+1].byte = encodingCodeToRightByte(ch);
++i; // Skip next one
}
}
}
void convertFromUTF8(cell *buffer, int bufferLength, WLEncoding encoding) {
for (int i = 0; i < bufferLength; ++i) {
if (buffer[i].attr.f.doubleByte == 1) {
unsigned short code = doubleByteToEncodingCode(buffer[i].byte, buffer[i+1].byte) + 0x8000;
unichar ch = [WLEncoder fromUnicode:code encoding:encoding];
buffer[i].byte = encodingCodeToLeftByte(ch);
buffer[i+1].byte = encodingCodeToRightByte(ch);
++i; // Skip next one
}
}
}
@implementation WLAnsiColorOperationManager
const cell WLWhiteSpaceCell = {WLWhitespaceCharacter, 0};
+ (NSData *)ansiColorDataFromTerminal:(WLTerminal *)terminal
atLocation:(int)location
length:(int)length {
int maxRow = [[WLGlobalConfig sharedInstance] row];
int maxColumn = [[WLGlobalConfig sharedInstance] column];
cell *buffer = (cell *)malloc((length + maxRow + maxColumn + 1) * sizeof(cell));
int i, j;
int bufferLength = 0;
int emptyCount = 0;
for (i = 0; i < length; i++) {
int index = location + i;
cell *currentRow = [terminal cellsOfRow:(index / maxColumn)];
if ((index % maxColumn == 0) && (index != location)) {
buffer[bufferLength].byte = WLNewlineCharacter;
buffer[bufferLength].attr = buffer[bufferLength - 1].attr;
bufferLength++;
emptyCount = 0;
}
if (!isEmptyCell(currentRow[index % maxColumn])) {
for (j = 0; j < emptyCount; j++) {
buffer[bufferLength] = WLWhiteSpaceCell;
bufferLength++;
}
buffer[bufferLength] = currentRow[index % maxColumn];
if (buffer[bufferLength].byte == WLNullTerminator)
buffer[bufferLength].byte = WLWhitespaceCharacter;
//clearNonANSIAttribute(&buffer[bufferLength]);
bufferLength++;
emptyCount = 0;
} else {
emptyCount++;
}
}
convertToUTF8(buffer, bufferLength, [[[terminal connection] site] encoding]);
NSData *returnValue = [NSData dataWithBytes:buffer length:bufferLength * sizeof(cell)];
free(buffer);
return returnValue;
}
+ (NSData *)ansiColorDataFromTerminal:(WLTerminal *)terminal
inRect:(NSRect)rect {
int maxRow = [[WLGlobalConfig sharedInstance] row];
int maxColumn = [[WLGlobalConfig sharedInstance] column];
cell *buffer = (cell *)malloc(((rect.size.height * rect.size.width) + maxRow + maxColumn + 1) * sizeof(cell));
int j;
int bufferLength = 0;
int emptyCount = 0;
for (int r = rect.origin.y; r < rect.origin.y + rect.size.height; ++r) {
cell *currentRow = [terminal cellsOfRow:r];
// Copy 'selectedRect.size.width' bytes from (r, selectedRect.origin.x)
for (int c = rect.origin.x; c < rect.origin.x + rect.size.width; ++c) {
if (!isEmptyCell(currentRow[c])) {
for (j = 0; j < emptyCount; j++) {
buffer[bufferLength] = WLWhiteSpaceCell;
bufferLength++;
}
buffer[bufferLength] = currentRow[c];
if (buffer[bufferLength].byte == WLNullTerminator)
buffer[bufferLength].byte = WLWhitespaceCharacter;
//clearNonANSIAttribute(&buffer[bufferLength]);
bufferLength++;
emptyCount = 0;
} else {
emptyCount++;
}
}
// Check if we should fill remaining empty count:
if (emptyCount > 0) {
for (int c = rect.origin.x + rect.size.width; c < maxColumn; ++c) {
if (!isEmptyCell(currentRow[c])) {
for (j = 0; j < emptyCount; j++) {
buffer[bufferLength] = WLWhiteSpaceCell;
bufferLength++;
}
buffer[bufferLength] = currentRow[c];
if (buffer[bufferLength].byte == WLNullTerminator)
buffer[bufferLength].byte = WLWhitespaceCharacter;
//clearNonANSIAttribute(&buffer[bufferLength]);
bufferLength++;
break;
}
}
}
// add '\n'
if (r == rect.origin.y + rect.size.height - 1)
break;
buffer[bufferLength].byte = WLNewlineCharacter;
buffer[bufferLength].attr = buffer[bufferLength - 1].attr;
bufferLength++;
emptyCount = 0;
}
convertToUTF8(buffer, bufferLength, [[[terminal connection] site] encoding]);
NSData *returnValue = [NSData dataWithBytes:buffer length:bufferLength * sizeof(cell)];
free(buffer);
return returnValue;
}
+ (NSData *)ansiCodeFromANSIColorData:(NSData *)ansiColorData
forANSIColorKey:(YLANSIColorKey)ansiColorKey
encoding:(WLEncoding)encoding {
NSData *escData;
if (ansiColorKey == YLCtrlUANSIColorKey) {
escData = [NSData dataWithBytes:"\x15" length:1];
} else if (ansiColorKey == YLEscEscANSIColorKey) {
escData = [NSData dataWithBytes:"\x1B\x1B" length:2];
} else {
escData = [NSData dataWithBytes:"\x1B" length:1];
}
cell *buffer = (cell *)[ansiColorData bytes];
int bufferLength = [ansiColorData length] / sizeof(cell);
convertFromUTF8(buffer, bufferLength, encoding);
attribute defaultANSI;
unsigned int bgColorIndex = [WLGlobalConfig sharedInstance]->_bgColorIndex;
unsigned int fgColorIndex = [WLGlobalConfig sharedInstance]->_fgColorIndex;
defaultANSI.f.bgColor = bgColorIndex;
defaultANSI.f.fgColor = fgColorIndex;
defaultANSI.f.blink = 0;
defaultANSI.f.bold = 0;
defaultANSI.f.underline = 0;
defaultANSI.f.reverse = 0;
attribute previousANSI = defaultANSI;
NSMutableData *writeBuffer = [NSMutableData data];
int i;
for (i = 0; i < bufferLength; i++) {
if (buffer[i].byte == '\n' ) {
previousANSI = defaultANSI;
[writeBuffer appendData:escData];
[writeBuffer appendBytes:"[m\r" length:3];
continue;
}
attribute currentANSI = buffer[i].attr;
char tmp[100];
tmp[0] = '\0';
/* Unchanged */
if ((currentANSI.f.blink == previousANSI.f.blink) &&
(currentANSI.f.bold == previousANSI.f.bold) &&
(currentANSI.f.underline == previousANSI.f.underline) &&
(currentANSI.f.reverse == previousANSI.f.reverse) &&
(currentANSI.f.bgColor == previousANSI.f.bgColor) &&
(currentANSI.f.fgColor == previousANSI.f.fgColor)) {
[writeBuffer appendBytes: &(buffer[i].byte) length: 1];
continue;
}
/* Clear */
if ((currentANSI.f.blink == 0 && previousANSI.f.blink == 1) ||
(currentANSI.f.bold == 0 && previousANSI.f.bold == 1) ||
(currentANSI.f.underline == 0 && previousANSI.f.underline == 1) ||
(currentANSI.f.reverse == 0 && previousANSI.f.reverse == 1) ||
(currentANSI.f.bgColor == bgColorIndex && previousANSI.f.reverse != bgColorIndex) ) {
strcpy(tmp, "[0");
if (currentANSI.f.blink == 1) strcat(tmp, ";5");
if (currentANSI.f.bold == 1) strcat(tmp, ";1");
if (currentANSI.f.underline == 1) strcat(tmp, ";4");
if (currentANSI.f.reverse == 1) strcat(tmp, ";7");
if (currentANSI.f.fgColor != fgColorIndex) sprintf(tmp, "%s;%d", tmp, currentANSI.f.fgColor + 30);
if (currentANSI.f.bgColor != bgColorIndex) sprintf(tmp, "%s;%d", tmp, currentANSI.f.bgColor + 40);
strcat(tmp, "m");
[writeBuffer appendData:escData];
[writeBuffer appendBytes:tmp length:strlen(tmp)];
[writeBuffer appendBytes:&(buffer[i].byte) length:1];
previousANSI = currentANSI;
continue;
}
/* Add attribute */
strcpy(tmp, "[");
if (currentANSI.f.blink == 1 && previousANSI.f.blink == 0)
strcat(tmp, "5;");
if (currentANSI.f.bold == 1 && previousANSI.f.bold == 0)
strcat(tmp, "1;");
if (currentANSI.f.underline == 1 && previousANSI.f.underline == 0)
strcat(tmp, "4;");
if (currentANSI.f.reverse == 1 && previousANSI.f.reverse == 0)
strcat(tmp, "7;");
if (currentANSI.f.fgColor != previousANSI.f.fgColor)
sprintf(tmp, "%s%d;", tmp, currentANSI.f.fgColor + 30);
if (currentANSI.f.bgColor != previousANSI.f.bgColor)
sprintf(tmp, "%s%d;", tmp, currentANSI.f.bgColor + 40);
tmp[strlen(tmp) - 1] = 'm';
sprintf(tmp, "%s%c", tmp, buffer[i].byte);
[writeBuffer appendData:escData];
[writeBuffer appendBytes:tmp length:strlen(tmp)];
previousANSI = currentANSI;
continue;
}
[writeBuffer appendData:escData];
[writeBuffer appendBytes:"[m" length:2];
return writeBuffer;
}
static NSColor* colorUsingNearestAnsiColor(NSColor *rawColor, BOOL isBackground) {
if (!rawColor)
return nil;
WLGlobalConfig *config = [WLGlobalConfig sharedInstance];
if ([rawColor isEqual:[config colorBG]] ||
[rawColor isEqual:[config colorBlack]] ||
[rawColor isEqual:[config colorRed]] ||
[rawColor isEqual:[config colorGreen]] ||
[rawColor isEqual:[config colorYellow]] ||
[rawColor isEqual:[config colorBlue]] ||
[rawColor isEqual:[config colorMagenta]] ||
[rawColor isEqual:[config colorCyan]] ||
[rawColor isEqual:[config colorWhite]] ||
[rawColor isEqual:[config colorBGHilite]] ||
[rawColor isEqual:[config colorBlackHilite]] ||
[rawColor isEqual:[config colorRedHilite]] ||
[rawColor isEqual:[config colorGreenHilite]] ||
[rawColor isEqual:[config colorYellowHilite]] ||
[rawColor isEqual:[config colorBlueHilite]] ||
[rawColor isEqual:[config colorMagentaHilite]] ||
[rawColor isEqual:[config colorCyanHilite]] ||
[rawColor isEqual:[config colorWhiteHilite]])
return rawColor;
CGFloat h, s, b;
[[rawColor colorUsingColorSpaceName:@"NSCalibratedRGBColorSpace"] getHue:&h saturation:&s brightness:&b alpha:nil];
if (s < 0.05) {
if (isBackground)
return [config colorBG];
if (!isBackground && b < 0.05)
return [config colorWhite];
switch ((int)(b * 4)) {
case 0:
return [config colorBlack];
case 1:
return [config colorBlackHilite];
case 2:
return [config colorWhite];
default:
return [config colorWhiteHilite];
}
}
if (b < 0.05)
return [config colorBlack];
switch ((int)((h + 1.0/6/2) * 6)) {
case 0:
case 6:
return (b < 0.5) ? [config colorRed] : [config colorRedHilite];
case 1:
return (b < 0.5) ? [config colorYellow] : [config colorYellowHilite];
case 2:
return (b < 0.5) ? [config colorGreen] : [config colorGreenHilite];
case 3:
return (b < 0.5) ? [config colorCyan] : [config colorCyanHilite];
case 4:
return (b < 0.5) ? [config colorBlue] : [config colorBlueHilite];
case 5:
return (b < 0.5) ? [config colorMagenta] : [config colorMagentaHilite];
default:
return [config colorWhite];
}
}
+ (NSString *)ansiCodeStringFromAttributedString:(NSAttributedString *)storage
forANSIColorKey:(YLANSIColorKey)ansiColorKey {
NSString *escString;
if (ansiColorKey == YLCtrlUANSIColorKey) {
escString = @"\x15";
} else if (ansiColorKey == YLEscEscANSIColorKey) {
escString = @"\x1B\x1B";
} else {
escString = @"\x1B";
}
//NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSMutableString *writeBuffer = [NSMutableString string];
NSString *rawString = [storage string];
BOOL underline, preUnderline = NO;
BOOL blink, preBlink = NO;
WLGlobalConfig *config = [WLGlobalConfig sharedInstance];
NSColor *color, *preColor = [config colorWhite];
NSColor *bgColor, *preBgColor = nil;
BOOL hasColor = NO;
for (int i = 0; i < [storage length]; ++i) {
char tmp[100] = "";
// get attributes of i-th character
underline = ([[storage attribute:NSUnderlineStyleAttributeName atIndex:i effectiveRange:nil] intValue] != NSUnderlineStyleNone);
//blink = [fontManager traitsOfFont:[storage attribute:NSFontAttributeName atIndex:i effectiveRange:nil]] & NSBoldFontMask;
blink = ([storage attribute:NSShadowAttributeName atIndex:i effectiveRange:nil] != nil);
color = colorUsingNearestAnsiColor([storage attribute:NSForegroundColorAttributeName atIndex:i effectiveRange:nil], NO);
bgColor = colorUsingNearestAnsiColor([storage attribute:NSBackgroundColorAttributeName atIndex:i effectiveRange:nil], YES);
/* Add attributes */
if ((underline != preUnderline) ||
(blink != preBlink) ||
(color != preColor) ||
(bgColor && ![bgColor isEqual:preBgColor]) || (!bgColor && preBgColor)) {
// pre-calculate background color
char bgColorCode[4] = "";
if (!bgColor || [bgColor isEqual:[config colorBG]] || [bgColor isEqual:[config colorBGHilite]])
/* do nothing */;
else if ([bgColor isEqual:[config colorBlack]] || [bgColor isEqual:[config colorBlackHilite]])
strcpy(bgColorCode, ";40");
else if ([bgColor isEqual:[config colorRed]] || [bgColor isEqual:[config colorRedHilite]])
strcpy(bgColorCode, ";41");
else if ([bgColor isEqual:[config colorGreen]] || [bgColor isEqual:[config colorGreenHilite]])
strcpy(bgColorCode, ";42");
else if ([bgColor isEqual:[config colorYellow]] || [bgColor isEqual:[config colorYellowHilite]])
strcpy(bgColorCode, ";43");
else if ([bgColor isEqual:[config colorBlue]] || [bgColor isEqual:[config colorBlueHilite]])
strcpy(bgColorCode, ";44");
else if ([bgColor isEqual:[config colorMagenta]] || [bgColor isEqual:[config colorMagentaHilite]])
strcpy(bgColorCode, ";45");
else if ([bgColor isEqual:[config colorCyan]] || [bgColor isEqual:[config colorCyanHilite]])
strcpy(bgColorCode, ";46");
else if ([bgColor isEqual:[config colorWhite]] || [bgColor isEqual:[config colorWhiteHilite]])
strcpy(bgColorCode, ";47");
// merge foreground color, underline, blink and background color
if (color == [config colorBlack])
sprintf(tmp, "[0;%s%s30%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorRed])
sprintf(tmp, "[0;%s%s31%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorGreen])
sprintf(tmp, "[0;%s%s32%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorYellow])
sprintf(tmp, "[0;%s%s33%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorBlue])
sprintf(tmp, "[0;%s%s34%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorMagenta])
sprintf(tmp, "[0;%s%s35%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorCyan])
sprintf(tmp, "[0;%s%s36%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorWhite])
sprintf(tmp, "[0;%s%s37%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorBlackHilite])
sprintf(tmp, "[0;1;%s%s30%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorRedHilite])
sprintf(tmp, "[0;1;%s%s31%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorGreenHilite])
sprintf(tmp, "[0;1;%s%s32%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorYellowHilite])
sprintf(tmp, "[0;1;%s%s33%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorBlueHilite])
sprintf(tmp, "[0;1;%s%s34%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorMagentaHilite])
sprintf(tmp, "[0;1;%s%s35%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorCyanHilite])
sprintf(tmp, "[0;1;%s%s36%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else if (color == [config colorWhiteHilite])
sprintf(tmp, "[0;1;%s%s37%sm", underline ? "4;" : "", blink ? "5;" : "", bgColorCode);
else
sprintf(tmp, "[%s%s%s%sm", (underline || blink || *bgColorCode) ? "0" : "", underline ? ";4" : "", blink ? ";5" : "", bgColorCode);
[writeBuffer appendString:escString];
[writeBuffer appendString:[NSString stringWithCString:tmp encoding:NSASCIIStringEncoding]];
preUnderline = underline;
preBlink = blink;
preColor = color;
preBgColor = bgColor;
hasColor = YES;
}
// get i-th character
unichar ch = [rawString characterAtIndex:i];
// write to the buffer
[writeBuffer appendString:[NSString stringWithCharacters:&ch length:1]];
}
if (hasColor) {
[writeBuffer appendString:escString];
[writeBuffer appendString:@"[m"];
}
return writeBuffer;
}
@end