Skip to content

Commit 1640632

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a7f01c6 commit 1640632

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

Documentation/pretty-formats.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ The placeholders are:
173173
columns, padding spaces on the right if necessary
174174
- '%>(<N>)', '%>|(<N>)': similar to '%<(<N>)', '%<|(<N>)'
175175
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>)'
177180
respectively, but padding both sides (i.e. the text is centered)
178181

179182
NOTE: Some placeholders may depend on other options given to the

pretty.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ enum flush_type {
773773
no_flush,
774774
flush_right,
775775
flush_left,
776+
flush_left_and_steal,
776777
flush_both
777778
};
778779

@@ -1026,6 +1027,9 @@ static size_t parse_padding_placeholder(struct strbuf *sb,
10261027
if (*ch == '<') {
10271028
flush_type = flush_both;
10281029
ch++;
1030+
} else if (*ch == '>') {
1031+
flush_type = flush_left_and_steal;
1032+
ch++;
10291033
} else
10301034
flush_type = flush_left;
10311035
break;
@@ -1334,6 +1338,36 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
13341338
total_consumed++;
13351339
}
13361340
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+
13371371
if (len > padding) {
13381372
switch (c->truncate) {
13391373
case trunc_left:

t/t4205-log-pretty-formats.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,18 @@ EOF
260260
test_cmp expected actual
261261
'
262262

263+
test_expect_success 'left/right alignment formatting with stealing' '
264+
git commit --amend -m short --author "long long long <[email protected]>" &&
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+
263277
test_done

utf8.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct interval {
99
int last;
1010
};
1111

12-
static size_t display_mode_esc_sequence_len(const char *s)
12+
size_t display_mode_esc_sequence_len(const char *s)
1313
{
1414
const char *p = s;
1515
if (*p++ != '\033')

utf8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
typedef unsigned int ucs_char_t; /* assuming 32bit int */
55

6+
size_t display_mode_esc_sequence_len(const char *s);
67
int utf8_width(const char **start, size_t *remainder_p);
78
int utf8_strnwidth(const char *string, int len, int skip_ansi);
89
int utf8_strwidth(const char *string);

0 commit comments

Comments
 (0)