Skip to content

Commit 066790d

Browse files
pcloudsgitster
authored andcommitted
pretty.c: support <direction>|(<negative number>) forms
%>|(num), %><|(num) and %<|(num), where num is a positive number, sets a fixed column from the screen's left border. There is no way for us to specifiy a column relative to the right border, which is useful when you want to make use of all terminal space (on big screens). Use negative num for that. Inspired by Go's array syntax (*). (*) I know Python has this first (or before Go, at least) but the idea didn't occur to me until I learned Go. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3ad87c8 commit 066790d

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

pretty.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,9 +1022,15 @@ static size_t parse_padding_placeholder(struct strbuf *sb,
10221022
int width;
10231023
if (!end || end == start)
10241024
return 0;
1025-
width = strtoul(start, &next, 10);
1025+
width = strtol(start, &next, 10);
10261026
if (next == start || width == 0)
10271027
return 0;
1028+
if (width < 0) {
1029+
if (to_column)
1030+
width += term_columns();
1031+
if (width < 0)
1032+
return 0;
1033+
}
10281034
c->padding = to_column ? -width : width;
10291035
c->flush_type = flush_type;
10301036

t/t4205-log-pretty-formats.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,17 @@ EOF
176176
test_cmp expected actual
177177
'
178178

179+
test_expect_success 'left alignment formatting at the nth column' '
180+
COLUMNS=50 git log --pretty="tformat:%h %<|(-10)%s" >actual &&
181+
qz_to_tab_space <<EOF >expected &&
182+
$head1 message two Z
183+
$head2 message one Z
184+
$head3 add bar Z
185+
$head4 $(commit_msg) Z
186+
EOF
187+
test_cmp expected actual
188+
'
189+
179190
test_expect_success 'left alignment formatting at the nth column. i18n.logOutputEncoding' '
180191
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %<|(40)%s" >actual &&
181192
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
@@ -308,6 +319,17 @@ EOF
308319
test_cmp expected actual
309320
'
310321

322+
test_expect_success 'right alignment formatting at the nth column' '
323+
COLUMNS=50 git log --pretty="tformat:%h %>|(-10)%s" >actual &&
324+
qz_to_tab_space <<EOF >expected &&
325+
$head1 message two
326+
$head2 message one
327+
$head3 add bar
328+
$head4 $(commit_msg)
329+
EOF
330+
test_cmp expected actual
331+
'
332+
311333
test_expect_success 'right alignment formatting at the nth column. i18n.logOutputEncoding' '
312334
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %>|(40)%s" >actual &&
313335
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&
@@ -397,6 +419,17 @@ EOF
397419
test_cmp expected actual
398420
'
399421

422+
test_expect_success 'center alignment formatting at the nth column' '
423+
COLUMNS=70 git log --pretty="tformat:%h %><|(-30)%s" >actual &&
424+
qz_to_tab_space <<EOF >expected &&
425+
$head1 message two Z
426+
$head2 message one Z
427+
$head3 add bar Z
428+
$head4 $(commit_msg) Z
429+
EOF
430+
test_cmp expected actual
431+
'
432+
400433
test_expect_success 'center alignment formatting at the nth column. i18n.logOutputEncoding' '
401434
git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %><|(40)%s" >actual &&
402435
qz_to_tab_space <<EOF | iconv -f utf-8 -t $test_encoding >expected &&

0 commit comments

Comments
 (0)