{"thread":{"id":"33851","subject":"[PATCH] for-each-ref: introduce color format","startedAt":"2013-05-17T14:55:52Z","lastAt":"2013-05-19T12:05:08Z","messageCount":4,"participants":["Ramkumar Ramachandra","Junio C Hamano","Duy Nguyen"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"217715","messageId":"1368802552-16024-1-git-send-email-artagnon@gmail.com","threadId":"33851","inReplyTo":null,"subject":"[PATCH] for-each-ref: introduce color format","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-05-17T14:55:52Z","receivedAt":"2013-05-17T14:55:52Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"You can now do something like\n\n$ git for-each-ref --format='%C(red)%(refname:short)%C(reset)\n%C(blue)%(upstream:diff)%C(reset)' --count 5 --sort='-committerdate'\nrefs/heads\n\nTo get output that's much more customizable 'git branch' output.  Future\npatches will attempt unify the semantics of 'git branch' and 'git\nfor-each-ref'.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n So my evil plan is to keep extending this format until it's on par\n with pretty-formats.  Then, we can move towards unifying 'git branch'\n and 'git for-each-ref'.  This will involve deprecating badly\n thought-out options like '-v', and replacing it with the more\n powerful '--format'.\n\n I just have one major doubt: in the above output, how do I align all\n the upstream branches to the same column?  How can I achieve it with\n pretty-formats?  Something like %*d?  But * is already taken to mean\n deref in for-each-ref's --format.\n\n By the way, the main motivation for all this comes from the fact that\n git for-each-ref is very nicely written :) Look at how it breaks\n everything up into atoms and lazily gets the information it needs to\n display.\n\n builtin/for-each-ref.c | 26 ++++++++++++++++++++++----\n 1 file changed, 22 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 7f059c3..1563b25 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -9,6 +9,7 @@\n #include \"quote.h\"\n #include \"parse-options.h\"\n #include \"remote.h\"\n+#include \"color.h\"\n \n /* Quoting styles */\n #define QUOTE_NONE 0\n@@ -155,10 +156,13 @@ static const char *find_next(const char *cp)\n \twhile (*cp) {\n \t\tif (*cp == '%') {\n \t\t\t/*\n+\t\t\t * %C( is the start of a color;\n \t\t\t * %( is the start of an atom;\n \t\t\t * %% is a quoted per-cent.\n \t\t\t */\n-\t\t\tif (cp[1] == '(')\n+\t\t\tif (cp[1] == 'C' && cp[2] == '(')\n+\t\t\t\treturn cp;\n+\t\t\telse if (cp[1] == '(')\n \t\t\t\treturn cp;\n \t\t\telse if (cp[1] == '%')\n \t\t\t\tcp++; /* skip over two % */\n@@ -180,8 +184,12 @@ static int verify_format(const char *format)\n \t\tconst char *ep = strchr(sp, ')');\n \t\tif (!ep)\n \t\t\treturn error(\"malformed format string %s\", sp);\n-\t\t/* sp points at \"%(\" and ep points at the closing \")\" */\n-\t\tparse_atom(sp + 2, ep);\n+\t\t/*\n+\t\t * Ignore color specifications: %C(\n+\t\t * sp points at \"%(\" and ep points at the closing \")\"\n+\t\t */\n+\t\tif (prefixcmp(sp, \"%C(\"))\n+\t\t\tparse_atom(sp + 2, ep);\n \t\tcp = ep + 1;\n \t}\n \treturn 0;\n@@ -928,12 +936,22 @@ static void emit(const char *cp, const char *ep)\n static void show_ref(struct refinfo *info, const char *format, int quote_style)\n {\n \tconst char *cp, *sp, *ep;\n+\tchar color[COLOR_MAXLEN];\n \n \tfor (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {\n \t\tep = strchr(sp, ')');\n \t\tif (cp < sp)\n \t\t\temit(cp, sp);\n-\t\tprint_value(info, parse_atom(sp + 2, ep), quote_style);\n+\n+\t\t/* Do we have a color specification? */\n+\t\tif (!prefixcmp(sp, \"%C(\"))\n+\t\t\tcolor_parse_mem(sp + 3,\n+\t\t\t\t\tep - sp - 3,\n+\t\t\t\t\t\"--format \", color);\n+\t\telse {\n+\t\t\tprintf(\"%s\", color);\n+\t\t\tprint_value(info, parse_atom(sp + 2, ep), quote_style);\n+\t\t}\n \t}\n \tif (*cp) {\n \t\tsp = cp + strlen(cp);\n-- \n1.8.3.rc2.13.g629b60a.dirty\n"},{"id":"217737","messageId":"7vsj1lzfo8.fsf@alter.siamese.dyndns.org","threadId":"33851","inReplyTo":"1368802552-16024-1-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH] for-each-ref: introduce color format","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-05-17T17:20:39Z","receivedAt":"2013-05-17T17:20:39Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ramkumar Ramachandra <artagnon@gmail.com> writes:\n\n>  I just have one major doubt: in the above output, how do I align all\n>  the upstream branches to the same column?  How can I achieve it with\n>  pretty-formats?  Something like %*d?  But * is already taken to mean\n>  deref in for-each-ref's --format.\n\nDoesn't Duy's more recent work on the pretty-format front introduce\nalignment operators?\n"},{"id":"217739","messageId":"CALkWK0=8nwU5d17=ci0yAYYzMiihQbHBqscfD2nUGgwPPirLkw@mail.gmail.com","threadId":"33851","inReplyTo":"7vsj1lzfo8.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH] for-each-ref: introduce color format","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-05-17T17:23:53Z","receivedAt":"2013-05-17T17:23:53Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"Junio C Hamano wrote:\n> Doesn't Duy's more recent work on the pretty-format front introduce\n> alignment operators?\n\nOh, awesome.  I know where to steal that from now ;)\n"},{"id":"217900","messageId":"CACsJy8CthvpJh6QM+umxY_Gq7nc-rGZ+Tcp7zAkadez-g+FFjQ@mail.gmail.com","threadId":"33851","inReplyTo":"1368802552-16024-1-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH] for-each-ref: introduce color format","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-05-19T12:05:08Z","receivedAt":"2013-05-19T12:05:08Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Fri, May 17, 2013 at 9:55 PM, Ramkumar Ramachandra\n<artagnon@gmail.com> wrote:\n> You can now do something like\n>\n> $ git for-each-ref --format='%C(red)%(refname:short)%C(reset)\n> %C(blue)%(upstream:diff)%C(reset)' --count 5 --sort='-committerdate'\n> refs/heads\n>\n> To get output that's much more customizable 'git branch' output.  Future\n> patches will attempt unify the semantics of 'git branch' and 'git\n> for-each-ref'.\n>\n> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n> ---\n>  So my evil plan is to keep extending this format until it's on par\n>  with pretty-formats.  Then, we can move towards unifying 'git branch'\n>  and 'git for-each-ref'.  This will involve deprecating badly\n>  thought-out options like '-v', and replacing it with the more\n>  powerful '--format'.\n>\n>  I just have one major doubt: in the above output, how do I align all\n>  the upstream branches to the same column?  How can I achieve it with\n>  pretty-formats?  Something like %*d?  But * is already taken to mean\n>  deref in for-each-ref's --format.\n>\n>  By the way, the main motivation for all this comes from the fact that\n>  git for-each-ref is very nicely written :) Look at how it breaks\n>  everything up into atoms and lazily gets the information it needs to\n>  display.\n\nIf you can put energy into this, I suggest you improve pretty.c a bit,\nadding new format_ref_message(), similar to format_commit_message(),\nexcept that it takes a ref. You can extend \"struct\nformat_commit_context\" to contain what \"struct refinfo\" does. I think\npretty.c only lacks a few specifiers that for-each-ref has. I think I\nmentioned it in my for-each-ref series already, but we need to think\nhow to specify \"align to the left with the best width\" and how\nformat_ref_message() can figure the width out. A callback function\nmight do.\n--\nDuy\n"}]}