Skip to content

Commit

Permalink
Specify padding/margin for -tterm.
Browse files Browse the repository at this point in the history
This adds initial support (which will be refactored in subsequent
commits) for specifying both --term-hmargin and --term-hpadding.
The former will move the content to the right; the latter will pad
into the content width toward the right.

In doing so, make the default body padding to be zero, which allows
the users to control finely for horizontal indent.

Fixes #129
  • Loading branch information
kristapsdz committed Dec 21, 2024
1 parent 53045c4 commit c1fbd4a
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 8 deletions.
1 change: 1 addition & 0 deletions lowdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ struct lowdown_opts {
size_t maxdepth;
size_t cols;
size_t hmargin;
size_t hpadding;
size_t vmargin;
unsigned int feat;
#define LOWDOWN_ATTRS 0x80000
Expand Down
8 changes: 8 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ main(int argc, char *argv[])

{ "term-columns", required_argument, NULL, 4 },
{ "term-hmargin", required_argument, NULL, 2 },
{ "term-hpadding", required_argument, NULL, 9 },
{ "term-vmargin", required_argument, NULL, 3 },
{ "term-width", required_argument, NULL, 1 },

Expand Down Expand Up @@ -426,6 +427,7 @@ main(int argc, char *argv[])
TAILQ_INIT(&mq);
memset(&opts, 0, sizeof(struct lowdown_opts));

opts.hpadding = 4;
opts.maxdepth = 128;
opts.type = LOWDOWN_HTML;
opts.feat =
Expand Down Expand Up @@ -571,6 +573,12 @@ main(int argc, char *argv[])
case 8:
templfn = optarg;
break;
case 9:
opts.hpadding = strtonum
(optarg, 0, INT_MAX, &er);
if (er == NULL)
break;
errx(1, "--term-hpadding: %s", er);
default:
goto usage;
}
Expand Down
12 changes: 6 additions & 6 deletions regress/table-footnotes.term
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
This used to crash -Tterm because the footnote size was saved and
zeroed prior to table column-width scanning, then restored. However,
this could result in off-by-ones or -twos because the actual
footnotes may have gone double-digit while the zeroed wouldn’t.

zeroed prior to table column-width scanning, then restored.
However, this could result in off-by-ones or -twos because the
actual footnotes may have gone double-digit while the zeroed wouldn’t.
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8]

Officer | Rank
------------------|----------------------
Jean-Luc Picard | Captain
Worf[9] | Lieutenant Commander
Data[10] | Lieutenant Commander
William Riker[11] | Commander

 ~~~~~~~~
~~~~~~~~

 1. foo
 2. foo
Expand Down
14 changes: 14 additions & 0 deletions regress/term-width.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***testttt***
***test***
2 changes: 2 additions & 0 deletions regress/term-width.term
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test test test test test test test test test test test test testttt
test
14 changes: 14 additions & 0 deletions regress/term-width2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***testtttt***
***test***
2 changes: 2 additions & 0 deletions regress/term-width2.term
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test test test test test test test test test test test test 
testtttt test
12 changes: 12 additions & 0 deletions regress/term-width3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test***
***test*** ***testttt***
1 change: 1 addition & 0 deletions regress/term-width3.term
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test test test test test test test test test test test test testttt
14 changes: 13 additions & 1 deletion term.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct term {
size_t stackpos; /* position in stack */
size_t maxcol; /* soft limit */
size_t hmargin; /* left of content */
size_t hpadding; /* left of content */
size_t vmargin; /* before/after content */
struct lowdown_buf *tmp; /* for temporary allocations */
wchar_t *buf; /* buffer for counting wchar */
Expand Down Expand Up @@ -619,6 +620,9 @@ rndr_buf_startline_prefixes(struct term *term,
for (i = 0; i < term->hmargin; i++)
if (!HBUF_PUTSL(out, " "))
return 0;
for (i = 0; i < term->hpadding; i++)
if (!HBUF_PUTSL(out, " "))
return 0;
break;
case LOWDOWN_BLOCKQUOTE:
rndr_node_style_apply(&sinner, &sty_bkqt_pfx);
Expand Down Expand Up @@ -1718,10 +1722,18 @@ lowdown_term_new(const struct lowdown_opts *opts)
if (opts != NULL) {
st->maxcol = opts->cols == 0 ? 80 : opts->cols;
st->hmargin = opts->hmargin;
st->hpadding = opts->hpadding;
st->vmargin = opts->vmargin;
st->opts = opts->oflags;
} else
} else {
st->maxcol = 80;
st->hpadding = 4;
}

if (st->hpadding >= st->maxcol)
st->maxcol = 1;
else
st->maxcol -= st->hpadding;

if ((st->tmp = hbuf_new(32)) == NULL) {
free(st);
Expand Down
2 changes: 1 addition & 1 deletion term.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ static const struct sty sty_table = { 0, 0, 0, 0, 0, 93, 0 };
*/

/* All non-header (child to the document root) content (no style). */
static const struct pfx pfx_body = { " ", 4 };
static const struct pfx pfx_body = { "", 0 };

/* All header (child to the document root) content (no style). */
static const struct pfx pfx_header = { "", 0 };
Expand Down

0 comments on commit c1fbd4a

Please sign in to comment.