| 3217 | class Text { |
| 3218 | public: |
| 3219 | Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() ) |
| 3220 | : attr( _attr ) |
| 3221 | { |
| 3222 | std::string wrappableChars = " [({.,/|\\-"; |
| 3223 | std::size_t indent = _attr.initialIndent != std::string::npos |
| 3224 | ? _attr.initialIndent |
| 3225 | : _attr.indent; |
| 3226 | std::string remainder = _str; |
| 3227 | |
| 3228 | while( !remainder.empty() ) { |
| 3229 | if( lines.size() >= 1000 ) { |
| 3230 | lines.push_back( "... message truncated due to excessive size" ); |
| 3231 | return; |
| 3232 | } |
| 3233 | std::size_t tabPos = std::string::npos; |
| 3234 | std::size_t width = (std::min)( remainder.size(), _attr.width - indent ); |
| 3235 | std::size_t pos = remainder.find_first_of( '\n' ); |
| 3236 | if( pos <= width ) { |
| 3237 | width = pos; |
| 3238 | } |
| 3239 | pos = remainder.find_last_of( _attr.tabChar, width ); |
| 3240 | if( pos != std::string::npos ) { |
| 3241 | tabPos = pos; |
| 3242 | if( remainder[width] == '\n' ) |
| 3243 | width--; |
| 3244 | remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 ); |
| 3245 | } |
| 3246 | |
| 3247 | if( width == remainder.size() ) { |
| 3248 | spliceLine( indent, remainder, width ); |
| 3249 | } |
| 3250 | else if( remainder[width] == '\n' ) { |
| 3251 | spliceLine( indent, remainder, width ); |
| 3252 | if( width <= 1 || remainder.size() != 1 ) |
| 3253 | remainder = remainder.substr( 1 ); |
| 3254 | indent = _attr.indent; |
| 3255 | } |
| 3256 | else { |
| 3257 | pos = remainder.find_last_of( wrappableChars, width ); |
| 3258 | if( pos != std::string::npos && pos > 0 ) { |
| 3259 | spliceLine( indent, remainder, pos ); |
| 3260 | if( remainder[0] == ' ' ) |
| 3261 | remainder = remainder.substr( 1 ); |
| 3262 | } |
| 3263 | else { |
| 3264 | spliceLine( indent, remainder, width-1 ); |
| 3265 | lines.back() += "-"; |
| 3266 | } |
| 3267 | if( lines.size() == 1 ) |
| 3268 | indent = _attr.indent; |
| 3269 | if( tabPos != std::string::npos ) |
| 3270 | indent += tabPos; |
| 3271 | } |
| 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | void spliceLine( std::size_t _indent, std::string& _remainder, std::size_t _pos ) { |
| 3276 | lines.push_back( std::string( _indent, ' ' ) + _remainder.substr( 0, _pos ) ); |
nothing calls this directly
no test coverage detected