Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions projects/tests/catch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3048,17 +3048,36 @@ namespace STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE {

namespace Tbc {

struct TextAttributes {
static std::size_t getDefaultWidth() {
#ifdef TBC_TEXT_FORMAT_CONSOLE_WIDTH
const unsigned int consoleWidth = TBC_TEXT_FORMAT_CONSOLE_WIDTH;
return TBC_TEXT_FORMAT_CONSOLE_WIDTH;
#else
const unsigned int consoleWidth = 80;
static std::size_t width = 0;

if( !width ) {
// Determining the real console width is not trivial as it
// needs to be done in different ways for different platforms,
// so we rely on the standard environment variable instead.
if( char const* const colstr = std::getenv("COLUMNS") ) {
int const colnum = std::atoi(colstr);
if( colnum > 0 )
width = static_cast<std::size_t>(colnum);
}

// Fall back to the hard-coded default.
if( !width )
width = 80;
}

return width;
#endif
}

struct TextAttributes {
TextAttributes()
: initialIndent( std::string::npos ),
indent( 0 ),
width( consoleWidth-1 ),
width( getDefaultWidth()-1 ),
tabChar( '\t' )
{}

Expand Down
11 changes: 4 additions & 7 deletions srcs/clara.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ namespace Clara {

namespace Detail {

#ifdef CLARA_CONSOLE_WIDTH
const unsigned int consoleWidth = CLARA_CONFIG_CONSOLE_WIDTH;
#else
const unsigned int consoleWidth = 80;
#endif

// Use this to try and stop compiler from warning about unreachable code
inline bool isTrue( bool value ) { return value; }

Expand Down Expand Up @@ -513,7 +507,10 @@ namespace Clara {
m_boundProcessName = new Detail::BoundUnaryMethod<C,M>( _unaryMethod );
}

void optUsage( std::ostream& os, std::size_t indent = 0, std::size_t width = Detail::consoleWidth ) const {
void optUsage( std::ostream& os, std::size_t indent = 0, std::size_t width = 0 ) const {
if( !width )
width = Detail::TextAttributes::getDefaultWidth();

typename std::vector<Arg>::const_iterator itBegin = m_options.begin(), itEnd = m_options.end(), it;
std::size_t maxWidth = 0;
for( it = itBegin; it != itEnd; ++it )
Expand Down
28 changes: 24 additions & 4 deletions srcs/tbc_text_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define TBC_TEXT_FORMAT_H_INCLUDED
#endif

#include <cstdlib>
#include <string>
#include <vector>
#include <sstream>
Expand All @@ -23,17 +24,36 @@ namespace STITCH_TBC_TEXT_FORMAT_OUTER_NAMESPACE {

namespace Tbc {

struct TextAttributes {
static std::size_t getDefaultWidth() {
#ifdef TBC_TEXT_FORMAT_CONSOLE_WIDTH
const unsigned int consoleWidth = TBC_TEXT_FORMAT_CONSOLE_WIDTH;
return TBC_TEXT_FORMAT_CONSOLE_WIDTH;
#else
const unsigned int consoleWidth = 80;
static std::size_t width = 0;

if( !width ) {
// Determining the real console width is not trivial as it
// needs to be done in different ways for different platforms,
// so we rely on the standard environment variable instead.
if( char const* const colstr = std::getenv("COLUMNS") ) {
int const colnum = std::atoi(colstr);
if( colnum > 0 )
width = static_cast<std::size_t>(colnum);
}

// Fall back to the hard-coded default.
if( !width )
width = 80;
}

return width;
#endif
}

struct TextAttributes {
TextAttributes()
: initialIndent( std::string::npos ),
indent( 0 ),
width( consoleWidth-1 ),
width( getDefaultWidth()-1 ),
tabChar( '\t' )
{}

Expand Down