mirror of
https://github.com/ascii-boxes/boxes.git
synced 2025-01-08 06:58:49 +01:00
Apply -q to box creation
This commit is contained in:
parent
5fad2e4c34
commit
ae96f13797
@ -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].len = strlen (buf);
|
||||||
|
input.lines[input.anz_lines].num_leading_blanks = 0;
|
||||||
input.final_newline = has_linebreak(buf, input.lines[input.anz_lines].len);
|
input.final_newline = has_linebreak(buf, input.lines[input.anz_lines].len);
|
||||||
|
|
||||||
if (opt.r) {
|
if (opt.r) {
|
||||||
|
@ -148,6 +148,7 @@ typedef struct {
|
|||||||
char *text; /* line content, tabs expanded */
|
char *text; /* line content, tabs expanded */
|
||||||
size_t *tabpos; /* tab positions in expanded work strings */
|
size_t *tabpos; /* tab positions in expanded work strings */
|
||||||
size_t tabpos_len; /* number of tabs in a line */
|
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;
|
} line_t;
|
||||||
|
|
||||||
#ifndef FILE_LEXER_L
|
#ifndef FILE_LEXER_L
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "shape.h"
|
#include "shape.h"
|
||||||
#include "boxes.h"
|
#include "boxes.h"
|
||||||
@ -692,10 +693,13 @@ static int justify_line (line_t *line, int skew)
|
|||||||
size_t shift;
|
size_t shift;
|
||||||
size_t oldlen = line->len;
|
size_t oldlen = line->len;
|
||||||
|
|
||||||
if (opt.justify == '\0')
|
if (empty_line(line)) {
|
||||||
|
line->num_leading_blanks = SIZE_MAX;
|
||||||
return 0;
|
return 0;
|
||||||
if (empty_line(line))
|
}
|
||||||
|
if (opt.justify == '\0') {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
for (p=line->text; *p==' ' || *p=='\t'; ++p);
|
for (p=line->text; *p==' ' || *p=='\t'; ++p);
|
||||||
newlen = line->len - (p-line->text);
|
newlen = line->len - (p-line->text);
|
||||||
@ -706,10 +710,12 @@ static int justify_line (line_t *line, int skew)
|
|||||||
if (opt.design->indentmode == 't') {
|
if (opt.design->indentmode == 't') {
|
||||||
memmove (line->text+input.indent, p, newlen+1);
|
memmove (line->text+input.indent, p, newlen+1);
|
||||||
line->len = newlen + input.indent;
|
line->len = newlen + input.indent;
|
||||||
|
line->num_leading_blanks = input.indent;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
memmove (line->text, p, newlen+1);
|
memmove (line->text, p, newlen+1);
|
||||||
line->len = newlen;
|
line->len = newlen;
|
||||||
|
line->num_leading_blanks = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -750,6 +756,7 @@ static int justify_line (line_t *line, int skew)
|
|||||||
BFREE (line->text);
|
BFREE (line->text);
|
||||||
line->text = newtext;
|
line->text = newtext;
|
||||||
line->len = shift + newlen;
|
line->len = shift + newlen;
|
||||||
|
line->num_leading_blanks = shift;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
@ -774,6 +781,7 @@ static int justify_line (line_t *line, int skew)
|
|||||||
BFREE (line->text);
|
BFREE (line->text);
|
||||||
line->text = newtext;
|
line->text = newtext;
|
||||||
line->len = input.maxline;
|
line->len = input.maxline;
|
||||||
|
line->num_leading_blanks = shift;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -816,7 +824,7 @@ int output_box (const sentry_t *thebox)
|
|||||||
char trailspc[LINE_MAX+1];
|
char trailspc[LINE_MAX+1];
|
||||||
char *indentspc;
|
char *indentspc;
|
||||||
int indentspclen;
|
int indentspclen;
|
||||||
size_t vfill, vfill1, vfill2; /* empty lines/columns in box */
|
size_t vfill, vfill1, vfill1_save, vfill2; /* empty lines/columns in box */
|
||||||
size_t hfill;
|
size_t hfill;
|
||||||
char *hfill1, *hfill2; /* space before/after text */
|
char *hfill1, *hfill2; /* space before/after text */
|
||||||
size_t hpl, hpr;
|
size_t hpl, hpr;
|
||||||
@ -829,6 +837,7 @@ int output_box (const sentry_t *thebox)
|
|||||||
size_t skip_left; /* true if left box part is to be skipped */
|
size_t skip_left; /* true if left box part is to be skipped */
|
||||||
int ntabs, nspcs; /* needed for unexpand of tabs */
|
int ntabs, nspcs; /* needed for unexpand of tabs */
|
||||||
char *restored_indent;
|
char *restored_indent;
|
||||||
|
size_t *contentPos; /* column of first char of input text in output text */
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
fprintf (stderr, "Padding used: left %d, top %d, right %d, bottom %d\n",
|
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];
|
vfill1 += opt.design->padding[BTOP];
|
||||||
vfill2 += opt.design->padding[BBOT];
|
vfill2 += opt.design->padding[BBOT];
|
||||||
vfill += opt.design->padding[BTOP] + opt.design->padding[BBOT];
|
vfill += opt.design->padding[BTOP] + opt.design->padding[BBOT];
|
||||||
|
vfill1_save = vfill1;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Provide strings for horizontal text alignment.
|
* Provide strings for horizontal text alignment.
|
||||||
@ -975,6 +985,7 @@ int output_box (const sentry_t *thebox)
|
|||||||
/*
|
/*
|
||||||
* Generate actual output
|
* Generate actual output
|
||||||
*/
|
*/
|
||||||
|
contentPos = calloc (input.anz_lines, sizeof(size_t));
|
||||||
for (j=skip_start; j<nol-skip_end; ++j) {
|
for (j=skip_start; j<nol-skip_end; ++j) {
|
||||||
|
|
||||||
if (j < thebox[BTOP].height) { /* box top */
|
if (j < thebox[BTOP].height) { /* box top */
|
||||||
@ -1008,6 +1019,14 @@ int output_box (const sentry_t *thebox)
|
|||||||
r = input.maxline - input.lines[ti].len;
|
r = input.maxline - input.lines[ti].len;
|
||||||
trailspc[r] = '\0';
|
trailspc[r] = '\0';
|
||||||
restored_indent = tabbify_indent (ti, indentspc, indentspclen);
|
restored_indent = tabbify_indent (ti, indentspc, indentspclen);
|
||||||
|
if (input.lines[ti].num_leading_blanks == SIZE_MAX) {
|
||||||
|
contentPos[ti] = SIZE_MAX;
|
||||||
|
} else {
|
||||||
|
contentPos[ti] = strlen(restored_indent)
|
||||||
|
+ (skip_left? 0: strlen(thebox[BLEF].chars[j]))
|
||||||
|
+ strlen(hfill1)
|
||||||
|
+ input.lines[ti].num_leading_blanks;
|
||||||
|
}
|
||||||
concat_strings (obuf, LINE_MAX+1, 7, restored_indent,
|
concat_strings (obuf, LINE_MAX+1, 7, restored_indent,
|
||||||
skip_left?"":thebox[BLEF].chars[j], hfill1,
|
skip_left?"":thebox[BLEF].chars[j], hfill1,
|
||||||
ti >= 0? input.lines[ti].text : "", hfill2,
|
ti >= 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" : ""));
|
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 (indentspc);
|
||||||
BFREE (hfill1);
|
BFREE (hfill1);
|
||||||
BFREE (hfill2);
|
BFREE (hfill2);
|
||||||
|
13
test/085_q_option_center.txt
Normal file
13
test/085_q_option_center.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
:ARGS
|
||||||
|
-ac -q -s 12
|
||||||
|
:INPUT
|
||||||
|
foo
|
||||||
|
barf
|
||||||
|
:OUTPUT-FILTER
|
||||||
|
:EXPECTED
|
||||||
|
/**********/
|
||||||
|
/* foo */
|
||||||
|
/* barf */
|
||||||
|
/**********/
|
||||||
|
1 4 4
|
||||||
|
:EOF
|
13
test/086_q_option_right.txt
Normal file
13
test/086_q_option_right.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
:ARGS
|
||||||
|
-ar -q -s 12
|
||||||
|
:INPUT
|
||||||
|
foo
|
||||||
|
barf
|
||||||
|
:OUTPUT-FILTER
|
||||||
|
:EXPECTED
|
||||||
|
/**********/
|
||||||
|
/* foo */
|
||||||
|
/* barf */
|
||||||
|
/**********/
|
||||||
|
1 6 5
|
||||||
|
:EOF
|
13
test/087_q_option_left.txt
Normal file
13
test/087_q_option_left.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
:ARGS
|
||||||
|
-q -s 12
|
||||||
|
:INPUT
|
||||||
|
foo
|
||||||
|
barf
|
||||||
|
:OUTPUT-FILTER
|
||||||
|
:EXPECTED
|
||||||
|
/**********/
|
||||||
|
/* foo */
|
||||||
|
/* barf */
|
||||||
|
/**********/
|
||||||
|
1 3 3
|
||||||
|
:EOF
|
14
test/088_q_option_center_padded.txt
Normal file
14
test/088_q_option_center_padded.txt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
:ARGS
|
||||||
|
-ac -q -s 18x5 -p h3
|
||||||
|
:INPUT
|
||||||
|
foo
|
||||||
|
barf
|
||||||
|
:OUTPUT-FILTER
|
||||||
|
:EXPECTED
|
||||||
|
/****************/
|
||||||
|
/* foo */
|
||||||
|
/* barf */
|
||||||
|
/* */
|
||||||
|
/****************/
|
||||||
|
1 7 7
|
||||||
|
:EOF
|
19
test/089_q_option_center_padded_indented.txt
Normal file
19
test/089_q_option_center_padded_indented.txt
Normal file
@ -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
|
25
test/090_q_option_center_padded_indented_bigbox.txt
Normal file
25
test/090_q_option_center_padded_indented_bigbox.txt
Normal file
@ -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
|
13
test/091_q_option_left_tabindent.txt
Normal file
13
test/091_q_option_left_tabindent.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
:ARGS
|
||||||
|
-q -t 8k
|
||||||
|
:INPUT
|
||||||
|
foo
|
||||||
|
barf
|
||||||
|
:OUTPUT-FILTER
|
||||||
|
:EXPECTED
|
||||||
|
/********/
|
||||||
|
/* foo */
|
||||||
|
/* barf */
|
||||||
|
/********/
|
||||||
|
1 4 4
|
||||||
|
:EOF
|
21
test/092_q_option_blanklines.txt
Normal file
21
test/092_q_option_blanklines.txt
Normal file
@ -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
|
21
test/093_q_option_blanklines_left.txt
Normal file
21
test/093_q_option_blanklines_left.txt
Normal file
@ -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
|
13
test/094_q_option_emptyboxside.txt
Normal file
13
test/094_q_option_emptyboxside.txt
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
:ARGS
|
||||||
|
-q -d right
|
||||||
|
:INPUT
|
||||||
|
foo
|
||||||
|
|
||||||
|
bar
|
||||||
|
:OUTPUT-FILTER
|
||||||
|
:EXPECTED
|
||||||
|
foo /*@TJ@*/
|
||||||
|
/*@TJ@*/
|
||||||
|
bar /*@TJ@*/
|
||||||
|
0 0 -1 0
|
||||||
|
:EOF
|
Loading…
Reference in New Issue
Block a user