pretty: support %>> that steal trailing spaces
This is pretty useful in `%<(100)%s%Cred%>(20)% an' where %s does not use up all 100 columns and %an needs more than 20 columns. By replacing %>(20) with %>>(20), %an can steal spaces from %s. %>> understands escape sequences, so %Cred does not stop it from stealing spaces in %<(100). Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Apr 19, 2013 at 09:08 UTC
1640632b4f3f69775f04e9e40dfd2fd912e0f458
5 files changed
+54
-2
Documentation/pretty-formats.txt
+4
-1
@@ -173,7 +173,10 @@ The placeholders are:
173
columns, padding spaces on the right if necessary
174
- '%>(<N>)', '%>|(<N>)': similar to '%<(<N>)', '%<|(<N>)'
175
respectively, but padding spaces on the left
176
-- '%><(<N>)', '%><|(<N>)': similar to '%<(<N>)', '%<|(<N>)'
176
+- '%>>(<N>)', '%>>|(<N>)': similar to '%>(<N>)', '%>|(<N>)'
177
+ respectively, except that if the next placeholder takes more spaces
178
+ than given and there are spaces on its left, use those spaces
179
+- '%><(<N>)', '%><|(<N>)': similar to '% <(<N>)', '%<|(<N>)'
180
respectively, but padding both sides (i.e. the text is centered)
181
182
NOTE: Some placeholders may depend on other options given to the
pretty.c
+34
@@ -773,6 +773,7 @@ enum flush_type {
773
no_flush,
774
flush_right,
775
flush_left,
776
+ flush_left_and_steal,
777
flush_both
778
};
779
@@ -1026,6 +1027,9 @@ static size_t parse_padding_placeholder(struct strbuf *sb,
1027
if (*ch == '<') {
1028
flush_type = flush_both;
1029
ch++;
1030
+ } else if (*ch == '>') {
1031
+ flush_type = flush_left_and_steal;
1032
+ ch++;
1033
} else
1034
flush_type = flush_left;
1035
break;
@@ -1334,6 +1338,36 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
1338
total_consumed++;
1339
}
1340
len = utf8_strnwidth(local_sb.buf, -1, 1);
1341
+
1342
+ if (c->flush_type == flush_left_and_steal) {
1343
+ const char *ch = sb->buf + sb->len - 1;
1344
+ while (len > padding && ch > sb->buf) {
1345
+ const char *p;
1346
+ if (*ch == ' ') {
1347
+ ch--;
1348
+ padding++;
1349
+ continue;
1350
+ }
1351
+ /* check for trailing ansi sequences */
1352
+ if (*ch != 'm')
1353
+ break;
1354
+ p = ch - 1;
1355
+ while (ch - p < 10 && *p != '\033')
1356
+ p--;
1357
+ if (*p != '\033' ||
1358
+ ch + 1 - p != display_mode_esc_sequence_len(p))
1359
+ break;
1360
+ /*
1361
+ * got a good ansi sequence, put it back to
1362
+ * local_sb as we're cutting sb
1363
+ */
1364
+ strbuf_insert(&local_sb, 0, p, ch + 1 - p);
1365
+ ch = p - 1;
1366
+ }
1367
+ strbuf_setlen(sb, ch + 1 - sb->buf);
1368
+ c->flush_type = flush_left;
1369
+ }
1370
+
1371
if (len > padding) {
1372
switch (c->truncate) {
1373
case trunc_left:
t/t4205-log-pretty-formats.sh
+14
@@ -260,4 +260,18 @@ EOF
260
test_cmp expected actual
261
'
262
263
+test_expect_success 'left/right alignment formatting with stealing' '
264
+ git commit --amend -m short --author "long long long <long@me.com>" &&
265
+ git log --pretty="format:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
266
+ # complete the incomplete line at the end
267
+ echo >>actual &&
268
+ cat <<\EOF >expected &&
269
+short long long long
270
+message .. A U Thor
271
+add bar A U Thor
272
+initial A U Thor
273
+EOF
274
+ test_cmp expected actual
275
+'
276
+
277
test_done
utf8.c
+1
-1
@@ -9,7 +9,7 @@ struct interval {
9
int last;
10
};
11
12
-static size_t display_mode_esc_sequence_len(const char *s)
12
+size_t display_mode_esc_sequence_len(const char *s)
13
{
14
const char *p = s;
15
if (*p++ != '\033')
utf8.h
+1
@@ -3,6 +3,7 @@
3
4
typedef unsigned int ucs_char_t; /* assuming 32bit int */
5
6
+size_t display_mode_esc_sequence_len(const char *s);
7
int utf8_width(const char **start, size_t *remainder_p);
8
int utf8_strnwidth(const char *string, int len, int skip_ansi);
9
int utf8_strwidth(const char *string);