# HG changeset patch # User Matti Hamalainen # Date 1420096947 -7200 # Node ID fdea1c3acc533015b264bf24b228f88cad66bc4d # Parent 51eec969b07ad41dff046dc7ba5acd2a47d42e8f# Parent c878cdcfea9dd52162efbf6f1050b0149edd3989 Merge. diff -r 51eec969b07a -r fdea1c3acc53 th_args.c --- a/th_args.c Thu Jan 01 09:22:08 2015 +0200 +++ b/th_args.c Thu Jan 01 09:22:27 2015 +0200 @@ -179,37 +179,35 @@ // Skip whitespace at line start while (th_isspace(str[pos]) || str[pos] == '\n') pos++; - + // Handle each word while (str[pos] && str[pos] != '\n') { size_t next; int wlen; + + // Find word length and next break for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++); -// fprintf(stdout, "X '%c', %d .. linelen=%d/%d, wlen=%d\n", str[pos], pos, linelen, width, wlen); - if (linelen + wlen < width) - { - for (;pos < next; pos++, linelen++) - fputc(str[pos], fh); + + // Check if we have too much of text? + if (linelen + wlen >= width) + break; - if (str[next] == '\n' || str[next] == 0) - { - fprintf(fh, "\n"); - break; - } - else - { - fputc(str[pos], fh); - pos++; - linelen++; - } - } + // Print what we have + for (;pos < next; pos++, linelen++) + fputc(str[pos], fh); + + // Check if we are at end of input or hard linefeed + if (str[next] == '\n' || str[next] == 0) + break; else { - fprintf(fh, "\n"); - break; + fputc(str[pos], fh); + pos++; + linelen++; } } + fprintf(fh, "\n"); } } @@ -250,6 +248,6 @@ fprintf(fh, "%-20s", tmpStr); - th_print_wrap(fh, opt->desc, 26, 26, 73); + th_print_wrap(fh, opt->desc, 26, 26, th_term_width() - 2); } } diff -r 51eec969b07a -r fdea1c3acc53 th_util.c --- a/th_util.c Thu Jan 01 09:22:08 2015 +0200 +++ b/th_util.c Thu Jan 01 09:22:27 2015 +0200 @@ -56,6 +56,24 @@ } +int th_term_width() +{ + char *var = getenv("COLUMNS"); + int res = (var != NULL) ? atoi(var) : 80; + if (res < 5) res = 80; + return res; +} + + +int th_term_height() +{ + char *var = getenv("LINESS"); + int res = (var != NULL) ? atoi(var) : 25; + if (res < 1) res = 1; + return res; +} + + /* Print formatted error, warning and information messages * TODO: Implement th_vfprintf() and friends? */ diff -r 51eec969b07a -r fdea1c3acc53 th_util.h --- a/th_util.h Thu Jan 01 09:22:08 2015 +0200 +++ b/th_util.h Thu Jan 01 09:22:27 2015 +0200 @@ -120,6 +120,9 @@ char *author, char *license); void th_print_banner(FILE *outFile, const char *binName, const char *usage); +int th_term_width(); +int th_term_height(); + int th_get_error(); int th_errno_to_error(int error); const char *th_error_str(int error);