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 "color.hpp"
7#include "report.hpp"
8
9namespace pretty_diagnostics {
10
19struct GlyphSet {
20 std::string corner_top_left;
22 std::string tee_right;
23 std::string cap_left;
24 std::string cap_right;
25 std::string line_vertical;
26 std::string line_horizontal;
27 std::string label_start;
28 std::string label_end;
29 std::string filler;
30 std::string arrow_right;
31};
32
33namespace Glyphs {
41
49} // namespace Glyphs
50
58struct ColorSet {
59 std::vector<color::Code> error;
60 std::vector<color::Code> warning;
61 std::vector<color::Code> info;
62 std::vector<color::Code> unknown;
63};
64
65namespace Colors {
74}
75
82struct Config {
89
96};
97
103class TextRenderer final : public IReporterRenderer {
104public:
111 explicit TextRenderer(const Report& report, Config config = {});
112
119 void render(const Severity& severity, std::ostream& stream) override;
120
127 void render(const Report& report, std::ostream& stream) override;
128
135 void render(const FileGroup& file_group, std::ostream& stream) override;
136
143 void render(const LineGroup& line_group, std::ostream& stream) override;
144
158 void render(const Label& label, std::ostream& stream, const std::vector<std::string>& text_lines, size_t text_index, bool active_render,
159 size_t column_start = 0) const;
160
170 static void render_wrapped_text(const std::string& text, const std::string& wrapped_prefix, size_t max_width, std::ostream& stream);
171
180 [[nodiscard]] static size_t widest_line_number(const Report::MappedFileGroups& groups, size_t padding);
181
190 [[nodiscard]] static std::vector<std::string> wrap_text(const std::string& text, size_t max_width);
191
192private:
193 size_t _line_number_width, _snippet_width;
194 std::string _whitespaces;
195 Config _config;
196};
197} // namespace pretty_diagnostics
198
199// BSD 3-Clause License
200//
201// Copyright (c) 2025, Timo Behrend
202//
203// Redistribution and use in source and binary forms, with or without
204// modification, are permitted provided that the following conditions are met:
205//
206// 1. Redistributions of source code must retain the above copyright notice, this
207// list of conditions and the following disclaimer.
208//
209// 2. Redistributions in binary form must reproduce the above copyright notice,
210// this list of conditions and the following disclaimer in the documentation
211// and/or other materials provided with the distribution.
212//
213// 3. Neither the name of the copyright holder nor the names of its
214// contributors may be used to endorse or promote products derived from
215// this software without specific prior written permission.
216//
217// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
218// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
220// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
221// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
222// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
223// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
224// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
225// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
226// 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:103
TextRenderer(const Report &report, Config config={})
Initializes the renderer with a reference layout taken from a Report
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 void render_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...
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.
ColorSet Default()
Returns the default ANSI color set for rendering.
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:6
Severity
Indicates the importance of a diagnostic.
Definition: report.hpp:18
Collection of color styles used for semantic highlighting.
Definition: renderer.hpp:58
std::vector< color::Code > error
Definition: renderer.hpp:59
std::vector< color::Code > unknown
Definition: renderer.hpp:62
std::vector< color::Code > info
Definition: renderer.hpp:61
std::vector< color::Code > warning
Definition: renderer.hpp:60
Configuration options for the TextRenderer.
Definition: renderer.hpp:82
GlyphSet glyphs
Glyph set used for rendering.
Definition: renderer.hpp:88
ColorSet colors
Color set used for rendering.
Definition: renderer.hpp:95
Collection of glyphs used for rendering text-based UI elements.
Definition: renderer.hpp:19
std::string line_vertical
Definition: renderer.hpp:25
std::string arrow_right
Definition: renderer.hpp:30
std::string corner_bottom_right
Definition: renderer.hpp:21
std::string filler
Definition: renderer.hpp:29
std::string label_end
Definition: renderer.hpp:28
std::string corner_top_left
Definition: renderer.hpp:20
std::string cap_left
Definition: renderer.hpp:23
std::string line_horizontal
Definition: renderer.hpp:26
std::string label_start
Definition: renderer.hpp:27
std::string cap_right
Definition: renderer.hpp:24
std::string tee_right
Definition: renderer.hpp:22