| 139 | |
| 140 | |
| 141 | void HelpFormatter::formatText(ostream& ostr, const string& text, int indent) const { |
| 142 | int pos = _indent; |
| 143 | size_t maxWordLen = WIDTH - indent; |
| 144 | string word; |
| 145 | for (const char c: text) { |
| 146 | if (c == '\n') { |
| 147 | flushWord(ostr, pos, word, indent); |
| 148 | ostr << '\n'; |
| 149 | pos = 0; |
| 150 | while (pos < indent) { |
| 151 | ostr << ' '; |
| 152 | ++pos; |
| 153 | } |
| 154 | } else if (c == '\t') { |
| 155 | flushWord(ostr, pos, word, indent); |
| 156 | if (pos < WIDTH) ++pos; |
| 157 | while (pos < WIDTH && pos % TAB_WIDTH != 0) { |
| 158 | ostr << ' '; |
| 159 | ++pos; |
| 160 | } |
| 161 | } else if (c == ' ') { |
| 162 | flushWord(ostr, pos, word, indent); |
| 163 | if (pos < WIDTH) { |
| 164 | ostr << ' '; |
| 165 | ++pos; |
| 166 | } |
| 167 | } else { |
| 168 | if (word.length() == maxWordLen) |
| 169 | flushWord(ostr, pos, word, indent); |
| 170 | else |
| 171 | word += c; |
| 172 | } |
| 173 | } |
| 174 | flushWord(ostr, pos, word, indent); |
| 175 | } |
| 176 | |
| 177 | void HelpFormatter::flushWord(ostream& ostr, int& pos, string& word, int indent) const { |
| 178 | if ((pos + word.length()) > WIDTH) { |