Skip to content

Commit 7ef705e

Browse files
committed
Merge branch 'js/log-rewrap'
* js/log-rewrap: Teach --wrap to only indent without wrapping Add strbuf_add_wrapped_text() to utf8.[ch] print_wrapped_text(): allow hard newlines
2 parents 4d8c325 + 00d3947 commit 7ef705e

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

utf8.c

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "git-compat-util.h"
2+
#include "strbuf.h"
23
#include "utf8.h"
34

45
/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
@@ -279,14 +280,22 @@ int is_utf8(const char *text)
279280
return 1;
280281
}
281282

282-
static void print_spaces(int count)
283+
static inline void strbuf_write(struct strbuf *sb, const char *buf, int len)
284+
{
285+
if (sb)
286+
strbuf_insert(sb, sb->len, buf, len);
287+
else
288+
fwrite(buf, len, 1, stdout);
289+
}
290+
291+
static void print_spaces(struct strbuf *buf, int count)
283292
{
284293
static const char s[] = " ";
285294
while (count >= sizeof(s)) {
286-
fwrite(s, sizeof(s) - 1, 1, stdout);
295+
strbuf_write(buf, s, sizeof(s) - 1);
287296
count -= sizeof(s) - 1;
288297
}
289-
fwrite(s, count, 1, stdout);
298+
strbuf_write(buf, s, count);
290299
}
291300

292301
/*
@@ -295,11 +304,25 @@ static void print_spaces(int count)
295304
* If indent is negative, assume that already -indent columns have been
296305
* consumed (and no extra indent is necessary for the first line).
297306
*/
298-
int print_wrapped_text(const char *text, int indent, int indent2, int width)
307+
int strbuf_add_wrapped_text(struct strbuf *buf,
308+
const char *text, int indent, int indent2, int width)
299309
{
300310
int w = indent, assume_utf8 = is_utf8(text);
301311
const char *bol = text, *space = NULL;
302312

313+
if (width <= 0) {
314+
/* just indent */
315+
while (*text) {
316+
const char *eol = strchrnul(text, '\n');
317+
if (*eol == '\n')
318+
eol++;
319+
print_spaces(buf, indent);
320+
strbuf_write(buf, text, eol-text);
321+
text = eol;
322+
}
323+
return 1;
324+
}
325+
303326
if (indent < 0) {
304327
w = -indent;
305328
space = text;
@@ -310,21 +333,35 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
310333
if (!c || isspace(c)) {
311334
if (w < width || !space) {
312335
const char *start = bol;
336+
if (!c && text == start)
337+
return w;
313338
if (space)
314339
start = space;
315340
else
316-
print_spaces(indent);
317-
fwrite(start, text - start, 1, stdout);
341+
print_spaces(buf, indent);
342+
strbuf_write(buf, start, text - start);
318343
if (!c)
319344
return w;
320-
else if (c == '\t')
321-
w |= 0x07;
322345
space = text;
346+
if (c == '\t')
347+
w |= 0x07;
348+
else if (c == '\n') {
349+
space++;
350+
if (*space == '\n') {
351+
strbuf_write(buf, "\n", 1);
352+
goto new_line;
353+
}
354+
else if (!isalnum(*space))
355+
goto new_line;
356+
else
357+
strbuf_write(buf, " ", 1);
358+
}
323359
w++;
324360
text++;
325361
}
326362
else {
327-
putchar('\n');
363+
new_line:
364+
strbuf_write(buf, "\n", 1);
328365
text = bol = space + isspace(*space);
329366
space = NULL;
330367
w = indent = indent2;
@@ -340,6 +377,11 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
340377
}
341378
}
342379

380+
int print_wrapped_text(const char *text, int indent, int indent2, int width)
381+
{
382+
return strbuf_add_wrapped_text(NULL, text, indent, indent2, width);
383+
}
384+
343385
int is_encoding_utf8(const char *name)
344386
{
345387
if (!name)

utf8.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ int is_utf8(const char *text);
1010
int is_encoding_utf8(const char *name);
1111

1212
int print_wrapped_text(const char *text, int indent, int indent2, int len);
13+
int strbuf_add_wrapped_text(struct strbuf *buf,
14+
const char *text, int indent, int indent2, int width);
1315

1416
#ifndef NO_ICONV
1517
char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);

0 commit comments

Comments
 (0)