| 4139 | class Text { |
| 4140 | public: |
| 4141 | Text( std::string const& _str, TextAttributes const& _attr = TextAttributes() ) |
| 4142 | : attr( _attr ) |
| 4143 | { |
| 4144 | std::string wrappableChars = " [({.,/|\\-"; |
| 4145 | std::size_t indent = _attr.initialIndent != std::string::npos |
| 4146 | ? _attr.initialIndent |
| 4147 | : _attr.indent; |
| 4148 | std::string remainder = _str; |
| 4149 | |
| 4150 | while( !remainder.empty() ) { |
| 4151 | if( lines.size() >= 1000 ) { |
| 4152 | lines.push_back( "... message truncated due to excessive size" ); |
| 4153 | return; |
| 4154 | } |
| 4155 | std::size_t tabPos = std::string::npos; |
| 4156 | std::size_t width = (std::min)( remainder.size(), _attr.width - indent ); |
| 4157 | std::size_t pos = remainder.find_first_of( '\n' ); |
| 4158 | if( pos <= width ) { |
| 4159 | width = pos; |
| 4160 | } |
| 4161 | pos = remainder.find_last_of( _attr.tabChar, width ); |
| 4162 | if( pos != std::string::npos ) { |
| 4163 | tabPos = pos; |
| 4164 | if( remainder[width] == '\n' ) |
| 4165 | width--; |
| 4166 | remainder = remainder.substr( 0, tabPos ) + remainder.substr( tabPos+1 ); |
| 4167 | } |
| 4168 | |
| 4169 | if( width == remainder.size() ) { |
| 4170 | spliceLine( indent, remainder, width ); |
| 4171 | } |
| 4172 | else if( remainder[width] == '\n' ) { |
| 4173 | spliceLine( indent, remainder, width ); |
| 4174 | if( width <= 1 || remainder.size() != 1 ) |
| 4175 | remainder = remainder.substr( 1 ); |
| 4176 | indent = _attr.indent; |
| 4177 | } |
| 4178 | else { |
| 4179 | pos = remainder.find_last_of( wrappableChars, width ); |
| 4180 | if( pos != std::string::npos && pos > 0 ) { |
| 4181 | spliceLine( indent, remainder, pos ); |
| 4182 | if( remainder[0] == ' ' ) |
| 4183 | remainder = remainder.substr( 1 ); |
| 4184 | } |
| 4185 | else { |
| 4186 | spliceLine( indent, remainder, width-1 ); |
| 4187 | lines.back() += "-"; |
| 4188 | } |
| 4189 | if( lines.size() == 1 ) |
| 4190 | indent = _attr.indent; |
| 4191 | if( tabPos != std::string::npos ) |
| 4192 | indent += tabPos; |
| 4193 | } |
| 4194 | } |
| 4195 | } |
| 4196 | |
| 4197 | void spliceLine( std::size_t _indent, std::string& _remainder, std::size_t _pos ) { |
| 4198 | lines.push_back( std::string( _indent, ' ' ) + _remainder.substr( 0, _pos ) ); |