diff --git a/Console.cc b/Console.cc index b8f9ea3..5b08477 100644 --- a/Console.cc +++ b/Console.cc @@ -244,11 +244,7 @@ namespace Alepha::Hydrogen ::detail:: Console_m --"dump-color-env-var"_option << [] { std::cout << "export " << colorsEnv() << "-\""; - AutoRAII delimiters - { - []{ std::cout << StartDelimitedList{ ":" }; }, - []{ std::cout << EndDelimitedList; } - }; + auto scopedList= adaptStream( StartDelimitedList{ ":" }, std::cout ); for( const auto &[ name, sgr ]: colorVariables() ) { std::cout << NextItem << name.name << "=" << sgr.code; diff --git a/IOStreams/StackableStreambuf.h b/IOStreams/StackableStreambuf.h index 843d4d4..ab608d7 100644 --- a/IOStreams/StackableStreambuf.h +++ b/IOStreams/StackableStreambuf.h @@ -10,6 +10,8 @@ static_assert( __cplusplus > 2020'99 ); #include #include +#include + namespace Alepha::Hydrogen::IOStreams ::detail:: StackableStreambuf_m { inline namespace exports @@ -169,6 +171,17 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: StackableStreambuf_m std::ostream & operator << ( std::ostream &os, PopStack ); std::istream & operator >> ( std::istream &is, PopStack ); } + + template< typename T > + [[nodiscard]] auto + adaptStream( PushStack< T > &¶ms, std::ostream &os ) + { + return AutoRAII + { + [&os, ¶ms] { build_streambuf( os, std::move( params ) ); }, + [&os] { os << PopStack{}; } + }; + } } namespace Alepha::Hydrogen::IOStreams::inline exports::inline StackableStreambuf_m diff --git a/ProgramOptions.cc b/ProgramOptions.cc index 6d121d8..64a6a20 100644 --- a/ProgramOptions.cc +++ b/ProgramOptions.cc @@ -195,11 +195,7 @@ namespace Alepha::Hydrogen ::detail:: ProgramOptions_m for( const auto &[ name, def ]: programOptions() ) { // When turning off wrapping, here, we also emit a newline between entries. - AutoRAII wrapping - { - [&] { std::cout << StartWrap{ width, alignmentWidth }; }, - [] { std::cout << EndWrap << std::endl; } - }; + auto wrapping= adaptStream( StartWrap{ width, alignmentWidth }, std::cout ); const auto &[ _, helpText, defaultBuilder, domains ]= def; @@ -217,11 +213,7 @@ namespace Alepha::Hydrogen ::detail:: ProgramOptions_m substitutions[ "canonical-name"s ]= lambaste<=canonicalProgramName.value(); } - AutoRAII substitution - { - [&]{ std::cout << StartSubstitutions{ '!', substitutions }; }, - [] { std::cout << EndSubstitutions; } - }; + auto substitution= adaptStream( StartSubstitutions{ '!', substitutions }, std::cout ); // How much unused of the max width there will be const std::size_t padding= alignmentWidth - name.size() - 2; @@ -289,16 +281,8 @@ namespace Alepha::Hydrogen ::detail:: ProgramOptions_m if( canonicalName.has_value() ) substitutions[ "canonical-name"s ]= lambaste<=canonicalName.value(); - AutoRAII wrapping - { - [] { std::cout << StartWrap{ getConsoleWidth() }; }, - [] { std::cout << EndWrap; } - }; - AutoRAII substitution - { - [&]{ std::cout << StartSubstitutions{ '!', substitutions }; }, - [] { std::cout << EndSubstitutions; } - }; + auto wrapping= adaptStream( StartWrap{ getConsoleWidth() }, std::cout ); + auto substitution= adaptStream( StartSubstitutions{ '!', substitutions }, std::cout ); std::cout << helpMessage << std::endl << std::endl; }