From 3ad87c807c2b6cbfbdfb2c78412781ecc7db593d Mon Sep 17 00:00:00 2001 From: Josef Kufner Date: Thu, 16 Jun 2016 20:18:37 +0700 Subject: [PATCH 1/2] pretty: pass graph width to pretty formatting for use in '%>|(N)' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pass graph width to pretty formatting, to make N in '%>|(N)' include columns consumed by graph rendered when --graph option is in use. For example, in the output of git log --all --graph --pretty='format: [%>|(20)%h] %ar%d' this change will make all commit hashes align at 20th column from the edge of the terminal, not from the edge of the graph. Signed-off-by: Josef Kufner Signed-off-by: Junio C Hamano Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- commit.h | 1 + graph.c | 7 +++++++ graph.h | 5 +++++ log-tree.c | 2 ++ pretty.c | 1 + t/t4205-log-pretty-formats.sh | 24 ++++++++++++++++++++++++ 6 files changed, 40 insertions(+) diff --git a/commit.h b/commit.h index b06db4d5d9..0c923f05f7 100644 --- a/commit.h +++ b/commit.h @@ -161,6 +161,7 @@ struct pretty_print_context { * should not be counted on by callers. */ struct string_list in_body_headers; + int graph_width; }; struct userformat_want { diff --git a/graph.c b/graph.c index 1350bdde3b..ad766facad 100644 --- a/graph.c +++ b/graph.c @@ -669,6 +669,13 @@ static void graph_output_padding_line(struct git_graph *graph, graph_pad_horizontally(graph, sb, graph->num_new_columns * 2); } + +int graph_width(struct git_graph *graph) +{ + return graph->width; +} + + static void graph_output_skip_line(struct git_graph *graph, struct strbuf *sb) { /* diff --git a/graph.h b/graph.h index 0be62bd8b1..3f48c19b62 100644 --- a/graph.h +++ b/graph.h @@ -67,6 +67,11 @@ int graph_is_commit_finished(struct git_graph const *graph); int graph_next_line(struct git_graph *graph, struct strbuf *sb); +/* + * Return current width of the graph in on-screen characters. + */ +int graph_width(struct git_graph *graph); + /* * graph_show_*: helper functions for printing to stdout */ diff --git a/log-tree.c b/log-tree.c index 78a5381d0e..8d393150c0 100644 --- a/log-tree.c +++ b/log-tree.c @@ -687,6 +687,8 @@ void show_log(struct rev_info *opt) ctx.output_encoding = get_log_output_encoding(); if (opt->from_ident.mail_begin && opt->from_ident.name_begin) ctx.from_ident = &opt->from_ident; + if (opt->graph) + ctx.graph_width = graph_width(opt->graph); pretty_print_commit(&ctx, commit, &msgbuf); if (opt->add_signoff) diff --git a/pretty.c b/pretty.c index 87c44971a1..4f33b09a25 100644 --- a/pretty.c +++ b/pretty.c @@ -1299,6 +1299,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */ if (!start) start = sb->buf; occupied = utf8_strnwidth(start, -1, 1); + occupied += c->pretty_ctx->graph_width; padding = (-padding) - occupied; } while (1) { diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh index 7398605e7b..d75cdbbf9c 100755 --- a/t/t4205-log-pretty-formats.sh +++ b/t/t4205-log-pretty-formats.sh @@ -319,6 +319,19 @@ EOF test_cmp expected actual ' +# Note: Space between 'message' and 'two' should be in the same column +# as in previous test. +test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' ' + git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual && + iconv -f utf-8 -t $test_encoding >expected <actual && cat <expected && @@ -330,6 +343,17 @@ EOF test_cmp expected actual ' +test_expect_success 'right alignment formatting with no padding and with --graph' ' + git log --graph --pretty="tformat:%>(1)%s" >actual && + cat <expected && +* message two +* message one +* add bar +* $(commit_msg) +EOF + test_cmp expected actual +' + test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' ' git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual && cat <expected && From 066790d7cb0fa22e64f1276d8a0e33d18484f62a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Thu, 16 Jun 2016 20:18:38 +0700 Subject: [PATCH 2/2] pretty.c: support |() forms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit %>|(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 Signed-off-by: Junio C Hamano --- pretty.c | 8 +++++++- t/t4205-log-pretty-formats.sh | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/pretty.c b/pretty.c index 4f33b09a25..1c67b23968 100644 --- a/pretty.c +++ b/pretty.c @@ -1022,9 +1022,15 @@ static size_t parse_padding_placeholder(struct strbuf *sb, int width; if (!end || end == start) return 0; - width = strtoul(start, &next, 10); + width = strtol(start, &next, 10); if (next == start || width == 0) return 0; + if (width < 0) { + if (to_column) + width += term_columns(); + if (width < 0) + return 0; + } c->padding = to_column ? -width : width; c->flush_type = flush_type; diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh index d75cdbbf9c..d9f62425b0 100755 --- a/t/t4205-log-pretty-formats.sh +++ b/t/t4205-log-pretty-formats.sh @@ -176,6 +176,17 @@ EOF test_cmp expected actual ' +test_expect_success 'left alignment formatting at the nth column' ' + COLUMNS=50 git log --pretty="tformat:%h %<|(-10)%s" >actual && + qz_to_tab_space <expected && +$head1 message two Z +$head2 message one Z +$head3 add bar Z +$head4 $(commit_msg) Z +EOF + test_cmp expected actual +' + test_expect_success 'left alignment formatting at the nth column. i18n.logOutputEncoding' ' git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %<|(40)%s" >actual && qz_to_tab_space <expected && @@ -308,6 +319,17 @@ EOF test_cmp expected actual ' +test_expect_success 'right alignment formatting at the nth column' ' + COLUMNS=50 git log --pretty="tformat:%h %>|(-10)%s" >actual && + qz_to_tab_space <expected && +$head1 message two +$head2 message one +$head3 add bar +$head4 $(commit_msg) +EOF + test_cmp expected actual +' + test_expect_success 'right alignment formatting at the nth column. i18n.logOutputEncoding' ' git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %>|(40)%s" >actual && qz_to_tab_space <expected && @@ -397,6 +419,17 @@ EOF test_cmp expected actual ' +test_expect_success 'center alignment formatting at the nth column' ' + COLUMNS=70 git log --pretty="tformat:%h %><|(-30)%s" >actual && + qz_to_tab_space <expected && +$head1 message two Z +$head2 message one Z +$head3 add bar Z +$head4 $(commit_msg) Z +EOF + test_cmp expected actual +' + test_expect_success 'center alignment formatting at the nth column. i18n.logOutputEncoding' ' git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %><|(40)%s" >actual && qz_to_tab_space <expected &&