121
|
1
|
|
2 #include "DiffPrinter.h"
|
|
3
|
|
4 #include "llvm/Support/FormatAdapters.h"
|
|
5
|
|
6 using namespace llvm;
|
|
7 using namespace llvm::pdb;
|
|
8
|
|
9 namespace {
|
|
10 struct Colorize {
|
|
11 Colorize(raw_ostream &OS, DiffResult Result) : OS(OS) {
|
|
12 if (!OS.has_colors())
|
|
13 return;
|
|
14 switch (Result) {
|
|
15 case DiffResult::IDENTICAL:
|
|
16 OS.changeColor(raw_ostream::Colors::GREEN, false);
|
|
17 break;
|
|
18 case DiffResult::EQUIVALENT:
|
|
19 OS.changeColor(raw_ostream::Colors::YELLOW, true);
|
|
20 break;
|
|
21 default:
|
|
22 OS.changeColor(raw_ostream::Colors::RED, false);
|
|
23 break;
|
|
24 }
|
|
25 }
|
|
26
|
|
27 ~Colorize() {
|
|
28 if (OS.has_colors())
|
|
29 OS.resetColor();
|
|
30 }
|
|
31
|
|
32 raw_ostream &OS;
|
|
33 };
|
|
34 }
|
|
35
|
|
36 DiffPrinter::DiffPrinter(uint32_t Indent, StringRef Header,
|
|
37 uint32_t PropertyWidth, uint32_t FieldWidth,
|
|
38 bool Result, bool Fields, raw_ostream &Stream)
|
|
39 : PrintResult(Result), PrintValues(Fields), Indent(Indent),
|
|
40 PropertyWidth(PropertyWidth), FieldWidth(FieldWidth), OS(Stream) {
|
|
41 printHeaderRow();
|
|
42 printFullRow(Header);
|
|
43 }
|
|
44
|
|
45 DiffPrinter::~DiffPrinter() {}
|
|
46
|
|
47 uint32_t DiffPrinter::tableWidth() const {
|
|
48 // `|`
|
|
49 uint32_t W = 1;
|
|
50
|
|
51 // `<width>|`
|
|
52 W += PropertyWidth + 1;
|
|
53
|
|
54 if (PrintResult) {
|
|
55 // ` I |`
|
|
56 W += 4;
|
|
57 }
|
|
58
|
|
59 if (PrintValues) {
|
|
60 // `<width>|<width>|`
|
|
61 W += 2 * (FieldWidth + 1);
|
|
62 }
|
|
63 return W;
|
|
64 }
|
|
65
|
|
66 void DiffPrinter::printFullRow(StringRef Text) {
|
|
67 newLine();
|
|
68 printValue(Text, DiffResult::UNSPECIFIED, AlignStyle::Center,
|
|
69 tableWidth() - 2, true);
|
|
70 printSeparatorRow();
|
|
71 }
|
|
72
|
|
73 void DiffPrinter::printSeparatorRow() {
|
|
74 newLine();
|
|
75 OS << formatv("{0}", fmt_repeat('-', PropertyWidth));
|
|
76 if (PrintResult) {
|
|
77 OS << '+';
|
|
78 OS << formatv("{0}", fmt_repeat('-', 3));
|
|
79 }
|
|
80 if (PrintValues) {
|
|
81 OS << '+';
|
|
82 OS << formatv("{0}", fmt_repeat('-', FieldWidth));
|
|
83 OS << '+';
|
|
84 OS << formatv("{0}", fmt_repeat('-', FieldWidth));
|
|
85 }
|
|
86 OS << '|';
|
|
87 }
|
|
88
|
|
89 void DiffPrinter::printHeaderRow() {
|
|
90 newLine('-');
|
|
91 OS << formatv("{0}", fmt_repeat('-', tableWidth() - 1));
|
|
92 }
|
|
93
|
|
94 void DiffPrinter::newLine(char InitialChar) {
|
|
95 OS << "\n";
|
|
96 OS.indent(Indent) << InitialChar;
|
|
97 }
|
|
98
|
|
99 void DiffPrinter::printExplicit(StringRef Property, DiffResult C,
|
|
100 StringRef Left, StringRef Right) {
|
|
101 newLine();
|
|
102 printValue(Property, DiffResult::UNSPECIFIED, AlignStyle::Right,
|
|
103 PropertyWidth, true);
|
|
104 printResult(C);
|
|
105 printValue(Left, C, AlignStyle::Center, FieldWidth, false);
|
|
106 printValue(Right, C, AlignStyle::Center, FieldWidth, false);
|
|
107 printSeparatorRow();
|
|
108 }
|
|
109
|
|
110 void DiffPrinter::printResult(DiffResult Result) {
|
|
111 if (!PrintResult)
|
|
112 return;
|
|
113 switch (Result) {
|
|
114 case DiffResult::DIFFERENT:
|
|
115 printValue("D", Result, AlignStyle::Center, 3, true);
|
|
116 break;
|
|
117 case DiffResult::EQUIVALENT:
|
|
118 printValue("E", Result, AlignStyle::Center, 3, true);
|
|
119 break;
|
|
120 case DiffResult::IDENTICAL:
|
|
121 printValue("I", Result, AlignStyle::Center, 3, true);
|
|
122 break;
|
|
123 case DiffResult::UNSPECIFIED:
|
|
124 printValue(" ", Result, AlignStyle::Center, 3, true);
|
|
125 break;
|
|
126 }
|
|
127 }
|
|
128
|
|
129 void DiffPrinter::printValue(StringRef Value, DiffResult C, AlignStyle Style,
|
|
130 uint32_t Width, bool Force) {
|
|
131 if (!Force && !PrintValues)
|
|
132 return;
|
|
133
|
|
134 if (Style == AlignStyle::Right)
|
|
135 --Width;
|
|
136
|
|
137 std::string FormattedItem =
|
|
138 formatv("{0}", fmt_align(Value, Style, Width)).str();
|
|
139 if (C != DiffResult::UNSPECIFIED) {
|
|
140 Colorize Color(OS, C);
|
|
141 OS << FormattedItem;
|
|
142 } else
|
|
143 OS << FormattedItem;
|
|
144 if (Style == AlignStyle::Right)
|
|
145 OS << ' ';
|
|
146 OS << '|';
|
|
147 }
|