From ae96f13797dfaf73accfc842dd9978970003b507 Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Thu, 28 Feb 2019 21:25:19 +0100 Subject: [PATCH] Apply -q to box creation --- src/boxes.c | 1 + src/boxes.h.in | 1 + src/generate.c | 70 +++++++++++++------ test/085_q_option_center.txt | 13 ++++ test/086_q_option_right.txt | 13 ++++ test/087_q_option_left.txt | 13 ++++ test/088_q_option_center_padded.txt | 14 ++++ test/089_q_option_center_padded_indented.txt | 19 +++++ ...q_option_center_padded_indented_bigbox.txt | 25 +++++++ test/091_q_option_left_tabindent.txt | 13 ++++ test/092_q_option_blanklines.txt | 21 ++++++ test/093_q_option_blanklines_left.txt | 21 ++++++ test/094_q_option_emptyboxside.txt | 13 ++++ 13 files changed, 217 insertions(+), 20 deletions(-) create mode 100644 test/085_q_option_center.txt create mode 100644 test/086_q_option_right.txt create mode 100644 test/087_q_option_left.txt create mode 100644 test/088_q_option_center_padded.txt create mode 100644 test/089_q_option_center_padded_indented.txt create mode 100644 test/090_q_option_center_padded_indented_bigbox.txt create mode 100644 test/091_q_option_left_tabindent.txt create mode 100644 test/092_q_option_blanklines.txt create mode 100644 test/093_q_option_blanklines_left.txt create mode 100644 test/094_q_option_emptyboxside.txt diff --git a/src/boxes.c b/src/boxes.c index d1e969e..8de84f3 100644 --- a/src/boxes.c +++ b/src/boxes.c @@ -1397,6 +1397,7 @@ static int read_all_input (const int use_stdin) } input.lines[input.anz_lines].len = strlen (buf); + input.lines[input.anz_lines].num_leading_blanks = 0; input.final_newline = has_linebreak(buf, input.lines[input.anz_lines].len); if (opt.r) { diff --git a/src/boxes.h.in b/src/boxes.h.in index 96edea4..d28df95 100644 --- a/src/boxes.h.in +++ b/src/boxes.h.in @@ -148,6 +148,7 @@ typedef struct { char *text; /* line content, tabs expanded */ size_t *tabpos; /* tab positions in expanded work strings */ size_t tabpos_len; /* number of tabs in a line */ + size_t num_leading_blanks; /* number of spaces at the start of the line after justification */ } line_t; #ifndef FILE_LEXER_L diff --git a/src/generate.c b/src/generate.c index c2c06ee..fb52d75 100644 --- a/src/generate.c +++ b/src/generate.c @@ -25,6 +25,7 @@ #include "config.h" #include #include +#include #include #include "shape.h" #include "boxes.h" @@ -692,10 +693,13 @@ static int justify_line (line_t *line, int skew) size_t shift; size_t oldlen = line->len; - if (opt.justify == '\0') + if (empty_line(line)) { + line->num_leading_blanks = SIZE_MAX; return 0; - if (empty_line(line)) + } + if (opt.justify == '\0') { return 0; + } for (p=line->text; *p==' ' || *p=='\t'; ++p); newlen = line->len - (p-line->text); @@ -706,10 +710,12 @@ static int justify_line (line_t *line, int skew) if (opt.design->indentmode == 't') { memmove (line->text+input.indent, p, newlen+1); line->len = newlen + input.indent; + line->num_leading_blanks = input.indent; } else { memmove (line->text, p, newlen+1); line->len = newlen; + line->num_leading_blanks = 0; } break; @@ -750,6 +756,7 @@ static int justify_line (line_t *line, int skew) BFREE (line->text); line->text = newtext; line->len = shift + newlen; + line->num_leading_blanks = shift; break; case 'r': @@ -774,6 +781,7 @@ static int justify_line (line_t *line, int skew) BFREE (line->text); line->text = newtext; line->len = input.maxline; + line->num_leading_blanks = shift; break; default: @@ -811,24 +819,25 @@ int output_box (const sentry_t *thebox) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ { - size_t j; - size_t nol = thebox[BRIG].height; /* number of output lines */ - char trailspc[LINE_MAX+1]; - char *indentspc; - int indentspclen; - size_t vfill, vfill1, vfill2; /* empty lines/columns in box */ - size_t hfill; - char *hfill1, *hfill2; /* space before/after text */ - size_t hpl, hpr; - size_t r; - int rc; - char obuf[LINE_MAX+1]; /* final output buffer */ - size_t obuf_len; /* length of content of obuf */ - size_t skip_start; /* lines to skip for box top */ - size_t skip_end; /* lines to skip for box bottom */ - size_t skip_left; /* true if left box part is to be skipped */ - int ntabs, nspcs; /* needed for unexpand of tabs */ - char *restored_indent; + size_t j; + size_t nol = thebox[BRIG].height; /* number of output lines */ + char trailspc[LINE_MAX+1]; + char *indentspc; + int indentspclen; + size_t vfill, vfill1, vfill1_save, vfill2; /* empty lines/columns in box */ + size_t hfill; + char *hfill1, *hfill2; /* space before/after text */ + size_t hpl, hpr; + size_t r; + int rc; + char obuf[LINE_MAX+1]; /* final output buffer */ + size_t obuf_len; /* length of content of obuf */ + size_t skip_start; /* lines to skip for box top */ + size_t skip_end; /* lines to skip for box bottom */ + size_t skip_left; /* true if left box part is to be skipped */ + int ntabs, nspcs; /* needed for unexpand of tabs */ + char *restored_indent; + size_t *contentPos; /* column of first char of input text in output text */ #ifdef DEBUG fprintf (stderr, "Padding used: left %d, top %d, right %d, bottom %d\n", @@ -900,6 +909,7 @@ int output_box (const sentry_t *thebox) vfill1 += opt.design->padding[BTOP]; vfill2 += opt.design->padding[BBOT]; vfill += opt.design->padding[BTOP] + opt.design->padding[BBOT]; + vfill1_save = vfill1; /* * Provide strings for horizontal text alignment. @@ -975,6 +985,7 @@ int output_box (const sentry_t *thebox) /* * Generate actual output */ + contentPos = calloc (input.anz_lines, sizeof(size_t)); for (j=skip_start; j= 0? input.lines[ti].text : "", hfill2, @@ -1048,6 +1067,17 @@ int output_box (const sentry_t *thebox) fprintf (opt.outfile, "%s%s", obuf, (input.final_newline || j < nol-skip_end-1 ? "\n" : "")); } + /* add info line for web ui if requested with -q */ + if (opt.q) { + fprintf (opt.outfile, "%d ", thebox[BTOP].height + vfill1_save - skip_start); + for (j = 0; j < input.anz_lines; j++) { + fprintf (opt.outfile, "%d%s", + (contentPos[j] == SIZE_MAX ? (int) -1 : (int) contentPos[j]), + j < input.anz_lines-1 ? " ": "\n"); + } + } + + BFREE (contentPos); BFREE (indentspc); BFREE (hfill1); BFREE (hfill2); diff --git a/test/085_q_option_center.txt b/test/085_q_option_center.txt new file mode 100644 index 0000000..a2f61ad --- /dev/null +++ b/test/085_q_option_center.txt @@ -0,0 +1,13 @@ +:ARGS +-ac -q -s 12 +:INPUT +foo +barf +:OUTPUT-FILTER +:EXPECTED +/**********/ +/* foo */ +/* barf */ +/**********/ +1 4 4 +:EOF diff --git a/test/086_q_option_right.txt b/test/086_q_option_right.txt new file mode 100644 index 0000000..76f94e9 --- /dev/null +++ b/test/086_q_option_right.txt @@ -0,0 +1,13 @@ +:ARGS +-ar -q -s 12 +:INPUT +foo +barf +:OUTPUT-FILTER +:EXPECTED +/**********/ +/* foo */ +/* barf */ +/**********/ +1 6 5 +:EOF diff --git a/test/087_q_option_left.txt b/test/087_q_option_left.txt new file mode 100644 index 0000000..e3a899a --- /dev/null +++ b/test/087_q_option_left.txt @@ -0,0 +1,13 @@ +:ARGS +-q -s 12 +:INPUT +foo +barf +:OUTPUT-FILTER +:EXPECTED +/**********/ +/* foo */ +/* barf */ +/**********/ +1 3 3 +:EOF diff --git a/test/088_q_option_center_padded.txt b/test/088_q_option_center_padded.txt new file mode 100644 index 0000000..6659337 --- /dev/null +++ b/test/088_q_option_center_padded.txt @@ -0,0 +1,14 @@ +:ARGS +-ac -q -s 18x5 -p h3 +:INPUT +foo +barf +:OUTPUT-FILTER +:EXPECTED +/****************/ +/* foo */ +/* barf */ +/* */ +/****************/ +1 7 7 +:EOF diff --git a/test/089_q_option_center_padded_indented.txt b/test/089_q_option_center_padded_indented.txt new file mode 100644 index 0000000..fb790a7 --- /dev/null +++ b/test/089_q_option_center_padded_indented.txt @@ -0,0 +1,19 @@ +:ARGS +-ac -q -s 18 -p a3 -i box +:INPUT + foo + barf +:OUTPUT-FILTER +:EXPECTED + /****************/ + /* */ + /* */ + /* */ + /* foo */ + /* barf */ + /* */ + /* */ + /* */ + /****************/ +4 9 9 +:EOF diff --git a/test/090_q_option_center_padded_indented_bigbox.txt b/test/090_q_option_center_padded_indented_bigbox.txt new file mode 100644 index 0000000..2490902 --- /dev/null +++ b/test/090_q_option_center_padded_indented_bigbox.txt @@ -0,0 +1,25 @@ +:ARGS +-ac -q -p a3 -d diamonds -i text +:INPUT + foo + barf +:OUTPUT-FILTER +:EXPECTED + /\ /\ /\ + /\//\\/\ /\//\\/\ /\//\\/\ + /\//\\\///\\/\//\\\///\\/\//\\\///\\/\ +//\\\//\/\\///\\\//\/\\///\\\//\/\\///\\ +\\//\/ \/\\// + \/ \/ + /\ /\ +//\\ foo //\\ +\\// barf \\// + \/ \/ + /\ /\ +//\\/\ /\//\\ +\\///\\/\//\\\///\\/\//\\\///\\/\//\\\// + \/\\///\\\//\/\\///\\\//\/\\///\\\//\/ + \/\\//\/ \/\\//\/ \/\\//\/ + \/ \/ \/ +7 20 20 +:EOF diff --git a/test/091_q_option_left_tabindent.txt b/test/091_q_option_left_tabindent.txt new file mode 100644 index 0000000..d09964c --- /dev/null +++ b/test/091_q_option_left_tabindent.txt @@ -0,0 +1,13 @@ +:ARGS +-q -t 8k +:INPUT + foo + barf +:OUTPUT-FILTER +:EXPECTED + /********/ + /* foo */ + /* barf */ + /********/ +1 4 4 +:EOF diff --git a/test/092_q_option_blanklines.txt b/test/092_q_option_blanklines.txt new file mode 100644 index 0000000..3a8161c --- /dev/null +++ b/test/092_q_option_blanklines.txt @@ -0,0 +1,21 @@ +:ARGS +-q -s 12 +:INPUT +f +fo + +foo +fooo + +:OUTPUT-FILTER +:EXPECTED +/**********/ +/* f */ +/* fo */ +/* */ +/* foo */ +/* fooo */ +/* */ +/**********/ +1 3 3 -1 3 3 -1 +:EOF diff --git a/test/093_q_option_blanklines_left.txt b/test/093_q_option_blanklines_left.txt new file mode 100644 index 0000000..e2207d8 --- /dev/null +++ b/test/093_q_option_blanklines_left.txt @@ -0,0 +1,21 @@ +:ARGS +-q -s 12 -al +:INPUT +f +fo + +foo +fooo + +:OUTPUT-FILTER +:EXPECTED +/**********/ +/* f */ +/* fo */ +/* */ +/* foo */ +/* fooo */ +/* */ +/**********/ +1 3 3 -1 3 3 -1 +:EOF diff --git a/test/094_q_option_emptyboxside.txt b/test/094_q_option_emptyboxside.txt new file mode 100644 index 0000000..1156247 --- /dev/null +++ b/test/094_q_option_emptyboxside.txt @@ -0,0 +1,13 @@ +:ARGS +-q -d right +:INPUT +foo + + bar +:OUTPUT-FILTER +:EXPECTED +foo /*@TJ@*/ + /*@TJ@*/ + bar /*@TJ@*/ +0 0 -1 0 +:EOF