1818#include < ctype.h>
1919#include < errno.h>
2020#include < stdlib.h>
21+ #include < unistd.h>
2122#include < string.h>
23+ #include < sys/ioctl.h>
2224#include < cmath>
2325#include < iostream>
2426#include < string>
@@ -903,9 +905,11 @@ namespace CommandLine {
903905 * per columns between calls. Examples: bold, underline.
904906 * @param out The resulting characters will be appended to this
905907 * string stream.
908+ * @param breakAll Break text after every character.
909+ * For languages like Japanese and Chinese.
906910 */
907- inline void FormatLine (std::string &text, int &cursor, int limit, char softHypen,
908- int &textAttribute, std::stringstream &out) {
911+ inline void FormatLine (const std::string &text, int &cursor, int limit, char softHypen,
912+ int &textAttribute, std::stringstream &out, bool breakAll ) {
909913
910914 int length = text.length ();
911915 int charCount = 0 ;
@@ -972,6 +976,32 @@ namespace CommandLine {
972976 continue ;
973977 }
974978
979+ /*
980+ Check for VT100 escape sequences.
981+ Allow only text attributes: ESC[0m, ESC[1m, etc.
982+ Output them, but don't include them
983+ in the char count.
984+ */
985+ if (c == ' \033 ' ) {
986+ if (cursor + 3 < length) {
987+ if (text[cursor + 1 ] == ' [' && text[cursor + 3 ] == ' m'
988+ && text[cursor + 2 ] >= 48 && text[cursor + 2 ] <= 56
989+ && text[cursor + 2 ] != 51 && text[cursor + 2 ] != 54 ) {
990+
991+ int value = text[cursor + 2 ] - 48 ;
992+
993+ if (!textAttributeWasSetInLastWord) {
994+ textAttribute = value;
995+ textAttributeWasSetInLastWord = true ;
996+ }
997+
998+ lastWord << " \033 [" << value << ' m' ;
999+ cursor += 3 ;
1000+ continue ;
1001+ }
1002+ }
1003+ }
1004+
9751005 /*
9761006 Check if we reached the character limit
9771007 for the line if we include the current
@@ -998,7 +1028,7 @@ namespace CommandLine {
9981028 didn't fit into the allowed width, then
9991029 force "break all" behavior.
10001030 */
1001- if (lastWordPosition == 0 ) {
1031+ if (lastWordPosition == 0 || breakAll ) {
10021032 out << lastWord.str ();
10031033 charCount += lastWordCharCount;
10041034 return ;
@@ -1011,7 +1041,7 @@ namespace CommandLine {
10111041 if (!beforeLastWordEndedInHyphen) {
10121042 /*
10131043 Output a dash for the soft hyphen only when
1014- the word before the last word doesn't start
1044+ the word before the last word doesn't end
10151045 with a hyphen.
10161046 */
10171047 out << ' -' ;
@@ -1043,32 +1073,6 @@ namespace CommandLine {
10431073 continue ;
10441074 }
10451075
1046- /*
1047- Check for VT100 escape sequences.
1048- Allow only text attributes: ESC[0m, ESC[1m, etc.
1049- Output them, but don't include them
1050- in the char count.
1051- */
1052- if (c == ' \033 ' ) {
1053- if (cursor + 3 < length) {
1054- if (text[cursor + 1 ] == ' [' && text[cursor + 3 ] == ' m'
1055- && text[cursor + 2 ] >= 48 && text[cursor + 2 ] <= 56
1056- && text[cursor + 2 ] != 51 && text[cursor + 2 ] != 54 ) {
1057-
1058- int value = text[cursor + 2 ] - 48 ;
1059-
1060- if (!textAttributeWasSetInLastWord) {
1061- textAttribute = value;
1062- textAttributeWasSetInLastWord = true ;
1063- }
1064-
1065- lastWord << " \033 [" << value << ' m' ;
1066- cursor += 3 ;
1067- continue ;
1068- }
1069- }
1070- }
1071-
10721076 /*
10731077 Check for word-breakers (non printable, space, soft hyphen)
10741078 */
@@ -1148,7 +1152,10 @@ namespace CommandLine {
11481152 * @param argv Second parameter of the main function.
11491153 */
11501154 Parser (int argc, char *argv[]) : argc(argc), argv(argv), accu(), Argument(accu) {
1151- this ->screenWidth = 80 ;
1155+ struct winsize size;
1156+ ioctl (STDOUT_FILENO, TIOCGWINSZ, &size);
1157+
1158+ this ->screenWidth = std::max (std::min ((int )size.ws_col - 1 , 80 ), 10 );
11521159 }
11531160
11541161 /* *
@@ -1427,7 +1434,7 @@ namespace CommandLine {
14271434 }
14281435
14291436 /* *
1430- * @brief Wrap a text to the maximal width, to be
1437+ * @brief Wrap a text to the screen width, to be
14311438 * outputted on a terminal.
14321439 *
14331440 * @param text The text to be formatted.
@@ -1565,7 +1572,7 @@ namespace CommandLine {
15651572 terminated++;
15661573 } else {
15671574 this ->FormatLine (texts[column], cursors[column], width[column], softHyphen,
1568- textAttributes[column], buff);
1575+ textAttributes[column], buff, breakAll );
15691576 }
15701577
15711578 if (rightPaddings[column] > 0 ) {
0 commit comments