1
0
forked from Alepha/Alepha

AutoRAII based stream adaptors.

This commit is contained in:
2024-04-03 16:28:55 -04:00
parent 435bd912a4
commit 83034c501f
3 changed files with 18 additions and 25 deletions

View File

@ -244,11 +244,7 @@ namespace Alepha::Hydrogen ::detail:: Console_m
--"dump-color-env-var"_option << [] --"dump-color-env-var"_option << []
{ {
std::cout << "export " << colorsEnv() << "-\""; std::cout << "export " << colorsEnv() << "-\"";
AutoRAII delimiters auto scopedList= adaptStream( StartDelimitedList{ ":" }, std::cout );
{
[]{ std::cout << StartDelimitedList{ ":" }; },
[]{ std::cout << EndDelimitedList; }
};
for( const auto &[ name, sgr ]: colorVariables() ) for( const auto &[ name, sgr ]: colorVariables() )
{ {
std::cout << NextItem << name.name << "=" << sgr.code; std::cout << NextItem << name.name << "=" << sgr.code;

View File

@ -10,6 +10,8 @@ static_assert( __cplusplus > 2020'99 );
#include <memory> #include <memory>
#include <stack> #include <stack>
#include <Alepha/AutoRAII.h>
namespace Alepha::Hydrogen::IOStreams ::detail:: StackableStreambuf_m namespace Alepha::Hydrogen::IOStreams ::detail:: StackableStreambuf_m
{ {
inline namespace exports inline namespace exports
@ -169,6 +171,17 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: StackableStreambuf_m
std::ostream & operator << ( std::ostream &os, PopStack ); std::ostream & operator << ( std::ostream &os, PopStack );
std::istream & operator >> ( std::istream &is, PopStack ); std::istream & operator >> ( std::istream &is, PopStack );
} }
template< typename T >
[[nodiscard]] auto
adaptStream( PushStack< T > &&params, std::ostream &os )
{
return AutoRAII
{
[&os, &params] { build_streambuf( os, std::move( params ) ); },
[&os] { os << PopStack{}; }
};
}
} }
namespace Alepha::Hydrogen::IOStreams::inline exports::inline StackableStreambuf_m namespace Alepha::Hydrogen::IOStreams::inline exports::inline StackableStreambuf_m

View File

@ -195,11 +195,7 @@ namespace Alepha::Hydrogen ::detail:: ProgramOptions_m
for( const auto &[ name, def ]: programOptions() ) for( const auto &[ name, def ]: programOptions() )
{ {
// When turning off wrapping, here, we also emit a newline between entries. // When turning off wrapping, here, we also emit a newline between entries.
AutoRAII wrapping auto wrapping= adaptStream( StartWrap{ width, alignmentWidth }, std::cout );
{
[&] { std::cout << StartWrap{ width, alignmentWidth }; },
[] { std::cout << EndWrap << std::endl; }
};
const auto &[ _, helpText, defaultBuilder, domains ]= def; const auto &[ _, helpText, defaultBuilder, domains ]= def;
@ -217,11 +213,7 @@ namespace Alepha::Hydrogen ::detail:: ProgramOptions_m
substitutions[ "canonical-name"s ]= lambaste<=canonicalProgramName.value(); substitutions[ "canonical-name"s ]= lambaste<=canonicalProgramName.value();
} }
AutoRAII substitution auto substitution= adaptStream( StartSubstitutions{ '!', substitutions }, std::cout );
{
[&]{ std::cout << StartSubstitutions{ '!', substitutions }; },
[] { std::cout << EndSubstitutions; }
};
// How much unused of the max width there will be // How much unused of the max width there will be
const std::size_t padding= alignmentWidth - name.size() - 2; 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(); if( canonicalName.has_value() ) substitutions[ "canonical-name"s ]= lambaste<=canonicalName.value();
AutoRAII wrapping auto wrapping= adaptStream( StartWrap{ getConsoleWidth() }, std::cout );
{ auto substitution= adaptStream( StartSubstitutions{ '!', substitutions }, std::cout );
[] { std::cout << StartWrap{ getConsoleWidth() }; },
[] { std::cout << EndWrap; }
};
AutoRAII substitution
{
[&]{ std::cout << StartSubstitutions{ '!', substitutions }; },
[] { std::cout << EndSubstitutions; }
};
std::cout << helpMessage << std::endl << std::endl; std::cout << helpMessage << std::endl << std::endl;
} }