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 <string>
4#include <vector>
5
6#include "report.hpp"
7
8namespace pretty_diagnostics {
9
18struct GlyphSet {
19 std::string corner_top_left;
21 std::string tee_right;
22 std::string cap_left;
23 std::string cap_right;
24 std::string line_vertical;
25 std::string line_horizontal;
26 std::string label_start;
27 std::string label_end;
28 std::string filler;
29 std::string arrow_right;
30};
31
32namespace Glyphs {
40
48} // namespace Glyphs
49
56struct Config {
63};
64
70class TextRenderer final : public IReporterRenderer {
71public:
78 explicit TextRenderer(const Report& report, Config config = {});
79
86 void render(const Severity& severity, std::ostream& stream) override;
87
94 void render(const Report& report, std::ostream& stream) override;
95
102 void render(const FileGroup& file_group, std::ostream& stream) override;
103
110 void render(const LineGroup& line_group, std::ostream& stream) override;
111
125 void render(const Label& label, std::ostream& stream, const std::vector<std::string>& text_lines, size_t text_index, bool active_render,
126 size_t column_start = 0) const;
127
136 [[nodiscard]] static size_t widest_line_number(const Report::MappedFileGroups& groups, size_t padding);
137
146 [[nodiscard]] static std::vector<std::string> wrap_text(const std::string& text, size_t max_width);
147
157 static void print_wrapped_text(const std::string& text, const std::string& wrapped_prefix, size_t max_width, std::ostream& stream);
158
159private:
160 size_t _line_number_width, _snippet_width;
161 std::string _whitespaces;
162 Config _config;
163};
164} // namespace pretty_diagnostics
165
166// BSD 3-Clause License
167//
168// Copyright (c) 2025, Timo Behrend
169//
170// Redistribution and use in source and binary forms, with or without
171// modification, are permitted provided that the following conditions are met:
172//
173// 1. Redistributions of source code must retain the above copyright notice, this
174// list of conditions and the following disclaimer.
175//
176// 2. Redistributions in binary form must reproduce the above copyright notice,
177// this list of conditions and the following disclaimer in the documentation
178// and/or other materials provided with the distribution.
179//
180// 3. Neither the name of the copyright holder nor the names of its
181// contributors may be used to endorse or promote products derived from
182// this software without specific prior written permission.
183//
184// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
185// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
186// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
187// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
188// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
189// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
190// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
191// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
192// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
193// 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 0-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:70
TextRenderer(const Report &report, Config config={})
Initializes the renderer with a reference layout taken from a Report
static void print_wrapped_text(const std::string &text, const std::string &wrapped_prefix, size_t max_width, std::ostream &stream)
Prints the wrapped text into lines no longer than max_width characters and adds a prefix to wrapped l...
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.
void render(const FileGroup &file_group, std::ostream &stream) override
Renders a single file group to the stream.
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.
void 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) const
Renders an individual label, optionally in active mode to draw the actual span arrows.
GlyphSet Unicode()
Returns a Unicode glyph set for rich terminal rendering.
GlyphSet Ascii()
Returns an ASCII-only glyph set for maximum compatibility.
Definition: color.hpp:5
Severity
Indicates the importance of a diagnostic.
Definition: report.hpp:18
Configuration options for the TextRenderer.
Definition: renderer.hpp:56
GlyphSet glyphs
Glyph set used for rendering.
Definition: renderer.hpp:62
Collection of glyphs used for rendering text-based UI elements.
Definition: renderer.hpp:18
std::string line_vertical
Definition: renderer.hpp:24
std::string arrow_right
Definition: renderer.hpp:29
std::string corner_bottom_right
Definition: renderer.hpp:20
std::string filler
Definition: renderer.hpp:28
std::string label_end
Definition: renderer.hpp:27
std::string corner_top_left
Definition: renderer.hpp:19
std::string cap_left
Definition: renderer.hpp:22
std::string line_horizontal
Definition: renderer.hpp:25
std::string label_start
Definition: renderer.hpp:26
std::string cap_right
Definition: renderer.hpp:23
std::string tee_right
Definition: renderer.hpp:21