From f411b2683ce9fc4b842d31087b10a02572d36451 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 7 Aug 2025 18:32:14 -0500 Subject: [PATCH] xo-alloc / xo-object: utest coverage + assorted bugfixes --- include/xo/indentlog/print/color.hpp | 30 ++++++++++++++++++----- include/xo/indentlog/print/pretty.hpp | 22 ++++++++++++----- include/xo/indentlog/print/tag.hpp | 4 +-- include/xo/indentlog/print/tag_config.hpp | 17 ++++++++++--- 4 files changed, 56 insertions(+), 17 deletions(-) diff --git a/include/xo/indentlog/print/color.hpp b/include/xo/indentlog/print/color.hpp index ef91f49..268b6d9 100644 --- a/include/xo/indentlog/print/color.hpp +++ b/include/xo/indentlog/print/color.hpp @@ -144,7 +144,8 @@ namespace xo { class color_impl { public: color_impl(coloring_control_flags flags, color_spec_type spec, Contents && contents) - : flags_{flags}, spec_{spec}, contents_{std::forward(contents)} {} + : flags_{flags}, + spec_{spec}, contents_{std::forward(contents)} {} color_spec_type const & spec() const { return spec_; } std::uint32_t color() const { return spec_.code(); } @@ -162,32 +163,49 @@ namespace xo { } /*print*/ private: - /* controls independently what to print + /** controls independently what to print * \033[38;5;117m hello, world! \033[0m * <------------> <-----------> <-----> * color_on contents color_off - */ + **/ coloring_control_flags flags_ = coloring_control_flags::none; + /** color to use, when @ref color_enabled_ is on **/ color_spec_type spec_; + /** contents to print surrounded by color escapes **/ Contents contents_; }; /*color_impl*/ + template + color_impl with_color_if(bool color_enabled, color_spec_type const & spec, Contents && contents) { + return color_impl((color_enabled + ? coloring_control_flags::all + : coloring_control_flags::contents), + spec, + std::forward(contents)); + } + template color_impl with_color(color_spec_type const & spec, Contents && contents) { - return color_impl(coloring_control_flags::all, spec, std::forward(contents)); + return color_impl(coloring_control_flags::all, + spec, + std::forward(contents)); } inline color_impl color_on(color_spec_type const & spec) { - return color_impl(coloring_control_flags::color_on, spec, 0); + return color_impl(coloring_control_flags::color_on, + spec, + 0); } inline color_impl color_off(color_spec_type const & spec) { /* any spec other than color_spec_type::none() works here */ - return color_impl(coloring_control_flags::color_off, spec, 0); + return color_impl(coloring_control_flags::color_off, + spec, + 0); } template diff --git a/include/xo/indentlog/print/pretty.hpp b/include/xo/indentlog/print/pretty.hpp index 8068a29..3ae010e 100644 --- a/include/xo/indentlog/print/pretty.hpp +++ b/include/xo/indentlog/print/pretty.hpp @@ -20,12 +20,19 @@ namespace xo { /** @class ppstate * @brief hold pretty-printer state * + * Need a log_streambuf instance to keep track of indent. + * Application code will likely prefer @ref pretty_printer + * * Use: + * @code * ppconfig ppc; - * ppstate pps(&cout, 0, &ppc); + * log_streambuf sbuf(buf_z); + * stringstream ss(&sbuf); + * ppstate pps(0, &ppc, &ss, &sbuf); * * pps.pretty("first"); * pps.pretty("second"); + * @endcode **/ struct ppstate { using streambuf_type = log_streambuf>; @@ -176,6 +183,8 @@ namespace xo { /** @class ppstate_standalone * @brief like ppstate, but also holds streambuf + * + * editor bait: pretty_printer prettyprinter */ struct ppstate_standalone : public ppstate { explicit ppstate_standalone(std::ostream * os, std::uint32_t ci, const ppconfig * config) @@ -377,8 +386,9 @@ namespace xo { ppii.pps()->write(" "); /* must color here, because we may keep the output if it fits! */ - if (!ppii.pps()->print_upto(with_color(color_spec_type::yellow(), // tag_config::tag_color, - concat((char const *)":", tag.name())))) + if (!ppii.pps()->print_upto(with_color_if(tag_config::tag_color_enabled, + color_spec_type::yellow(), // tag_config::tag_color, + concat((char const *)":", tag.name())))) return false; ppii.pps()->write(" "); @@ -395,8 +405,9 @@ namespace xo { if (tag.prefix_space()) ppii.pps()->write(" "); - ppii.pps()->write(with_color(color_spec_type::yellow(), //tag_config::tag_color, - concat((char const *)":", tag.name()))); + ppii.pps()->write(with_color_if(tag_config::tag_color_enabled, + color_spec_type::yellow(), //tag_config::tag_color, + concat((char const *)":", tag.name()))); ppii.pps()->newline_indent(ppii.ci1()); ppii.pps()->pretty(tag.value()); @@ -446,6 +457,5 @@ namespace xo { return *this; } - } /*namespace print*/ } /*namespace xo*/ diff --git a/include/xo/indentlog/print/tag.hpp b/include/xo/indentlog/print/tag.hpp index 678eb0f..026e820 100644 --- a/include/xo/indentlog/print/tag.hpp +++ b/include/xo/indentlog/print/tag.hpp @@ -181,7 +181,7 @@ namespace xo { if (PrefixSpace) s << " "; - s << with_color(tag_config::tag_color, concat((char const *)":", tag.name())) + s << with_color_if(tag_config::tag_color_enabled, tag_config::tag_color, concat((char const *)":", tag.name())) << " "; if (TagStyle == tagstyle::autoescape) @@ -204,7 +204,7 @@ namespace xo { if (PrefixSpace) s << " "; - s << with_color(tag_config::tag_color, concat((char const *)":", tag.name())) + s << with_color_if(tag_config::tag_color_enabled, tag_config::tag_color, concat((char const *)":", tag.name())) << " "; if (TagStyle == tagstyle::autoescape) diff --git a/include/xo/indentlog/print/tag_config.hpp b/include/xo/indentlog/print/tag_config.hpp index f86b2c8..e9c76cd 100644 --- a/include/xo/indentlog/print/tag_config.hpp +++ b/include/xo/indentlog/print/tag_config.hpp @@ -6,18 +6,29 @@ #include namespace xo { - /* Tag here b/c we want header-only library */ + /** @class tag_config_impl + * @brief configuration for tag-printing + * + * note: Tag here b/c we want header-only library + **/ template struct tag_config_impl { - /* color to use for tags + /** true to enable colored tag printing **/ + static bool tag_color_enabled; + /** color to use for tags * os << tag("foo", foovalue) * to produces output like * :foo foovalue * with :foo using .tag_color - */ + **/ static color_spec_type tag_color; }; /*tag_config_impl*/ + template + bool + tag_config_impl::tag_color_enabled = true; + + /** default value is a light gray **/ template color_spec_type tag_config_impl::tag_color = color_spec_type::xterm(245);