| // Copyright 2020 The Tint Authors. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #ifndef SRC_TINT_UTILS_DIAGNOSTIC_PRINTER_H_ |
| #define SRC_TINT_UTILS_DIAGNOSTIC_PRINTER_H_ |
| |
| #include <memory> |
| #include <sstream> |
| #include <string> |
| |
| namespace tint::diag { |
| |
| class List; |
| |
| /// Color is an enumerator of colors used by Style. |
| enum class Color { |
| kDefault, |
| kBlack, |
| kRed, |
| kGreen, |
| kYellow, |
| kBlue, |
| kMagenta, |
| kCyan, |
| kWhite, |
| }; |
| |
| /// Style describes how a diagnostic message should be printed. |
| struct Style { |
| /// The foreground text color |
| Color color = Color::kDefault; |
| /// If true the text will be displayed with a strong weight |
| bool bold = false; |
| }; |
| |
| /// Printers are used to print formatted diagnostic messages to a terminal. |
| class Printer { |
| public: |
| /// @returns a diagnostic Printer |
| /// @param out the file to print to. |
| /// @param use_colors if true, the printer will use colors if `out` is a |
| /// terminal and supports them. |
| static std::unique_ptr<Printer> create(FILE* out, bool use_colors); |
| |
| virtual ~Printer(); |
| |
| /// writes the string str to the printer with the given style. |
| /// @param str the string to write to the printer |
| /// @param style the style used to print `str` |
| virtual void write(const std::string& str, const Style& style) = 0; |
| }; |
| |
| /// StringPrinter is an implementation of Printer that writes to a std::string. |
| class StringPrinter : public Printer { |
| public: |
| StringPrinter(); |
| ~StringPrinter() override; |
| |
| /// @returns the printed string. |
| std::string str() const; |
| |
| void write(const std::string& str, const Style&) override; |
| |
| private: |
| std::stringstream stream; |
| }; |
| |
| } // namespace tint::diag |
| |
| #endif // SRC_TINT_UTILS_DIAGNOSTIC_PRINTER_H_ |