2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "telescope.h"
26 * There's a simple text wrapping algorithm.
28 * 1. if it's a line in a pre-formatted block:
31 * 2. there is enough room for the next word?
33 * b. no: break the current line.
34 * i. while there isn't enough space to draw the current
36 * ii. draw the remainder of the current word (can be the
38 * 3. render the spaces after the word
39 * a. but if there is not enough room break the line and
46 push_line(struct tab *tab, const struct line *l, const char *buf, size_t len, int cont)
52 if ((vl = calloc(1, sizeof(*vl))) == NULL)
55 if (len != 0 && (vl->line = calloc(1, len+1)) == NULL) {
62 memcpy(vl->line, buf, len);
65 if (TAILQ_EMPTY(&tab->s.head))
66 TAILQ_INSERT_HEAD(&tab->s.head, vl, vlines);
68 TAILQ_INSERT_TAIL(&tab->s.head, vl, vlines);
73 * Helper function for wrap_text. Find the end of the current word
74 * and the end of the separator after the word.
77 word_boundaries(const char *s, const char *sep, const char **endword, const char **endspc)
85 /* find the end of the current world */
86 for (; *s != '\0'; ++s) {
87 if (strchr(sep, *s) != NULL)
93 /* find the end of the separator */
94 for (; *s != '\0'; ++s) {
95 if (strchr(sep, *s) == NULL)
105 emitline(struct tab *tab, size_t zero, size_t *off, const struct line *l,
106 const char **line, int *cont)
108 if (!push_line(tab, l, *line, *off - zero, *cont))
112 *line += *off - zero;
118 * Build a list of visual line by wrapping the given line, assuming
119 * that when printed will have a leading prefix prfx.
121 * TODO: it considers each byte one cell on the screen!
124 wrap_text(struct tab *tab, const char *prfx, struct line *l, size_t width)
126 size_t zero, off, len;
128 const char *endword, *endspc, *line, *linestart;
136 push_line(tab, l, NULL, 0, 0);
140 while (word_boundaries(line, " \t-", &endword, &endspc)) {
141 len = endword - line;
142 if (off + len >= width) {
143 emitline(tab, zero, &off, l, &linestart, &cont);
144 while (len >= width) {
146 emitline(tab, zero, &off, l, &linestart, &cont);
156 /* print the spaces iff not at bol */
157 len = endspc - endword;
160 if (off + len >= width) {
161 emitline(tab, zero, &off, l, &linestart, &cont);
170 emitline(tab, zero, &off, l, &linestart, &cont);
174 hardwrap_text(struct tab *tab, struct line *l, size_t width)
178 const char *linestart;
184 return emitline(tab, 0, &off, l, &linestart, &cont);
186 len = strlen(l->line);
188 while (len >= width) {
191 if (!emitline(tab, 0, &off, l, &linestart, &cont))
196 return emitline(tab, 0, &len, l, &linestart, &cont);