diff options
| -rw-r--r-- | clectio.c | 50 | ||||
| -rw-r--r-- | config.def.h | 3 |
2 files changed, 41 insertions, 12 deletions
@@ -98,17 +98,42 @@ static int termwidth(void) { return 72; } -/* Print s wrapped at w columns, breaking on spaces (greedy). w <= 0 prints s - * unwrapped. UTF-8 aware: continuation bytes (0x80-0xBF) do not count as a - * column, so multibyte letters measure as one. */ +/* Print one verse: a leading "chap:verse" number in a fixed left column (VNUM + * wide), then the text wrapped at w columns, breaking on spaces (greedy), with + * continuation lines hang-indented under the text so its left edge stays + * straight. w <= 0 prints the text on one line after the number column. UTF-8 + * aware: continuation bytes (0x80-0xBF) do not count as a column, so multibyte + * letters measure as one. A verse that does not begin with a digit gets no + * number column (indent 0) and wraps flush-left, as before. */ static void putwrapped(const char *s, int w) { + const char *text = s; + int indent = 0, first = 1; + + if (*s >= '0' && *s <= '9') { /* peel the leading "chap:verse " token */ + const char *sp = strchr(s, ' '); + if (sp) { + int numlen = (int)(sp - s), pad = VNUM - numlen; + fwrite(s, 1, (size_t)numlen, stdout); + if (pad < 1) + pad = 1; /* always a space, even after a long number */ + while (pad-- > 0) + putchar(' '); + indent = VNUM; + text = sp + 1; + } + } + if (w <= 0) { - printf("%s\n", s); + printf("%s\n", text); return; } - while (*s) { - const char *p = s, *brk = NULL; - int col = 0; + + while (*text) { + const char *p = text, *brk = NULL; + int col = indent, k; + if (!first) /* hang-indent continuation lines under the text */ + for (k = 0; k < indent; k++) + putchar(' '); while (*p && col < w) { if (*p == ' ') brk = p; @@ -116,16 +141,17 @@ static void putwrapped(const char *s, int w) { col++; p++; } - if (!*p) { /* the rest fits on one line */ - printf("%s\n", s); + if (!*p) { /* the rest fits on this line */ + printf("%s\n", text); return; } - if (!brk) /* a single word longer than w: hard-break at the column */ + if (!brk) /* a single word longer than the line: hard-break */ brk = p; - fwrite(s, 1, (size_t)(brk - s), stdout); + fwrite(text, 1, (size_t)(brk - text), stdout); putchar('\n'); - for (s = brk; *s == ' '; s++) /* skip the space(s) we broke on */ + for (text = brk; *text == ' '; text++) /* skip the break space(s) */ ; + first = 0; } } diff --git a/config.def.h b/config.def.h index 53f3378..25d1fe0 100644 --- a/config.def.h +++ b/config.def.h @@ -18,6 +18,9 @@ /* --- Wrap reading text at a column width. --------------------------------- */ #define WRAP 0 /* 0 = fit the terminal (no wrap when piped); N = fixed cols */ +/* --- Verse-number column. ------------------------------------------------- */ +#define VNUM 8 /* width of the left "chap:verse" column; text hangs under it */ + /* ------------------------- derived (do not edit) ------------------------- */ #if GOSPEL_ONLY # define READMODE "Gospel only" |
