From 7265193539e621eb56a84b61cbc69e400105d47a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 4 Mar 2016 22:31:42 +0100 Subject: [PATCH] Use COLUMNS environment variable for console width If this variable is defined (which is not always the case but at least respecting it allows us to wrap things properly if it is), use it instead of the hard-coded 80 character line width. Closes #7. --- projects/tests/catch.hpp | 27 +++++++++++++++++++++++---- srcs/clara.h | 11 ++++------- srcs/tbc_text_format.h | 28 ++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/projects/tests/catch.hpp b/projects/tests/catch.hpp index 88f0088..6ceaec2 100644 --- a/projects/tests/catch.hpp +++ b/projects/tests/catch.hpp @@ -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(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' ) {} diff --git a/srcs/clara.h b/srcs/clara.h index 2ab0fa3..3869e97 100644 --- a/srcs/clara.h +++ b/srcs/clara.h @@ -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; } @@ -513,7 +507,10 @@ namespace Clara { m_boundProcessName = new Detail::BoundUnaryMethod( _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::const_iterator itBegin = m_options.begin(), itEnd = m_options.end(), it; std::size_t maxWidth = 0; for( it = itBegin; it != itEnd; ++it ) diff --git a/srcs/tbc_text_format.h b/srcs/tbc_text_format.h index 8d7baa3..35becb5 100644 --- a/srcs/tbc_text_format.h +++ b/srcs/tbc_text_format.h @@ -11,6 +11,7 @@ #define TBC_TEXT_FORMAT_H_INCLUDED #endif +#include #include #include #include @@ -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(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' ) {}