Pretty Diagnostics
Create your own pretty diagnostics
Loading...
Searching...
No Matches
renderer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4
5#include "report.hpp"
6
7namespace pretty_diagnostics {
13class TextRenderer final : public IReporterRenderer {
14public:
20 explicit TextRenderer(const Report& report);
21
28 void render(const Severity& severity, std::ostream& stream) override;
29
36 void render(const Report& report, std::ostream& stream) override;
37
44 void render(const FileGroup& file_group, std::ostream& stream) override;
45
52 void render(const LineGroup& line_group, std::ostream& stream) override;
53
67 static size_t render(const Label& label, std::ostream& stream,
68 const std::vector<std::string>& text_lines, size_t text_index,
69 bool active_render, size_t column_start = 0);
70
79 [[nodiscard]] static size_t widest_line_number(const Report::MappedFileGroups& groups, size_t padding);
80
89 [[nodiscard]] static std::vector<std::string> wrap_text(const std::string& text, size_t max_width);
90
91private:
92 size_t _padding, _snippet_width;
93 std::string _whitespaces;
94};
95} // namespace pretty_diagnostics
96
97// BSD 3-Clause License
98//
99// Copyright (c) 2025, Timo Behrend
100//
101// Redistribution and use in source and binary forms, with or without
102// modification, are permitted provided that the following conditions are met:
103//
104// 1. Redistributions of source code must retain the above copyright notice, this
105// list of conditions and the following disclaimer.
106//
107// 2. Redistributions in binary form must reproduce the above copyright notice,
108// this list of conditions and the following disclaimer in the documentation
109// and/or other materials provided with the distribution.
110//
111// 3. Neither the name of the copyright holder nor the names of its
112// contributors may be used to endorse or promote products derived from
113// this software without specific prior written permission.
114//
115// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
116// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
117// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
118// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
119// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
120// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
121// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
122// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
123// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
124// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Groups LineGroups belonging to the same Source
Definition: report.hpp:67
Interface implemented by renderers that turn reports into output (e.g., text)
Definition: report.hpp:195
Represents a short textual annotation attached to a Span in a source.
Definition: label.hpp:12
A set of labels that belong to the same 1-based line number.
Definition: report.hpp:28
Represents a fully constructed diagnostic report to be rendered.
Definition: report.hpp:109
std::unordered_map< std::shared_ptr< Source >, FileGroup > MappedFileGroups
Definition: report.hpp:111
A plain-text renderer for diagnostic Reports.
Definition: renderer.hpp:13
void render(const Report &report, std::ostream &stream) override
Renders an entire report to the stream.
void render(const LineGroup &line_group, std::ostream &stream) override
Renders a single line group to the stream.
static size_t widest_line_number(const Report::MappedFileGroups &groups, size_t padding)
Computes the width of the widest line number across groups, plus padding.
TextRenderer(const Report &report)
Initializes the renderer with a reference layout taken from a Report
void render(const FileGroup &file_group, std::ostream &stream) override
Renders a single file group to the stream.
static size_t render(const Label &label, std::ostream &stream, const std::vector< std::string > &text_lines, size_t text_index, bool active_render, size_t column_start=0)
Renders an individual label, optionally in active mode to draw the actual span arrows.
static std::vector< std::string > wrap_text(const std::string &text, size_t max_width)
Wraps the given text to lines no longer than max_width characters.
void render(const Severity &severity, std::ostream &stream) override
Renders just the severity label to the stream.
Definition: label.hpp:5
Severity
Indicates the importance of a diagnostic.
Definition: report.hpp:18