-
Notifications
You must be signed in to change notification settings - Fork 32
/
ANSIEscapeHelper.m
983 lines (861 loc) · 36.2 KB
/
ANSIEscapeHelper.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
//
// ANSIEscapeHelper.m
//
// Created by Ali Rantakari on 18.3.09.
/*
The MIT License
Copyright (c) 2008-2009 Ali Rantakari
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
todo:
- don't add useless "reset" escape codes to the string in
-ansiEscapedStringWithAttributedString:
*/
#import "ANSIEscapeHelper.h"
@implementation ANSIEscapeHelper
@synthesize font;
@synthesize ansiColors;
@synthesize defaultStringColor;
- (id) init
{
if (( self = [super init] ))
{
self.font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
self.defaultStringColor = [NSColor blackColor];
self.ansiColors = [NSMutableDictionary dictionary];
}
return self;
}
- (void) dealloc
{
self.font = nil;
self.ansiColors = nil;
self.defaultStringColor = nil;
[super dealloc];
}
- (NSAttributedString*) attributedStringWithANSIEscapedString:(NSString*)aString
{
if (aString == nil)
return nil;
NSString *cleanString;
NSArray *attributesAndRanges = [self attributesForString:aString cleanString:&cleanString];
NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc]
initWithString:cleanString
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
self.font, NSFontAttributeName,
self.defaultStringColor, NSForegroundColorAttributeName,
nil
]
] autorelease];
NSDictionary *thisAttributeDict;
for (thisAttributeDict in attributesAndRanges)
{
[attributedString
addAttribute:[thisAttributeDict objectForKey:kAttrDictKey_attrName]
value:[thisAttributeDict objectForKey:kAttrDictKey_attrValue]
range:[[thisAttributeDict objectForKey:kAttrDictKey_range] rangeValue]
];
}
return attributedString;
}
- (NSString*) ansiEscapedStringWithAttributedString:(NSAttributedString*)aAttributedString
{
NSRange limitRange;
NSRange effectiveRange;
id attributeValue;
NSMutableArray *codesAndLocations = [NSMutableArray array];
NSArray *attrNames = [NSArray arrayWithObjects:
NSFontAttributeName, NSForegroundColorAttributeName,
NSBackgroundColorAttributeName, NSUnderlineStyleAttributeName,
nil
];
NSString *thisAttrName;
for (thisAttrName in attrNames)
{
limitRange = NSMakeRange(0, [aAttributedString length]);
while (limitRange.length > 0)
{
attributeValue = [aAttributedString
attribute:thisAttrName
atIndex:limitRange.location
longestEffectiveRange:&effectiveRange
inRange:limitRange
];
enum sgrCode thisSGRCode = SGRCodeNoneOrInvalid;
if ([thisAttrName isEqualToString:NSForegroundColorAttributeName])
{
if (attributeValue != nil)
thisSGRCode = [self closestSGRCodeForColor:attributeValue isForegroundColor:YES];
else
thisSGRCode = SGRCodeFgReset;
}
else if ([thisAttrName isEqualToString:NSBackgroundColorAttributeName])
{
if (attributeValue != nil)
thisSGRCode = [self closestSGRCodeForColor:attributeValue isForegroundColor:NO];
else
thisSGRCode = SGRCodeBgReset;
}
else if ([thisAttrName isEqualToString:NSFontAttributeName])
{
// we currently only use NSFontAttributeName for bolding so
// here we assume that the formatting "type" in ANSI SGR
// terms is indeed intensity
if (attributeValue != nil)
thisSGRCode = ([[NSFontManager sharedFontManager] weightOfFont:attributeValue] >= kBoldFontMinWeight)
? SGRCodeIntensityBold : SGRCodeIntensityNormal;
else
thisSGRCode = SGRCodeIntensityNormal;
}
else if ([thisAttrName isEqualToString:NSUnderlineStyleAttributeName])
{
if (attributeValue != nil)
{
if ([attributeValue intValue] == NSUnderlineStyleSingle)
thisSGRCode = SGRCodeUnderlineSingle;
else if ([attributeValue intValue] == NSUnderlineStyleDouble)
thisSGRCode = SGRCodeUnderlineDouble;
else
thisSGRCode = SGRCodeUnderlineNone;
}
else
thisSGRCode = SGRCodeUnderlineNone;
}
if (thisSGRCode != SGRCodeNoneOrInvalid)
{
[codesAndLocations addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:thisSGRCode], kCodeDictKey_code,
[NSNumber numberWithUnsignedInteger:effectiveRange.location], kCodeDictKey_location,
nil
]
];
}
limitRange = NSMakeRange(NSMaxRange(effectiveRange),
NSMaxRange(limitRange) - NSMaxRange(effectiveRange));
}
}
NSString *ansiEscapedString = [self ansiEscapedStringWithCodesAndLocations:codesAndLocations cleanString:[aAttributedString string]];
return ansiEscapedString;
}
- (NSArray*) escapeCodesForString:(NSString*)aString cleanString:(NSString**)aCleanString
{
if (aString == nil)
return nil;
if ([aString length] <= [kANSIEscapeCSI length])
{
*aCleanString = [NSString stringWithString:aString];
return [NSArray array];
}
NSString *cleanString = @"";
// find all escape sequence codes from aString and put them in this array
// along with their start locations within the "clean" version of aString
NSMutableArray *formatCodes = [NSMutableArray array];
NSUInteger aStringLength = [aString length];
NSUInteger coveredLength = 0;
NSRange searchRange = NSMakeRange(0,aStringLength);
NSRange thisEscapeSequenceRange;
do
{
thisEscapeSequenceRange = [aString rangeOfString:kANSIEscapeCSI options:NSLiteralSearch range:searchRange];
if (thisEscapeSequenceRange.location != NSNotFound)
{
// adjust range's length so that it encompasses the whole ANSI escape sequence
// and not just the Control Sequence Initiator (the "prefix") by finding the
// final byte of the control sequence (one that has an ASCII decimal value
// between 64 and 126.) at the same time, read all formatting codes from inside
// this escape sequence (there may be several, separated by semicolons.)
NSMutableArray *codes = [NSMutableArray array];
unsigned int code = 0;
unsigned int lengthAddition = 1;
NSUInteger thisIndex;
for (;;)
{
thisIndex = (NSMaxRange(thisEscapeSequenceRange)+lengthAddition-1);
if (thisIndex >= aStringLength)
break;
int c = (int)[aString characterAtIndex:thisIndex];
if ((48 <= c) && (c <= 57)) // 0-9
{
int digit = c-48;
code = (code == 0) ? digit : code*10+digit;
}
// ASCII decimal 109 is the SGR (Select Graphic Rendition) final byte
// ("m"). this means that the code value we've just read specifies formatting
// for the output; exactly what we're interested in.
if (c == 109)
{
[codes addObject:[NSNumber numberWithUnsignedInt:code]];
break;
}
else if ((64 <= c) && (c <= 126)) // any other valid final byte
{
[codes removeAllObjects];
break;
}
else if (c == 59) // semicolon (;) separates codes within the same sequence
{
[codes addObject:[NSNumber numberWithUnsignedInt:code]];
code = 0;
}
lengthAddition++;
}
thisEscapeSequenceRange.length += lengthAddition;
NSUInteger locationInCleanString = coveredLength+thisEscapeSequenceRange.location-searchRange.location;
NSUInteger iCode;
for (iCode = 0; iCode < [codes count]; iCode++)
{
[formatCodes addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[codes objectAtIndex:iCode], kCodeDictKey_code,
[NSNumber numberWithUnsignedInteger:locationInCleanString], kCodeDictKey_location,
nil
]
];
}
NSUInteger thisCoveredLength = thisEscapeSequenceRange.location-searchRange.location;
if (thisCoveredLength > 0)
cleanString = [cleanString stringByAppendingString:[aString substringWithRange:NSMakeRange(searchRange.location, thisCoveredLength)]];
coveredLength += thisCoveredLength;
searchRange.location = NSMaxRange(thisEscapeSequenceRange);
searchRange.length = aStringLength-searchRange.location;
}
}
while(thisEscapeSequenceRange.location != NSNotFound);
if (searchRange.length > 0)
cleanString = [cleanString stringByAppendingString:[aString substringWithRange:searchRange]];
*aCleanString = cleanString;
return formatCodes;
}
- (NSString*) ansiEscapedStringWithCodesAndLocations:(NSArray*)aCodesArray cleanString:(NSString*)aCleanString
{
NSMutableString* retStr = [NSMutableString stringWithCapacity:[aCleanString length]];
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:kCodeDictKey_location ascending:YES] autorelease];
NSArray *codesArray = [aCodesArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSUInteger aCleanStringIndex = 0;
NSUInteger aCleanStringLength = [aCleanString length];
NSDictionary *thisCodeDict;
for (thisCodeDict in codesArray)
{
if (!( [[thisCodeDict allKeys] containsObject:kCodeDictKey_code] &&
[[thisCodeDict allKeys] containsObject:kCodeDictKey_location]
))
continue;
enum sgrCode thisCode = [[thisCodeDict objectForKey:kCodeDictKey_code] unsignedIntValue];
NSUInteger formattingRunStartLocation = [[thisCodeDict objectForKey:kCodeDictKey_location] unsignedIntegerValue];
if (formattingRunStartLocation > aCleanStringLength)
continue;
if (aCleanStringIndex < formattingRunStartLocation)
[retStr appendString:[aCleanString substringWithRange:NSMakeRange(aCleanStringIndex, formattingRunStartLocation-aCleanStringIndex)]];
[retStr appendString:kANSIEscapeCSI];
[retStr appendString:[NSString stringWithFormat:@"%d", thisCode]];
[retStr appendString:kANSIEscapeSGREnd];
aCleanStringIndex = formattingRunStartLocation;
}
if (aCleanStringIndex < aCleanStringLength)
[retStr appendString:[aCleanString substringFromIndex:aCleanStringIndex]];
return retStr;
}
- (NSArray*) attributesForString:(NSString*)aString cleanString:(NSString**)aCleanString
{
if (aString == nil)
return nil;
if ([aString length] <= [kANSIEscapeCSI length])
{
if (aCleanString != NULL)
*aCleanString = [NSString stringWithString:aString];
return [NSArray array];
}
NSMutableArray *attrsAndRanges = [NSMutableArray array];
NSString *cleanString;
NSArray *formatCodes = [self escapeCodesForString:aString cleanString:&cleanString];
// go through all the found escape sequence codes and for each one, create
// the string formatting attribute name and value, find the next escape
// sequence that specifies the end of the formatting run started by
// the currently handled code, and generate a range from the difference
// in those codes' locations within the clean aString.
NSUInteger iCode;
for (iCode = 0; iCode < [formatCodes count]; iCode++)
{
NSDictionary *thisCodeDict = [formatCodes objectAtIndex:iCode];
enum sgrCode thisCode = [[thisCodeDict objectForKey:kCodeDictKey_code] unsignedIntValue];
NSUInteger formattingRunStartLocation = [[thisCodeDict objectForKey:kCodeDictKey_location] unsignedIntegerValue];
// the attributed string attribute name for the formatting run introduced
// by this code
NSString *thisAttributeName = nil;
// the attributed string attribute value for this formatting run introduced
// by this code
NSObject *thisAttributeValue = nil;
// set attribute name
switch(thisCode)
{
case SGRCodeFgBlack:
case SGRCodeFgRed:
case SGRCodeFgGreen:
case SGRCodeFgYellow:
case SGRCodeFgBlue:
case SGRCodeFgMagenta:
case SGRCodeFgCyan:
case SGRCodeFgWhite:
case SGRCodeFgBrightBlack:
case SGRCodeFgBrightRed:
case SGRCodeFgBrightGreen:
case SGRCodeFgBrightYellow:
case SGRCodeFgBrightBlue:
case SGRCodeFgBrightMagenta:
case SGRCodeFgBrightCyan:
case SGRCodeFgBrightWhite:
thisAttributeName = NSForegroundColorAttributeName;
break;
case SGRCodeBgBlack:
case SGRCodeBgRed:
case SGRCodeBgGreen:
case SGRCodeBgYellow:
case SGRCodeBgBlue:
case SGRCodeBgMagenta:
case SGRCodeBgCyan:
case SGRCodeBgWhite:
case SGRCodeBgBrightBlack:
case SGRCodeBgBrightRed:
case SGRCodeBgBrightGreen:
case SGRCodeBgBrightYellow:
case SGRCodeBgBrightBlue:
case SGRCodeBgBrightMagenta:
case SGRCodeBgBrightCyan:
case SGRCodeBgBrightWhite:
thisAttributeName = NSBackgroundColorAttributeName;
break;
case SGRCodeIntensityBold:
case SGRCodeIntensityNormal:
thisAttributeName = NSFontAttributeName;
break;
case SGRCodeUnderlineSingle:
case SGRCodeUnderlineDouble:
thisAttributeName = NSUnderlineStyleAttributeName;
break;
default:
continue;
break;
}
// set attribute value
switch(thisCode)
{
case SGRCodeBgBlack:
case SGRCodeFgBlack:
case SGRCodeBgRed:
case SGRCodeFgRed:
case SGRCodeBgGreen:
case SGRCodeFgGreen:
case SGRCodeBgYellow:
case SGRCodeFgYellow:
case SGRCodeBgBlue:
case SGRCodeFgBlue:
case SGRCodeBgMagenta:
case SGRCodeFgMagenta:
case SGRCodeBgCyan:
case SGRCodeFgCyan:
case SGRCodeBgWhite:
case SGRCodeFgWhite:
case SGRCodeBgBrightBlack:
case SGRCodeFgBrightBlack:
case SGRCodeBgBrightRed:
case SGRCodeFgBrightRed:
case SGRCodeBgBrightGreen:
case SGRCodeFgBrightGreen:
case SGRCodeBgBrightYellow:
case SGRCodeFgBrightYellow:
case SGRCodeBgBrightBlue:
case SGRCodeFgBrightBlue:
case SGRCodeBgBrightMagenta:
case SGRCodeFgBrightMagenta:
case SGRCodeBgBrightCyan:
case SGRCodeFgBrightCyan:
case SGRCodeBgBrightWhite:
case SGRCodeFgBrightWhite:
thisAttributeValue = [self colorForSGRCode:thisCode];
break;
case SGRCodeIntensityBold:
{
NSFont *boldFont = [[NSFontManager sharedFontManager] convertFont:self.font toHaveTrait:NSBoldFontMask];
thisAttributeValue = boldFont;
}
break;
case SGRCodeIntensityNormal:
{
NSFont *unboldFont = [[NSFontManager sharedFontManager] convertFont:self.font toHaveTrait:NSUnboldFontMask];
thisAttributeValue = unboldFont;
}
break;
case SGRCodeUnderlineSingle:
thisAttributeValue = [NSNumber numberWithInteger:NSUnderlineStyleSingle];
break;
case SGRCodeUnderlineDouble:
thisAttributeValue = [NSNumber numberWithInteger:NSUnderlineStyleDouble];
break;
default:
break;
}
// find the next sequence that specifies the end of this formatting run
NSInteger formattingRunEndLocation = -1;
if (iCode < ([formatCodes count]-1))
{
NSUInteger iEndCode;
NSDictionary *thisEndCodeCandidateDict;
unichar thisEndCodeCandidate;
for (iEndCode = iCode+1; iEndCode < [formatCodes count]; iEndCode++)
{
thisEndCodeCandidateDict = [formatCodes objectAtIndex:iEndCode];
thisEndCodeCandidate = [[thisEndCodeCandidateDict objectForKey:kCodeDictKey_code] unsignedIntValue];
if ([self sgrCode:thisEndCodeCandidate endsFormattingIntroducedByCode:thisCode])
{
formattingRunEndLocation = [[thisEndCodeCandidateDict objectForKey:kCodeDictKey_location] unsignedIntegerValue];
break;
}
}
}
if (formattingRunEndLocation == -1)
formattingRunEndLocation = [cleanString length];
// add attribute name, attribute value and formatting run range
// to the array we're going to return
[attrsAndRanges addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithRange:NSMakeRange(formattingRunStartLocation, (formattingRunEndLocation-formattingRunStartLocation))], kAttrDictKey_range,
thisAttributeName, kAttrDictKey_attrName,
thisAttributeValue, kAttrDictKey_attrValue,
nil
]
];
}
if (aCleanString != NULL)
*aCleanString = cleanString;
return attrsAndRanges;
}
- (BOOL) sgrCode:(enum sgrCode)endCode endsFormattingIntroducedByCode:(enum sgrCode)startCode
{
switch(startCode)
{
case SGRCodeFgBlack:
case SGRCodeFgRed:
case SGRCodeFgGreen:
case SGRCodeFgYellow:
case SGRCodeFgBlue:
case SGRCodeFgMagenta:
case SGRCodeFgCyan:
case SGRCodeFgWhite:
case SGRCodeFgBrightBlack:
case SGRCodeFgBrightRed:
case SGRCodeFgBrightGreen:
case SGRCodeFgBrightYellow:
case SGRCodeFgBrightBlue:
case SGRCodeFgBrightMagenta:
case SGRCodeFgBrightCyan:
case SGRCodeFgBrightWhite:
return (endCode == SGRCodeAllReset || endCode == SGRCodeFgReset ||
endCode == SGRCodeFgBlack || endCode == SGRCodeFgRed ||
endCode == SGRCodeFgGreen || endCode == SGRCodeFgYellow ||
endCode == SGRCodeFgBlue || endCode == SGRCodeFgMagenta ||
endCode == SGRCodeFgCyan || endCode == SGRCodeFgWhite ||
endCode == SGRCodeFgBrightBlack || endCode == SGRCodeFgBrightRed ||
endCode == SGRCodeFgBrightGreen || endCode == SGRCodeFgBrightYellow ||
endCode == SGRCodeFgBrightBlue || endCode == SGRCodeFgBrightMagenta ||
endCode == SGRCodeFgBrightCyan || endCode == SGRCodeFgBrightWhite);
break;
case SGRCodeBgBlack:
case SGRCodeBgRed:
case SGRCodeBgGreen:
case SGRCodeBgYellow:
case SGRCodeBgBlue:
case SGRCodeBgMagenta:
case SGRCodeBgCyan:
case SGRCodeBgWhite:
case SGRCodeBgBrightBlack:
case SGRCodeBgBrightRed:
case SGRCodeBgBrightGreen:
case SGRCodeBgBrightYellow:
case SGRCodeBgBrightBlue:
case SGRCodeBgBrightMagenta:
case SGRCodeBgBrightCyan:
case SGRCodeBgBrightWhite:
return (endCode == SGRCodeAllReset || endCode == SGRCodeBgReset ||
endCode == SGRCodeBgBlack || endCode == SGRCodeBgRed ||
endCode == SGRCodeBgGreen || endCode == SGRCodeBgYellow ||
endCode == SGRCodeBgBlue || endCode == SGRCodeBgMagenta ||
endCode == SGRCodeBgCyan || endCode == SGRCodeBgWhite ||
endCode == SGRCodeBgBrightBlack || endCode == SGRCodeBgBrightRed ||
endCode == SGRCodeBgBrightGreen || endCode == SGRCodeBgBrightYellow ||
endCode == SGRCodeBgBrightBlue || endCode == SGRCodeBgBrightMagenta ||
endCode == SGRCodeBgBrightCyan || endCode == SGRCodeBgBrightWhite);
break;
case SGRCodeIntensityBold:
case SGRCodeIntensityNormal:
return (endCode == SGRCodeAllReset || endCode == SGRCodeIntensityNormal ||
endCode == SGRCodeIntensityBold || endCode == SGRCodeIntensityFaint);
break;
case SGRCodeUnderlineSingle:
case SGRCodeUnderlineDouble:
return (endCode == SGRCodeAllReset || endCode == SGRCodeUnderlineNone ||
endCode == SGRCodeUnderlineSingle || endCode == SGRCodeUnderlineDouble);
break;
default:
return NO;
break;
}
return NO;
}
- (NSColor*) colorForSGRCode:(enum sgrCode)code
{
if (self.ansiColors != nil)
{
NSColor *preferredColor = [self.ansiColors objectForKey:[NSNumber numberWithInt:code]];
if (preferredColor != nil)
return preferredColor;
}
switch(code)
{
case SGRCodeFgBlack:
return kDefaultANSIColorFgBlack;
break;
case SGRCodeFgRed:
return kDefaultANSIColorFgRed;
break;
case SGRCodeFgGreen:
return kDefaultANSIColorFgGreen;
break;
case SGRCodeFgYellow:
return kDefaultANSIColorFgYellow;
break;
case SGRCodeFgBlue:
return kDefaultANSIColorFgBlue;
break;
case SGRCodeFgMagenta:
return kDefaultANSIColorFgMagenta;
break;
case SGRCodeFgCyan:
return kDefaultANSIColorFgCyan;
break;
case SGRCodeFgWhite:
return kDefaultANSIColorFgWhite;
break;
case SGRCodeFgBrightBlack:
return kDefaultANSIColorFgBrightBlack;
break;
case SGRCodeFgBrightRed:
return kDefaultANSIColorFgBrightRed;
break;
case SGRCodeFgBrightGreen:
return kDefaultANSIColorFgBrightGreen;
break;
case SGRCodeFgBrightYellow:
return kDefaultANSIColorFgBrightYellow;
break;
case SGRCodeFgBrightBlue:
return kDefaultANSIColorFgBrightBlue;
break;
case SGRCodeFgBrightMagenta:
return kDefaultANSIColorFgBrightMagenta;
break;
case SGRCodeFgBrightCyan:
return kDefaultANSIColorFgBrightCyan;
break;
case SGRCodeFgBrightWhite:
return kDefaultANSIColorFgBrightWhite;
break;
case SGRCodeBgBlack:
return kDefaultANSIColorBgBlack;
break;
case SGRCodeBgRed:
return kDefaultANSIColorBgRed;
break;
case SGRCodeBgGreen:
return kDefaultANSIColorBgGreen;
break;
case SGRCodeBgYellow:
return kDefaultANSIColorBgYellow;
break;
case SGRCodeBgBlue:
return kDefaultANSIColorBgBlue;
break;
case SGRCodeBgMagenta:
return kDefaultANSIColorBgMagenta;
break;
case SGRCodeBgCyan:
return kDefaultANSIColorBgCyan;
break;
case SGRCodeBgWhite:
return kDefaultANSIColorBgWhite;
break;
case SGRCodeBgBrightBlack:
return kDefaultANSIColorBgBrightBlack;
break;
case SGRCodeBgBrightRed:
return kDefaultANSIColorBgBrightRed;
break;
case SGRCodeBgBrightGreen:
return kDefaultANSIColorBgBrightGreen;
break;
case SGRCodeBgBrightYellow:
return kDefaultANSIColorBgBrightYellow;
break;
case SGRCodeBgBrightBlue:
return kDefaultANSIColorBgBrightBlue;
break;
case SGRCodeBgBrightMagenta:
return kDefaultANSIColorBgBrightMagenta;
break;
case SGRCodeBgBrightCyan:
return kDefaultANSIColorBgBrightCyan;
break;
case SGRCodeBgBrightWhite:
return kDefaultANSIColorBgBrightWhite;
break;
default:
break;
}
return kDefaultANSIColorFgBlack;
}
- (enum sgrCode) sgrCodeForColor:(NSColor*)aColor isForegroundColor:(BOOL)aForeground
{
if (self.ansiColors != nil)
{
NSArray *codesForGivenColor = [self.ansiColors allKeysForObject:aColor];
if (codesForGivenColor != nil && [codesForGivenColor count] > 0)
{
NSNumber *thisCode;
for (thisCode in codesForGivenColor)
{
BOOL thisIsForegroundColor = ([thisCode intValue] < 40);
if (aForeground == thisIsForegroundColor)
return [thisCode intValue];
}
}
}
if (aForeground)
{
if ([aColor isEqual:kDefaultANSIColorFgBlack])
return SGRCodeFgBlack;
else if ([aColor isEqual:kDefaultANSIColorFgRed])
return SGRCodeFgRed;
else if ([aColor isEqual:kDefaultANSIColorFgGreen])
return SGRCodeFgGreen;
else if ([aColor isEqual:kDefaultANSIColorFgYellow])
return SGRCodeFgYellow;
else if ([aColor isEqual:kDefaultANSIColorFgBlue])
return SGRCodeFgBlue;
else if ([aColor isEqual:kDefaultANSIColorFgMagenta])
return SGRCodeFgMagenta;
else if ([aColor isEqual:kDefaultANSIColorFgCyan])
return SGRCodeFgCyan;
else if ([aColor isEqual:kDefaultANSIColorFgWhite])
return SGRCodeFgWhite;
else if ([aColor isEqual:kDefaultANSIColorFgBrightBlack])
return SGRCodeFgBrightBlack;
else if ([aColor isEqual:kDefaultANSIColorFgBrightRed])
return SGRCodeFgBrightRed;
else if ([aColor isEqual:kDefaultANSIColorFgBrightGreen])
return SGRCodeFgBrightGreen;
else if ([aColor isEqual:kDefaultANSIColorFgBrightYellow])
return SGRCodeFgBrightYellow;
else if ([aColor isEqual:kDefaultANSIColorFgBrightBlue])
return SGRCodeFgBrightBlue;
else if ([aColor isEqual:kDefaultANSIColorFgBrightMagenta])
return SGRCodeFgBrightMagenta;
else if ([aColor isEqual:kDefaultANSIColorFgBrightCyan])
return SGRCodeFgBrightCyan;
else if ([aColor isEqual:kDefaultANSIColorFgBrightWhite])
return SGRCodeFgBrightWhite;
}
else
{
if ([aColor isEqual:kDefaultANSIColorBgBlack])
return SGRCodeBgBlack;
else if ([aColor isEqual:kDefaultANSIColorBgRed])
return SGRCodeBgRed;
else if ([aColor isEqual:kDefaultANSIColorBgGreen])
return SGRCodeBgGreen;
else if ([aColor isEqual:kDefaultANSIColorBgYellow])
return SGRCodeBgYellow;
else if ([aColor isEqual:kDefaultANSIColorBgBlue])
return SGRCodeBgBlue;
else if ([aColor isEqual:kDefaultANSIColorBgMagenta])
return SGRCodeBgMagenta;
else if ([aColor isEqual:kDefaultANSIColorBgCyan])
return SGRCodeBgCyan;
else if ([aColor isEqual:kDefaultANSIColorBgWhite])
return SGRCodeBgWhite;
else if ([aColor isEqual:kDefaultANSIColorBgBrightBlack])
return SGRCodeBgBrightBlack;
else if ([aColor isEqual:kDefaultANSIColorBgBrightRed])
return SGRCodeBgBrightRed;
else if ([aColor isEqual:kDefaultANSIColorBgBrightGreen])
return SGRCodeBgBrightGreen;
else if ([aColor isEqual:kDefaultANSIColorBgBrightYellow])
return SGRCodeBgBrightYellow;
else if ([aColor isEqual:kDefaultANSIColorBgBrightBlue])
return SGRCodeBgBrightBlue;
else if ([aColor isEqual:kDefaultANSIColorBgBrightMagenta])
return SGRCodeBgBrightMagenta;
else if ([aColor isEqual:kDefaultANSIColorBgBrightCyan])
return SGRCodeBgBrightCyan;
else if ([aColor isEqual:kDefaultANSIColorBgBrightWhite])
return SGRCodeBgBrightWhite;
}
return SGRCodeNoneOrInvalid;
}
// helper struct typedef and a few functions for
// -closestSGRCodeForColor:isForegroundColor:
typedef struct _HSB {
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
} HSB;
HSB makeHSB(CGFloat hue, CGFloat saturation, CGFloat brightness)
{
HSB outHSB;
outHSB.hue = hue;
outHSB.saturation = saturation;
outHSB.brightness = brightness;
return outHSB;
}
HSB getHSBFromColor(NSColor *color)
{
CGFloat hue = 0.0;
CGFloat saturation = 0.0;
CGFloat brightness = 0.0;
[[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]
getHue:&hue
saturation:&saturation
brightness:&brightness
alpha:NULL
];
return makeHSB(hue, saturation, brightness);
}
BOOL floatsEqual(CGFloat first, CGFloat second, CGFloat maxAbsError)
{
return (fabs(first-second)) < maxAbsError;
}
#define MAX_HUE_FLOAT_EQUALITY_ABS_ERROR 0.000001
- (enum sgrCode) closestSGRCodeForColor:(NSColor *)color isForegroundColor:(BOOL)foreground
{
if (color == nil)
return SGRCodeNoneOrInvalid;
enum sgrCode closestColorSGRCode = [self sgrCodeForColor:color isForegroundColor:foreground];
if (closestColorSGRCode != SGRCodeNoneOrInvalid)
return closestColorSGRCode;
HSB givenColorHSB = getHSBFromColor(color);
CGFloat closestColorHueDiff = FLT_MAX;
CGFloat closestColorSaturationDiff = FLT_MAX;
CGFloat closestColorBrightnessDiff = FLT_MAX;
// (background SGR codes are +10 from foreground ones:)
NSUInteger sgrCodeShift = (foreground)?0:10;
NSArray *ansiFgColorCodes = [NSArray
arrayWithObjects:
[NSNumber numberWithInt:SGRCodeFgBlack+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgRed+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgGreen+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgYellow+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBlue+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgMagenta+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgCyan+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgWhite+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBrightBlack+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBrightRed+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBrightGreen+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBrightYellow+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBrightBlue+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBrightMagenta+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBrightCyan+sgrCodeShift],
[NSNumber numberWithInt:SGRCodeFgBrightWhite+sgrCodeShift],
nil
];
for (NSNumber *thisSGRCodeNumber in ansiFgColorCodes)
{
enum sgrCode thisSGRCode = [thisSGRCodeNumber intValue];
NSColor *thisColor = [self colorForSGRCode:thisSGRCode];
HSB thisColorHSB = getHSBFromColor(thisColor);
CGFloat hueDiff = fabs(givenColorHSB.hue - thisColorHSB.hue);
CGFloat saturationDiff = fabs(givenColorHSB.saturation - thisColorHSB.saturation);
CGFloat brightnessDiff = fabs(givenColorHSB.brightness - thisColorHSB.brightness);
// comparison depends on hue, saturation and brightness
// (strictly in that order):
if (!floatsEqual(hueDiff, closestColorHueDiff, MAX_HUE_FLOAT_EQUALITY_ABS_ERROR))
{
if (hueDiff > closestColorHueDiff)
continue;
closestColorSGRCode = thisSGRCode;
closestColorHueDiff = hueDiff;
closestColorSaturationDiff = saturationDiff;
closestColorBrightnessDiff = brightnessDiff;
continue;
}
if (!floatsEqual(saturationDiff, closestColorSaturationDiff, MAX_HUE_FLOAT_EQUALITY_ABS_ERROR))
{
if (saturationDiff > closestColorSaturationDiff)
continue;
closestColorSGRCode = thisSGRCode;
closestColorHueDiff = hueDiff;
closestColorSaturationDiff = saturationDiff;
closestColorBrightnessDiff = brightnessDiff;
continue;
}
if (!floatsEqual(brightnessDiff, closestColorBrightnessDiff, MAX_HUE_FLOAT_EQUALITY_ABS_ERROR))
{
if (brightnessDiff > closestColorBrightnessDiff)
continue;
closestColorSGRCode = thisSGRCode;
closestColorHueDiff = hueDiff;
closestColorSaturationDiff = saturationDiff;
closestColorBrightnessDiff = brightnessDiff;
continue;
}
// If hue (especially hue!), saturation and brightness diffs all
// are equal to some other color, we need to prefer one or the
// other so we'll select the more 'distinctive' color of the
// two (this is *very* subjective, obviously). I basically just
// looked at the hue chart, went through all the points between
// our main ANSI colors and decided which side the middle point
// would lean on. (e.g. the purple color that is exactly between
// the blue and magenta ANSI colors looks more magenta than
// blue to me so I put magenta higher than blue in the list
// below.)
//
// subjective ordering of colors from most to least 'distinctive':
int colorDistinctivenessOrder[6] = {
SGRCodeFgRed+sgrCodeShift,
SGRCodeFgMagenta+sgrCodeShift,
SGRCodeFgBlue+sgrCodeShift,
SGRCodeFgGreen+sgrCodeShift,
SGRCodeFgCyan+sgrCodeShift,
SGRCodeFgYellow+sgrCodeShift
};
int i;
for (i = 0; i < 6; i++)
{
if (colorDistinctivenessOrder[i] == closestColorSGRCode)
break;
else if (colorDistinctivenessOrder[i] == thisSGRCode)
{
closestColorSGRCode = thisSGRCode;
closestColorHueDiff = hueDiff;
closestColorSaturationDiff = saturationDiff;
closestColorBrightnessDiff = brightnessDiff;
}
}
}
return closestColorSGRCode;
}
@end