-
Notifications
You must be signed in to change notification settings - Fork 1
/
Configuration.cs
1223 lines (1143 loc) · 43.7 KB
/
Configuration.cs
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Threading;
namespace StackCleaner;
/// <summary>
/// Used to configure properties of the StackCleaner including color and behavior flags.
/// </summary>
public class StackCleanerConfiguration : ICloneable
{
private const string FrozenErrorText = "Configuration is frozen.";
private static StackCleanerConfiguration? _default;
internal bool Frozen;
private IFormatProvider _locale = CultureInfo.InvariantCulture;
private ColorConfig _colors = Color4Config.Default;
private bool _includeSourceData = true;
private bool _includeAssemblyData;
private bool _putSourceDataOnNewLine = true;
private bool _includeNamespaces = true;
private bool _includeLineData = true;
private bool _useTypeAliases = true;
private bool _htmlWriteOuterDiv = true;
private bool _warnForHiddenLines;
private bool _htmlUseClassNames;
private bool _includeILOffset;
private bool _includeFileData;
private StackColorFormatType _colorFormatting = StackColorFormatType.None;
private IReadOnlyCollection<Type> _hiddenTypes = StackTraceCleaner.DefaultHiddenTypes;
/// <summary>
/// Default implementation of <see cref="StackCleanerConfiguration"/>.
/// </summary>
public static StackCleanerConfiguration Default => _default ??= new StackCleanerConfiguration { Frozen = true };
/// <summary>
/// Instance of <see cref="Color4Config"/>, <see cref="Color32Config"/>, or override <see cref="ColorConfig"/> and make your own color provider.<br/>
/// Default value is <see cref="Color4Config.Default"/>.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public ColorConfig? Colors
{
get => _colors;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_colors = value ?? Color4Config.Default;
if (_colors is Color4Config && _colorFormatting == StackColorFormatType.ExtendedANSIColor)
_colorFormatting = StackColorFormatType.ANSIColor;
}
}
/// <summary>
/// Used to convert line numbers, column numbers, and IL offsets to strings.<br/>
/// Default value is <see cref="CultureInfo.InvariantCulture"/>.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public IFormatProvider Locale
{
get => _locale;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_locale = value ?? CultureInfo.InvariantCulture;
}
}
/// <summary>
/// Source data includes line number, column number, IL offset, and file name when available.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool IncludeSourceData
{
get => _includeSourceData;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_includeSourceData = value;
}
}
/// <summary>
/// Appends a warning to the end of the stack trace that lines were removed for visibility.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool WarnForHiddenLines
{
get => _warnForHiddenLines;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_warnForHiddenLines = value;
}
}
/// <summary>
/// Source data is put on the line after the stack frame declaration.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool PutSourceDataOnNewLine
{
get => _putSourceDataOnNewLine;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_putSourceDataOnNewLine = value;
}
}
/// <summary>
/// Namespaces are included in the method declaration.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool IncludeNamespaces
{
get => _includeNamespaces;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_includeNamespaces = value;
}
}
/// <summary>
/// IL offsets are included in the source data.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool IncludeILOffset
{
get => _includeILOffset;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_includeILOffset = value;
}
}
/// <summary>
/// Line and column numbers are included in the source data.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool IncludeLineData
{
get => _includeLineData;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_includeLineData = value;
}
}
/// <summary>
/// Relative source file path will be included in the source data.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool IncludeFileData
{
get => _includeFileData;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_includeFileData = value;
}
}
/// <summary>
/// Assembly-qualified name (and path if <see cref="IncludeFileData"/> is <see langword="true"/>) will be included in the source data.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool IncludeAssemblyData
{
get => _includeAssemblyData;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_includeAssemblyData = value;
}
}
/// <summary>
/// Primitive types will use their aliases.
/// </summary>
/// <remarks>ex. <see cref="int"/> instead of <see cref="Int32">Int32</see>.</remarks>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool UseTypeAliases
{
get => _useTypeAliases;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_useTypeAliases = value;
}
}
/// <summary>
/// If <see cref="ColorFormatting"/> is set to <see cref="StackColorFormatType.Html"/>,
/// use css class names (defined as public constants in <see cref="StackTraceCleaner"/>) instead of style tags.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool HtmlUseClassNames
{
get => _htmlUseClassNames;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_htmlUseClassNames = value;
}
}
/// <summary>
/// If <see cref="ColorFormatting"/> is set to <see cref="StackColorFormatType.Html"/>,
/// write an outer <div> with a background color around the output HTML.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public bool HtmlWriteOuterDiv
{
get => _htmlWriteOuterDiv;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_htmlWriteOuterDiv = value;
}
}
/// <summary>
/// Describes how colors will be handled by the formatter.
/// </summary>
/// <remarks>Default value is <see cref="StackColorFormatType.ConsoleColor"/>.</remarks>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
/// <exception cref="ArgumentOutOfRangeException">Value is out of range of <see cref="StackColorFormatType"/>.</exception>
public StackColorFormatType ColorFormatting
{
get => _colorFormatting;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
if (value is < 0 or > StackColorFormatType.ANSIColorNoBright)
throw new ArgumentOutOfRangeException(nameof(value));
_colorFormatting = value;
}
}
/// <summary>
/// Override types who's methods will be skipped in stack traces.<br/>
/// Default values:<br/><br/>
/// <see cref="ExecutionContext"/><br/>
/// <see cref="TaskAwaiter"/><br/>
/// <see cref="TaskAwaiter{TResult}"/><br/>
/// <see cref="ConfiguredTaskAwaitable.ConfiguredTaskAwaiter"/><br/>
/// <see cref="ConfiguredTaskAwaitable{TResult}.ConfiguredTaskAwaiter"/><br/>
/// <see cref="ExceptionDispatchInfo"/><br/>
/// Use <see cref="GetHiddenTypes"/> to <see langword="get"/> the value.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
/// <remarks>Setting a <see langword="null"/> value will be converted to an empty array.</remarks>
public ICollection<Type>? HiddenTypes
{
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
if (value == null)
value = Array.AsReadOnly(Array.Empty<Type>());
else if (!ReferenceEquals(value, StackTraceCleaner.DefaultHiddenTypes))
{
if (value is Type[] arr)
{
Type[] arr2 = new Type[arr.Length];
Array.Copy(arr, arr2, arr.Length);
value = Array.AsReadOnly(arr2);
}
else
value = Array.AsReadOnly(value.ToArray());
}
_hiddenTypes = (IReadOnlyCollection<Type>)value;
}
}
/// <summary>As close as possible to Visual Studio default formatting.</summary>
public StackCleanerConfiguration() { }
/// <summary>
/// Creates a non-frozen clone of this <see cref="StackCleanerConfiguration"/>.
/// </summary>
/// <returns>An exact copy of this <see cref="StackCleanerConfiguration"/>, safe to cast. Never is frozen.</returns>
public object Clone() => new StackCleanerConfiguration
{
_colorFormatting = _colorFormatting,
_colors = _colors,
_hiddenTypes = _hiddenTypes,
_includeSourceData = _includeSourceData,
_putSourceDataOnNewLine = _putSourceDataOnNewLine,
_useTypeAliases = _useTypeAliases,
_includeNamespaces = _includeNamespaces,
_includeAssemblyData = _includeAssemblyData
};
/// <returns>A readonly array representing the current hidden types. May equal <see cref="StackTraceCleaner.DefaultHiddenTypes"/> </returns>
public IReadOnlyCollection<Type> GetHiddenTypes() => _hiddenTypes;
}
/// <summary>
/// Describes the color formatting behavior of <see cref="StackTraceCleaner"/>
/// </summary>
public enum StackColorFormatType
{
/// <summary>
/// No color formatting, just raw text.
/// </summary>
None,
/// <summary>
/// Sets the <see cref="Console.ForegroundColor"/> for each section. Only applicable when printed to console.
/// </summary>
ConsoleColor,
/// <summary>
/// UnityEngine rich text tags.<br/>
/// <seealso href="https://docs.unity3d.com/Packages/[email protected]/manual/StyledText.html"/>
/// </summary>
UnityRichText,
/// <summary>
/// TextMeshPro rich text tags.<br/>
/// <seealso href="http://digitalnativestudios.com/textmeshpro/docs/rich-text/"/>
/// </summary>
TextMeshProRichText,
/// <summary>
/// ANSI Text formatting codes.<br/>
/// <seealso href="https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences?redirectedfrom=MSDN#text-formatting"/>
/// </summary>
ANSIColor,
/// <summary>
/// Will not work on all terminals.<br/>
/// <seealso href="https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences?redirectedfrom=MSDN#extended-colors"/>
/// </summary>
ExtendedANSIColor,
/// <summary>
/// Text is colored with <span> tags.
/// </summary>
/// <remarks>Use classes instead of constant CSS styles by setting <see cref="StackCleanerConfiguration.HtmlUseClassNames"/> to <see langword="true"/>.</remarks>
Html,
/// <summary>
/// ANSI Text formatting codes without bright colors (3-bit).<br/>
/// <seealso href="https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences?redirectedfrom=MSDN#text-formatting"/>
/// </summary>
ANSIColorNoBright
}
/// <summary>
/// Base class for all color configurations.<br/>
/// To use your own source of colors, override this class and set the values of the color fields to a 32 bit ARGB integer.
/// <code>
/// Alpha = (argb >> 24) & 0xFF
/// Red = (argb >> 16) & 0xFF
/// Green = (argb >> 8 ) & 0xFF
/// Blue = (argb >> 0 ) & 0xFF
/// </code>
/// </summary>
public abstract class ColorConfig
{
/// <summary>Error text used to throw a <see cref="NotSupportedException"/> when the config is frozen.</summary>
protected const string FrozenErrorText = "Color configuration is frozen.";
private int _keywordColor;
private int _methodColor;
private int _propertyColor;
private int _parameterColor;
private int _classColor;
private int _structColor;
private int _flowKeywordColor;
private int _interfaceColor;
private int _genericParameterColor;
private int _enumColor;
private int _namespaceColor;
private int _punctuationColor;
private int _extraDataColor;
private int _linesHiddenWarningColor;
private int _htmlBackgroundColor;
/// <summary>
/// This property is set to <see langword="true"/> after the config is passed to a <see cref="StackTraceCleaner"/> so it can not be modified.
/// </summary>
public bool Frozen { get; internal set; }
/// <summary>
/// Color of keywords including types: <br/><see langword="null"/>, <see langword="bool"/>,
/// <see langword="byte"/>, <see langword="char"/>, <see langword="double"/>,
/// <see langword="decimal"/>, <see langword="float"/>, <see langword="int"/>, <see langword="long"/>,
/// <see langword="sbyte"/>, <see langword="short"/>, <see langword="object"/>, <see langword="string"/>,
/// <see langword="uint"/>, <see langword="bool"/>, <see langword="ulong"/>, <see langword="ushort"/>,
/// <see langword="void"/><br/>
/// method keywords: <br/><see langword="static"/>, <see langword="async"/>, <see langword="enumerator"/>,
/// <see langword="get"/>, <see langword="set"/>, <see langword="anonymous"/><br/>
/// and the <see langword="global"/> and <see langword="params"/> keywords.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int KeywordColor
{
get => _keywordColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_keywordColor = value;
}
}
/// <summary>
/// Color of method names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int MethodColor
{
get => _methodColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_methodColor = value;
}
}
/// <summary>
/// Color of property names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int PropertyColor
{
get => _propertyColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_propertyColor = value;
}
}
/// <summary>
/// Color of parameter names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int ParameterColor
{
get => _parameterColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_parameterColor = value;
}
}
/// <summary>
/// Color of class type names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int ClassColor
{
get => _classColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_classColor = value;
}
}
/// <summary>
/// Color of value type names (not including enums).
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int StructColor
{
get => _structColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_structColor = value;
}
}
/// <summary>
/// Color of flow keywords, currently only used for the 'at' at the beginning of each declaration.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int FlowKeywordColor
{
get => _flowKeywordColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_flowKeywordColor = value;
}
}
/// <summary>
/// Color of interface type names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int InterfaceColor
{
get => _interfaceColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_interfaceColor = value;
}
}
/// <summary>
/// Color of generic parameter names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int GenericParameterColor
{
get => _genericParameterColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_genericParameterColor = value;
}
}
/// <summary>
/// Color of enum type names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int EnumColor
{
get => _enumColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_enumColor = value;
}
}
/// <summary>
/// Color of namespaces.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int NamespaceColor
{
get => _namespaceColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_namespaceColor = value;
}
}
/// <summary>
/// Color of any punctuation: periods, commas, parenthesis, etc.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int PunctuationColor
{
get => _punctuationColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_punctuationColor = value;
}
}
/// <summary>
/// Color of the source data (line number, column number, IL offset, and file name).
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int ExtraDataColor
{
get => _extraDataColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_extraDataColor = value;
}
}
/// <summary>
/// Color of the warning optionally shown when unnecessary lines are removed.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int LinesHiddenWarningColor
{
get => _linesHiddenWarningColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_linesHiddenWarningColor = value;
}
}
/// <summary>
/// Color of the optionally added background when writing as HTML.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public virtual int HtmlBackgroundColor
{
get => _htmlBackgroundColor;
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
_htmlBackgroundColor = value;
}
}
internal static int ToArgb(ConsoleColor color)
{
// Based off Windows 10 Console colors from https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
return color switch
{
ConsoleColor.Black => unchecked((int)0xff0c0c0c),
ConsoleColor.DarkRed => unchecked((int)0xffc50f1f),
ConsoleColor.DarkGreen => unchecked((int)0xff13a10e),
ConsoleColor.DarkYellow => unchecked((int)0xffc19c00),
ConsoleColor.DarkBlue => unchecked((int)0xff0037da),
ConsoleColor.DarkMagenta => unchecked((int)0xff881798),
ConsoleColor.DarkCyan => unchecked((int)0xff3a96dd),
ConsoleColor.DarkGray => unchecked((int)0xff767676),
ConsoleColor.Red => unchecked((int)0xffe74856),
ConsoleColor.Green => unchecked((int)0xff16c60c),
ConsoleColor.Yellow => unchecked((int)0xfff9f1a5),
ConsoleColor.Blue => unchecked((int)0xff3b78ff),
ConsoleColor.Magenta => unchecked((int)0xffb4009e),
ConsoleColor.Cyan => unchecked((int)0xff61d6d6),
ConsoleColor.White => unchecked((int)0xfff2f2f2),
_ => unchecked((int)0xffcccccc) // ConsoleColor.Gray
};
}
}
/// <summary>
/// Provides colors in the <see cref="ConsoleColor"/> format.
/// </summary>
public sealed class Color4Config : ColorConfig
{
private static Color4Config? _default;
/// <summary>
/// Default values of <see cref="Color4Config"/>. Frozen.
/// </summary>
public static Color4Config Default => _default ??= new Color4Config { Frozen = true };
/// <summary>
/// Color of keywords including types: <br/><see langword="null"/>, <see langword="bool"/>,
/// <see langword="byte"/>, <see langword="char"/>, <see langword="double"/>,
/// <see langword="decimal"/>, <see langword="float"/>, <see langword="int"/>, <see langword="long"/>,
/// <see langword="sbyte"/>, <see langword="short"/>, <see langword="object"/>, <see langword="string"/>,
/// <see langword="uint"/>, <see langword="bool"/>, <see langword="ulong"/>, <see langword="ushort"/>,
/// <see langword="void"/><br/>
/// method keywords: <br/><see langword="static"/>, <see langword="async"/>, <see langword="enumerator"/>,
/// <see langword="get"/>, <see langword="set"/>, <see langword="anonymous"/><br/>
/// and the <see langword="global"/> and <see langword="params"/> keywords.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor KeywordColor
{
get => (ConsoleColor)(base.KeywordColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.KeywordColor = (int)value + 1;
}
}
/// <summary>
/// Color of method names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor MethodColor
{
get => (ConsoleColor)(base.MethodColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.MethodColor = (int)value + 1;
}
}
/// <summary>
/// Color of property names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor PropertyColor
{
get => (ConsoleColor)(base.PropertyColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.PropertyColor = (int)value + 1;
}
}
/// <summary>
/// Color of parameter names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor ParameterColor
{
get => (ConsoleColor)(base.ParameterColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.ParameterColor = (int)value + 1;
}
}
/// <summary>
/// Color of class type names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor ClassColor
{
get => (ConsoleColor)(base.ClassColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.ClassColor = (int)value + 1;
}
}
/// <summary>
/// Color of value type names (not including enums).
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor StructColor
{
get => (ConsoleColor)(base.StructColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.StructColor = (int)value + 1;
}
}
/// <summary>
/// Color of flow keywords, currently only used for the 'at' at the beginning of each declaration.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor FlowKeywordColor
{
get => (ConsoleColor)(base.FlowKeywordColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.FlowKeywordColor = (int)value + 1;
}
}
/// <summary>
/// Color of interface type names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor InterfaceColor
{
get => (ConsoleColor)(base.InterfaceColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.InterfaceColor = (int)value + 1;
}
}
/// <summary>
/// Color of generic parameter names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor GenericParameterColor
{
get => (ConsoleColor)(base.GenericParameterColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.GenericParameterColor = (int)value + 1;
}
}
/// <summary>
/// Color of enum type names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor EnumColor
{
get => (ConsoleColor)(base.EnumColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.EnumColor = (int)value + 1;
}
}
/// <summary>
/// Color of namespaces.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor NamespaceColor
{
get => (ConsoleColor)(base.NamespaceColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.NamespaceColor = (int)value + 1;
}
}
/// <summary>
/// Color of any punctuation: periods, commas, parenthesis, etc.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor PunctuationColor
{
get => (ConsoleColor)(base.PunctuationColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.PunctuationColor = (int)value + 1;
}
}
/// <summary>
/// Color of the source data (line number, column number, IL offset, and file name).
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor ExtraDataColor
{
get => (ConsoleColor)(base.ExtraDataColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.ExtraDataColor = (int)value + 1;
}
}
/// <summary>
/// Color of the warning optionally shown when unnecessary lines are removed.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor LinesHiddenWarningColor
{
get => (ConsoleColor)(base.LinesHiddenWarningColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.LinesHiddenWarningColor = (int)value + 1;
}
}
/// <summary>
/// Color of the optionally added background when writing as HTML.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new ConsoleColor HtmlBackgroundColor
{
get => (ConsoleColor)(base.HtmlBackgroundColor - 1);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.HtmlBackgroundColor = (int)value + 1;
}
}
/// <summary>
/// Sets the default values for <see cref="Color4Config"/>.
/// </summary>
public Color4Config()
{
KeywordColor = ConsoleColor.Blue;
MethodColor = ConsoleColor.DarkYellow;
PropertyColor = ConsoleColor.White;
ParameterColor = ConsoleColor.Cyan;
ClassColor = ConsoleColor.DarkGreen;
StructColor = ConsoleColor.Green;
FlowKeywordColor = ConsoleColor.Magenta;
InterfaceColor = ConsoleColor.Yellow;
GenericParameterColor = ConsoleColor.Yellow;
EnumColor = ConsoleColor.Yellow;
NamespaceColor = ConsoleColor.Gray;
PunctuationColor = ConsoleColor.DarkGray;
ExtraDataColor = ConsoleColor.DarkGray;
LinesHiddenWarningColor = ConsoleColor.Yellow;
HtmlBackgroundColor = ConsoleColor.Black;
}
/// <summary>
/// Convert ARGB data to <see cref="ConsoleColor"/> (picks the closest).
/// </summary>
internal static ConsoleColor ToConsoleColor(int argb, bool allowBright)
{
int bits = allowBright && (((argb >> 16) & byte.MaxValue) > 128 || ((argb >> 8) & byte.MaxValue) > 128 || (argb & byte.MaxValue) > 128) ? 8 : 0;
if (((argb >> 16) & byte.MaxValue) > 180)
bits |= 4;
if (((argb >> 8) & byte.MaxValue) > 180)
bits |= 2;
if ((argb & byte.MaxValue) > 180)
bits |= 1;
return bits == (int)ConsoleColor.Black ? ConsoleColor.DarkGray : (ConsoleColor)bits;
}
}
/// <summary>
/// Provides colors in the <see cref="System.Drawing.Color"/> format.
/// </summary>
public sealed class Color32Config : ColorConfig
{
private static Color32Config? _default;
/// <summary>
/// Default values of <see cref="Color32Config"/>. Frozen.
/// </summary>
public static Color32Config Default => _default ??= new Color32Config { Frozen = true };
/// <summary>
/// Color of keywords including types: <br/><see langword="null"/>, <see langword="bool"/>,
/// <see langword="byte"/>, <see langword="char"/>, <see langword="double"/>,
/// <see langword="decimal"/>, <see langword="float"/>, <see langword="int"/>, <see langword="long"/>,
/// <see langword="sbyte"/>, <see langword="short"/>, <see langword="object"/>, <see langword="string"/>,
/// <see langword="uint"/>, <see langword="bool"/>, <see langword="ulong"/>, <see langword="ushort"/>,
/// <see langword="void"/><br/>
/// method keywords: <br/><see langword="static"/>, <see langword="async"/>, <see langword="enumerator"/>,
/// <see langword="get"/>, <see langword="set"/>, <see langword="anonymous"/><br/>
/// and the <see langword="global"/> and <see langword="params"/> keywords.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new Color KeywordColor
{
get => Color.FromArgb(base.KeywordColor);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.KeywordColor = value.ToArgb();
}
}
/// <summary>
/// Color of method names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new Color MethodColor
{
get => Color.FromArgb(base.MethodColor);
set
{
if (Frozen)
throw new NotSupportedException(FrozenErrorText);
base.MethodColor = value.ToArgb();
}
}
/// <summary>
/// Color of property names.
/// </summary>
/// <exception cref="NotSupportedException">Object is frozen (has been given to a <see cref="StackTraceCleaner"/>).</exception>
public new Color PropertyColor
{
get => Color.FromArgb(base.PropertyColor);
set
{