Pretty Diagnostics
Create your own pretty diagnostics
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iomanip>
4#include <string>
5#include <string_view>
6
7namespace pretty_diagnostics {
8
17std::string escape_string(std::string_view input);
18
26template <typename T>
27T max(T first) {
28 return first;
29}
30
40template <typename T, typename... Ts>
41T max(T first, Ts... rest) {
42 T rest_max = max(rest...);
43 return (first < rest_max) ? rest_max : first;
44}
45
53template <typename T>
54T min(T first) {
55 return first;
56}
57
67template <typename T, typename... Ts>
68T min(T first, Ts... rest) {
69 T rest_min = min(rest...);
70 return (rest_min < first) ? rest_min : first;
71}
72
73size_t get_stream_width(const std::ostream &stream);
74
78struct VisualChar {
80 size_t byte_count;
81};
82
90[[nodiscard]] VisualChar get_visual_char(std::string_view input, size_t index);
91
102size_t visual_width(std::string_view input);
103
112size_t to_visual_column(std::string_view line, size_t byte_column);
113
122size_t from_visual_column(std::string_view line, size_t visual_column);
123
124} // namespace pretty_diagnostics
125
126// BSD 3-Clause License
127//
128// Copyright (c) 2025, Timo Behrend
129//
130// Redistribution and use in source and binary forms, with or without
131// modification, are permitted provided that the following conditions are met:
132//
133// 1. Redistributions of source code must retain the above copyright notice, this
134// list of conditions and the following disclaimer.
135//
136// 2. Redistributions in binary form must reproduce the above copyright notice,
137// this list of conditions and the following disclaimer in the documentation
138// and/or other materials provided with the distribution.
139//
140// 3. Neither the name of the copyright holder nor the names of its
141// contributors may be used to endorse or promote products derived from
142// this software without specific prior written permission.
143//
144// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
145// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
146// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
147// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
148// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
149// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
150// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
151// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
152// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
153// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition: color.hpp:5
size_t get_stream_width(const std::ostream &stream)
size_t visual_width(std::string_view input)
Calculates the visual display width of a UTF-8 string (for terminal display)
T max(T first)
Returns the maximum of a single value.
Definition: utils.hpp:27
size_t to_visual_column(std::string_view line, size_t byte_column)
Returns the visual column of a specific byte column in a UTF-8 string.
T min(T first)
Returns the minimum of a single value.
Definition: utils.hpp:54
std::string escape_string(std::string_view input)
Escapes control characters and quotes in a string for safe display. For example, converts newlines to...
VisualChar get_visual_char(std::string_view input, size_t index)
Returns the visual width and byte count of a given UTF8 character.
size_t from_visual_column(std::string_view line, size_t visual_column)
Maps a visual column to a UTF-8 byte index in a line.
A structure to contian the return values from get_visual_char
Definition: utils.hpp:78
size_t byte_count
Definition: utils.hpp:80
size_t visual_width
Definition: utils.hpp:79