aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2009-11-15 16:41:07 -0800
committerJunio C Hamano <gitster@pobox.com>2009-11-15 16:41:07 -0800
commit7ef705eff6afbc676ab2999dbcdb1a7c8fbf62e6 (patch)
treea48191ab3276365dccbb9d629eeb102c2257230f
parent4d8c3258880548510b1d23f0db517adb7dfd2486 (diff)
parent00d3947366a50a06da40989a1fd1e3f99885a4c3 (diff)
downloadgit-7ef705eff6afbc676ab2999dbcdb1a7c8fbf62e6.tar.gz
git-7ef705eff6afbc676ab2999dbcdb1a7c8fbf62e6.tar.xz
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
-rw-r--r--utf8.c60
-rw-r--r--utf8.h2
2 files changed, 53 insertions, 9 deletions
diff --git a/utf8.c b/utf8.c
index db706ac4e..5c18f0c28 100644
--- a/utf8.c
+++ b/utf8.c
@@ -1,4 +1,5 @@
#include "git-compat-util.h"
+#include "strbuf.h"
#include "utf8.h"
/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
@@ -279,14 +280,22 @@ int is_utf8(const char *text)
return 1;
}
-static void print_spaces(int count)
+static inline void strbuf_write(struct strbuf *sb, const char *buf, int len)
+{
+ if (sb)
+ strbuf_insert(sb, sb->len, buf, len);
+ else
+ fwrite(buf, len, 1, stdout);
+}
+
+static void print_spaces(struct strbuf *buf, int count)
{
static const char s[] = " ";
while (count >= sizeof(s)) {
- fwrite(s, sizeof(s) - 1, 1, stdout);
+ strbuf_write(buf, s, sizeof(s) - 1);
count -= sizeof(s) - 1;
}
- fwrite(s, count, 1, stdout);
+ strbuf_write(buf, s, count);
}
/*
@@ -295,11 +304,25 @@ static void print_spaces(int count)
* If indent is negative, assume that already -indent columns have been
* consumed (and no extra indent is necessary for the first line).
*/
-int print_wrapped_text(const char *text, int indent, int indent2, int width)
+int strbuf_add_wrapped_text(struct strbuf *buf,
+ const char *text, int indent, int indent2, int width)
{
int w = indent, assume_utf8 = is_utf8(text);
const char *bol = text, *space = NULL;
+ if (width <= 0) {
+ /* just indent */
+ while (*text) {
+ const char *eol = strchrnul(text, '\n');
+ if (*eol == '\n')
+ eol++;
+ print_spaces(buf, indent);
+ strbuf_write(buf, text, eol-text);
+ text = eol;
+ }
+ return 1;
+ }
+
if (indent < 0) {
w = -indent;
space = text;
@@ -310,21 +333,35 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
if (!c || isspace(c)) {
if (w < width || !space) {
const char *start = bol;
+ if (!c && text == start)
+ return w;
if (space)
start = space;
else
- print_spaces(indent);
- fwrite(start, text - start, 1, stdout);
+ print_spaces(buf, indent);
+ strbuf_write(buf, start, text - start);
if (!c)
return w;
- else if (c == '\t')
- w |= 0x07;
space = text;
+ if (c == '\t')
+ w |= 0x07;
+ else if (c == '\n') {
+ space++;
+ if (*space == '\n') {
+ strbuf_write(buf, "\n", 1);
+ goto new_line;
+ }
+ else if (!isalnum(*space))
+ goto new_line;
+ else
+ strbuf_write(buf, " ", 1);
+ }
w++;
text++;
}
else {
- putchar('\n');
+new_line:
+ strbuf_write(buf, "\n", 1);
text = bol = space + isspace(*space);
space = NULL;
w = indent = indent2;
@@ -340,6 +377,11 @@ int print_wrapped_text(const char *text, int indent, int indent2, int width)
}
}
+int print_wrapped_text(const char *text, int indent, int indent2, int width)
+{
+ return strbuf_add_wrapped_text(NULL, text, indent, indent2, width);
+}
+
int is_encoding_utf8(const char *name)
{
if (!name)
diff --git a/utf8.h b/utf8.h
index 2f1b14ff4..ae30ae4c6 100644
--- a/utf8.h
+++ b/utf8.h
@@ -10,6 +10,8 @@ int is_utf8(const char *text);
int is_encoding_utf8(const char *name);
int print_wrapped_text(const char *text, int indent, int indent2, int len);
+int strbuf_add_wrapped_text(struct strbuf *buf,
+ const char *text, int indent, int indent2, int width);
#ifndef NO_ICONV
char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding);