Text class is now full replacement for LineWrap

This commit is contained in:
Phil Nash 2013-04-20 11:27:28 +01:00
parent 7059c6e1c3
commit 54ca219aad
2 changed files with 38 additions and 34 deletions

View File

@ -42,45 +42,50 @@ namespace Catch {
? _attr.initialIndent ? _attr.initialIndent
: _attr.indent; : _attr.indent;
std::string remainder = _str; std::string remainder = _str;
std::size_t tabPos = std::string::npos;
while( !remainder.empty() ) { while( !remainder.empty() ) {
std::size_t width = _attr.width - indent; assert( lines.size() < 1000 );
std::size_t wrapPos = width; std::size_t tabPos = std::string::npos;
std::size_t width = (std::min)( remainder.size(), _attr.width - indent );
std::size_t pos = remainder.find_first_of( '\n' ); std::size_t pos = remainder.find_first_of( '\n' );
if( pos <= width ) { if( pos <= width ) {
wrapPos = pos; width = pos;
addLine( indent, remainder.substr( 0, pos ) ); }
remainder = remainder.substr( pos+1 ); pos = remainder.find_last_of( _attr.tabChar, width );
if( remainder.empty() ) if( pos != std::string::npos ) {
addLine (indent, "" ); // Trailing newlines result in extra line tabPos = pos;
if( remainder[width] == '\n' )
width--;
remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 );
}
if( width == remainder.size() ) {
addLine( indent, remainder );
remainder = std::string();
}
else if( remainder[width] == '\n' ) {
addLine( indent, remainder.substr( 0, width ) );
indent = _attr.indent;
if( width > 1 && width == remainder.size()-1 )
remainder = remainder.substr( width );
else
remainder = remainder.substr( width+1 );
} }
else { else {
pos = remainder.find_last_of( _attr.tabChar, width ); pos = remainder.find_last_of( wrappableChars, width );
if( pos != std::string::npos ) { if( pos != std::string::npos ) {
tabPos = pos; addLine( indent, remainder.substr( 0, pos ) );
remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 ); if( remainder[pos] == ' ' )
} pos++;
if( remainder.size() <= width ) { remainder = remainder.substr( pos );
addLine( indent, remainder );
remainder = std::string();
} }
else { else {
pos = remainder.find_last_of( wrappableChars, width ); addLine( indent, remainder.substr( 0, width-1 ) + "-" );
if( pos == std::string::npos ) { remainder = remainder.substr( width-1 );
addLine( indent, remainder.substr( 0, width-1 ) + "-" );
remainder = remainder.substr( width-1 );
}
else {
addLine( indent, remainder.substr( 0, pos ) );
if( remainder[pos] == ' ' )
pos++;
remainder = remainder.substr( pos );
}
} }
if( lines.size() == 1 )
indent = _attr.indent;
if( tabPos != std::string::npos )
indent += tabPos;
} }
indent = tabPos == std::string::npos
? _attr.indent
: indent + tabPos;
}; };
} }
void addLine( std::size_t indent, std::string const& _line ) { void addLine( std::size_t indent, std::string const& _line ) {

View File

@ -507,10 +507,10 @@ TEST_CASE( "Long strings can be wrapped", "[wrap]" ) {
SECTION( "With tabs", "" ) { SECTION( "With tabs", "" ) {
// guide: 1234567890123456789 // guide: 1234567890123456789
std::string testString = "one two /tthree four five six"; std::string testString = "one two \tthree four five six";
CHECK( Text( testString, TextAttributes().setWidth( 18 ) ).toString() CHECK( Text( testString, TextAttributes().setWidth( 15 ) ).toString()
== "one two three/n four/n five/n six" ); == "one two three\n four\n five\n six" );
} }
@ -609,7 +609,6 @@ TEST_CASE( "Strings can be rendered with colour", "[colour]" ) {
} }
TEST_CASE( "Text can be formatted using the Text class", "" ) { TEST_CASE( "Text can be formatted using the Text class", "" ) {
Text text( "hi there" );
CHECK( Text( "hi there" ).toString() == "hi there" ); CHECK( Text( "hi there" ).toString() == "hi there" );