Zachary Turner | f3b4b2d | 2017-07-07 18:45:37 +0000 | [diff] [blame^] | 1 | |
| 2 | #include "DiffPrinter.h" |
| 3 | |
| 4 | #include "llvm/Support/FormatAdapters.h" |
| 5 | |
| 6 | using namespace llvm; |
| 7 | using namespace llvm::pdb; |
| 8 | |
| 9 | static void setColor(llvm::raw_ostream &OS, DiffResult Result) { |
| 10 | switch (Result) { |
| 11 | case DiffResult::IDENTICAL: |
| 12 | OS.changeColor(raw_ostream::Colors::GREEN, false); |
| 13 | break; |
| 14 | case DiffResult::EQUIVALENT: |
| 15 | OS.changeColor(raw_ostream::Colors::YELLOW, true); |
| 16 | break; |
| 17 | default: |
| 18 | OS.changeColor(raw_ostream::Colors::RED, false); |
| 19 | break; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | DiffPrinter::DiffPrinter(uint32_t Indent, StringRef Header, |
| 24 | uint32_t PropertyWidth, uint32_t FieldWidth, |
| 25 | raw_ostream &Stream) |
| 26 | : Indent(Indent), PropertyWidth(PropertyWidth), FieldWidth(FieldWidth), |
| 27 | OS(Stream) { |
| 28 | printHeaderRow(); |
| 29 | printFullRow(Header); |
| 30 | } |
| 31 | |
| 32 | DiffPrinter::~DiffPrinter() {} |
| 33 | |
| 34 | void DiffPrinter::printFullRow(StringRef Text) { |
| 35 | newLine(); |
| 36 | printField(Text, DiffResult::UNSPECIFIED, AlignStyle::Center, |
| 37 | PropertyWidth + 1 + FieldWidth + 1 + FieldWidth); |
| 38 | printSeparatorRow(); |
| 39 | } |
| 40 | |
| 41 | void DiffPrinter::printSeparatorRow() { |
| 42 | newLine(); |
| 43 | OS << formatv("{0}", fmt_repeat('-', PropertyWidth)); |
| 44 | OS << '+'; |
| 45 | OS << formatv("{0}", fmt_repeat('-', FieldWidth)); |
| 46 | OS << '+'; |
| 47 | OS << formatv("{0}", fmt_repeat('-', FieldWidth)); |
| 48 | OS << '|'; |
| 49 | } |
| 50 | |
| 51 | void DiffPrinter::printHeaderRow() { |
| 52 | newLine('-'); |
| 53 | OS << formatv("{0}", fmt_repeat('-', PropertyWidth + 2 * FieldWidth + 3)); |
| 54 | } |
| 55 | |
| 56 | void DiffPrinter::newLine(char InitialChar) { |
| 57 | OS << "\n"; |
| 58 | OS.indent(Indent) << InitialChar; |
| 59 | } |
| 60 | |
| 61 | void DiffPrinter::printExplicit(StringRef Property, DiffResult C, |
| 62 | StringRef Left, StringRef Right) { |
| 63 | newLine(); |
| 64 | printField(Property, DiffResult::UNSPECIFIED, AlignStyle::Right, |
| 65 | PropertyWidth); |
| 66 | printField(Left, C, AlignStyle::Center, FieldWidth); |
| 67 | printField(Right, C, AlignStyle::Center, FieldWidth); |
| 68 | printSeparatorRow(); |
| 69 | } |
| 70 | |
| 71 | void DiffPrinter::printSame(StringRef Property, StringRef Value) { |
| 72 | newLine(); |
| 73 | printField(Property, DiffResult::UNSPECIFIED, AlignStyle::Right, |
| 74 | PropertyWidth); |
| 75 | printField(Value, DiffResult::IDENTICAL, AlignStyle::Center, |
| 76 | FieldWidth + 1 + FieldWidth); |
| 77 | printSeparatorRow(); |
| 78 | } |
| 79 | |
| 80 | void DiffPrinter::printDifferent(StringRef Property, StringRef Left, |
| 81 | StringRef Right) { |
| 82 | newLine(); |
| 83 | printField(Property, DiffResult::UNSPECIFIED, AlignStyle::Right, |
| 84 | PropertyWidth); |
| 85 | printField(Left, DiffResult::DIFFERENT, AlignStyle::Center, FieldWidth); |
| 86 | printField(Right, DiffResult::DIFFERENT, AlignStyle::Center, FieldWidth); |
| 87 | printSeparatorRow(); |
| 88 | } |
| 89 | |
| 90 | void DiffPrinter::printField(StringRef Value, DiffResult C, AlignStyle Style, |
| 91 | uint32_t Width) { |
| 92 | if (Style == AlignStyle::Right) |
| 93 | --Width; |
| 94 | |
| 95 | std::string FormattedItem = |
| 96 | formatv("{0}", fmt_align(Value, Style, Width)).str(); |
| 97 | if (C != DiffResult::UNSPECIFIED) { |
| 98 | setColor(OS, C); |
| 99 | OS << FormattedItem; |
| 100 | OS.resetColor(); |
| 101 | } else |
| 102 | OS << FormattedItem; |
| 103 | if (Style == AlignStyle::Right) |
| 104 | OS << ' '; |
| 105 | OS << '|'; |
| 106 | } |