forked from Alepha/Alepha
AutoRAII based stream adaptors.
This commit is contained in:
@ -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;
|
||||
|
@ -10,6 +10,8 @@ static_assert( __cplusplus > 2020'99 );
|
||||
#include <memory>
|
||||
#include <stack>
|
||||
|
||||
#include <Alepha/AutoRAII.h>
|
||||
|
||||
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
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user