Skip to content

Commit 094e03b

Browse files
dschoJunio C Hamano
authored andcommitted
Actually make print_wrapped_text() useful
Now, it returns the current column, does not add a newline, and you can pass a negative indent, to indicate that the indent was already printed. With this, you can actually continue in the middle of a paragraph, not having to print everything into a buffer first. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e52a5de commit 094e03b

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

utf8.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,19 @@ static void print_spaces(int count)
235235
/*
236236
* Wrap the text, if necessary. The variable indent is the indent for the
237237
* first line, indent2 is the indent for all other lines.
238+
* If indent is negative, assume that already -indent columns have been
239+
* consumed (and no extra indent is necessary for the first line).
238240
*/
239-
void print_wrapped_text(const char *text, int indent, int indent2, int width)
241+
int print_wrapped_text(const char *text, int indent, int indent2, int width)
240242
{
241243
int w = indent, assume_utf8 = is_utf8(text);
242244
const char *bol = text, *space = NULL;
243245

246+
if (indent < 0) {
247+
w = -indent;
248+
space = text;
249+
}
250+
244251
for (;;) {
245252
char c = *text;
246253
if (!c || isspace(c)) {
@@ -251,10 +258,9 @@ void print_wrapped_text(const char *text, int indent, int indent2, int width)
251258
else
252259
print_spaces(indent);
253260
fwrite(start, text - start, 1, stdout);
254-
if (!c) {
255-
putchar('\n');
256-
return;
257-
} else if (c == '\t')
261+
if (!c)
262+
return w;
263+
else if (c == '\t')
258264
w |= 0x07;
259265
space = text;
260266
w++;
@@ -275,6 +281,7 @@ void print_wrapped_text(const char *text, int indent, int indent2, int width)
275281
text++;
276282
}
277283
}
284+
return w;
278285
}
279286

280287
int is_encoding_utf8(const char *name)

utf8.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ int utf8_width(const char **start);
55
int is_utf8(const char *text);
66
int is_encoding_utf8(const char *name);
77

8-
void print_wrapped_text(const char *text, int indent, int indent2, int len);
8+
int print_wrapped_text(const char *text, int indent, int indent2, int len);
99

1010
#ifndef NO_ICONV
1111
char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);

0 commit comments

Comments
 (0)