{"thread":{"id":"21704","subject":"[PATCH] strbuf_add_wrapped_text(): factor out strbuf_add_indented_text()","startedAt":"2009-11-22T16:15:29Z","lastAt":"2009-11-22T16:15:29Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"128107","messageId":"4B0963A1.1010900@lsrfire.ath.cx","threadId":"21704","inReplyTo":null,"subject":"[PATCH] strbuf_add_wrapped_text(): factor out strbuf_add_indented_text()","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2009-11-22T16:15:29Z","receivedAt":"2009-11-22T16:15:29Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Add a new helper function, strbuf_add_indented_text(), to indent text\nwithout a width limit, and call it from strbuf_add_wrapped_text().  It\nrespects both indent (applied to the first line) and indent2 (applied to\nthe rest of the lines); indent2 was ignored by the indent-only path of\nstrbuf_add_wrapped_text() before the patch.\n\nTwo simple test cases are added, one exercising strbuf_add_wrapped_text()\nand the other strbuf_add_indented_text().\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n t/t4202-log.sh |   21 +++++++++++++++++++++\n utf8.c         |   26 +++++++++++++++++---------\n 2 files changed, 38 insertions(+), 9 deletions(-)\n\ndiff --git a/t/t4202-log.sh b/t/t4202-log.sh\nindex 1e952ca..779a5ad 100755\n--- a/t/t4202-log.sh\n+++ b/t/t4202-log.sh\n@@ -64,6 +64,27 @@ test_expect_success 'format' '\n '\n \n cat > expect << EOF\n+ This is\n+  the sixth\n+  commit.\n+ This is\n+  the fifth\n+  commit.\n+EOF\n+\n+test_expect_success 'format %w(12,1,2)' '\n+\n+\tgit log -2 --format=\"%w(12,1,2)This is the %s commit.\" > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'format %w(,1,2)' '\n+\n+\tgit log -2 --format=\"%w(,1,2)This is%nthe %s%ncommit.\" > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+cat > expect << EOF\n 804a787 sixth\n 394ef78 fifth\n 5d31159 fourth\ndiff --git a/utf8.c b/utf8.c\nindex 5c18f0c..01d1869 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -298,6 +298,22 @@ static void print_spaces(struct strbuf *buf, int count)\n \tstrbuf_write(buf, s, count);\n }\n \n+static void strbuf_add_indented_text(struct strbuf *buf, const char *text,\n+\t\t\t\t     int indent, int indent2)\n+{\n+\tif (indent < 0)\n+\t\tindent = 0;\n+\twhile (*text) {\n+\t\tconst char *eol = strchrnul(text, '\\n');\n+\t\tif (*eol == '\\n')\n+\t\t\teol++;\n+\t\tprint_spaces(buf, indent);\n+\t\tstrbuf_write(buf, text, eol - text);\n+\t\ttext = eol;\n+\t\tindent = indent2;\n+\t}\n+}\n+\n /*\n  * Wrap the text, if necessary. The variable indent is the indent for the\n  * first line, indent2 is the indent for all other lines.\n@@ -311,15 +327,7 @@ int strbuf_add_wrapped_text(struct strbuf *buf,\n \tconst char *bol = text, *space = NULL;\n \n \tif (width <= 0) {\n-\t\t/* just indent */\n-\t\twhile (*text) {\n-\t\t\tconst char *eol = strchrnul(text, '\\n');\n-\t\t\tif (*eol == '\\n')\n-\t\t\t\teol++;\n-\t\t\tprint_spaces(buf, indent);\n-\t\t\tstrbuf_write(buf, text, eol-text);\n-\t\t\ttext = eol;\n-\t\t}\n+\t\tstrbuf_add_indented_text(buf, text, indent, indent2);\n \t\treturn 1;\n \t}\n \n-- \n1.6.5.3\n"}]}