* [PATCH] for-each-ref: introduce color format
@ 2013-05-17 14:55 Ramkumar Ramachandra
2013-05-17 17:20 ` Junio C Hamano
2013-05-19 12:05 ` Duy Nguyen
0 siblings, 2 replies; 4+ messages in thread
From: Ramkumar Ramachandra @ 2013-05-17 14:55 UTC (permalink / raw)
To: Git List; +Cc: Junio C Hamano, Jeff King
You can now do something like
$ git for-each-ref --format='%C(red)%(refname:short)%C(reset)
%C(blue)%(upstream:diff)%C(reset)' --count 5 --sort='-committerdate'
refs/heads
To get output that's much more customizable 'git branch' output. Future
patches will attempt unify the semantics of 'git branch' and 'git
for-each-ref'.
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
So my evil plan is to keep extending this format until it's on par
with pretty-formats. Then, we can move towards unifying 'git branch'
and 'git for-each-ref'. This will involve deprecating badly
thought-out options like '-v', and replacing it with the more
powerful '--format'.
I just have one major doubt: in the above output, how do I align all
the upstream branches to the same column? How can I achieve it with
pretty-formats? Something like %*d? But * is already taken to mean
deref in for-each-ref's --format.
By the way, the main motivation for all this comes from the fact that
git for-each-ref is very nicely written :) Look at how it breaks
everything up into atoms and lazily gets the information it needs to
display.
builtin/for-each-ref.c | 26 ++++++++++++++++++++++----
1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
index 7f059c3..1563b25 100644
--- a/builtin/for-each-ref.c
+++ b/builtin/for-each-ref.c
@@ -9,6 +9,7 @@
#include "quote.h"
#include "parse-options.h"
#include "remote.h"
+#include "color.h"
/* Quoting styles */
#define QUOTE_NONE 0
@@ -155,10 +156,13 @@ static const char *find_next(const char *cp)
while (*cp) {
if (*cp == '%') {
/*
+ * %C( is the start of a color;
* %( is the start of an atom;
* %% is a quoted per-cent.
*/
- if (cp[1] == '(')
+ if (cp[1] == 'C' && cp[2] == '(')
+ return cp;
+ else if (cp[1] == '(')
return cp;
else if (cp[1] == '%')
cp++; /* skip over two % */
@@ -180,8 +184,12 @@ static int verify_format(const char *format)
const char *ep = strchr(sp, ')');
if (!ep)
return error("malformed format string %s", sp);
- /* sp points at "%(" and ep points at the closing ")" */
- parse_atom(sp + 2, ep);
+ /*
+ * Ignore color specifications: %C(
+ * sp points at "%(" and ep points at the closing ")"
+ */
+ if (prefixcmp(sp, "%C("))
+ parse_atom(sp + 2, ep);
cp = ep + 1;
}
return 0;
@@ -928,12 +936,22 @@ static void emit(const char *cp, const char *ep)
static void show_ref(struct refinfo *info, const char *format, int quote_style)
{
const char *cp, *sp, *ep;
+ char color[COLOR_MAXLEN];
for (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {
ep = strchr(sp, ')');
if (cp < sp)
emit(cp, sp);
- print_value(info, parse_atom(sp + 2, ep), quote_style);
+
+ /* Do we have a color specification? */
+ if (!prefixcmp(sp, "%C("))
+ color_parse_mem(sp + 3,
+ ep - sp - 3,
+ "--format ", color);
+ else {
+ printf("%s", color);
+ print_value(info, parse_atom(sp + 2, ep), quote_style);
+ }
}
if (*cp) {
sp = cp + strlen(cp);
--
1.8.3.rc2.13.g629b60a.dirty
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] for-each-ref: introduce color format
2013-05-17 14:55 [PATCH] for-each-ref: introduce color format Ramkumar Ramachandra
@ 2013-05-17 17:20 ` Junio C Hamano
2013-05-17 17:23 ` Ramkumar Ramachandra
2013-05-19 12:05 ` Duy Nguyen
1 sibling, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2013-05-17 17:20 UTC (permalink / raw)
To: Ramkumar Ramachandra; +Cc: Git List, Jeff King
Ramkumar Ramachandra <artagnon@gmail.com> writes:
> I just have one major doubt: in the above output, how do I align all
> the upstream branches to the same column? How can I achieve it with
> pretty-formats? Something like %*d? But * is already taken to mean
> deref in for-each-ref's --format.
Doesn't Duy's more recent work on the pretty-format front introduce
alignment operators?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] for-each-ref: introduce color format
2013-05-17 17:20 ` Junio C Hamano
@ 2013-05-17 17:23 ` Ramkumar Ramachandra
0 siblings, 0 replies; 4+ messages in thread
From: Ramkumar Ramachandra @ 2013-05-17 17:23 UTC (permalink / raw)
To: Junio C Hamano; +Cc: Git List, Jeff King
Junio C Hamano wrote:
> Doesn't Duy's more recent work on the pretty-format front introduce
> alignment operators?
Oh, awesome. I know where to steal that from now ;)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] for-each-ref: introduce color format
2013-05-17 14:55 [PATCH] for-each-ref: introduce color format Ramkumar Ramachandra
2013-05-17 17:20 ` Junio C Hamano
@ 2013-05-19 12:05 ` Duy Nguyen
1 sibling, 0 replies; 4+ messages in thread
From: Duy Nguyen @ 2013-05-19 12:05 UTC (permalink / raw)
To: Ramkumar Ramachandra; +Cc: Git List, Junio C Hamano, Jeff King
On Fri, May 17, 2013 at 9:55 PM, Ramkumar Ramachandra
<artagnon@gmail.com> wrote:
> You can now do something like
>
> $ git for-each-ref --format='%C(red)%(refname:short)%C(reset)
> %C(blue)%(upstream:diff)%C(reset)' --count 5 --sort='-committerdate'
> refs/heads
>
> To get output that's much more customizable 'git branch' output. Future
> patches will attempt unify the semantics of 'git branch' and 'git
> for-each-ref'.
>
> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
> ---
> So my evil plan is to keep extending this format until it's on par
> with pretty-formats. Then, we can move towards unifying 'git branch'
> and 'git for-each-ref'. This will involve deprecating badly
> thought-out options like '-v', and replacing it with the more
> powerful '--format'.
>
> I just have one major doubt: in the above output, how do I align all
> the upstream branches to the same column? How can I achieve it with
> pretty-formats? Something like %*d? But * is already taken to mean
> deref in for-each-ref's --format.
>
> By the way, the main motivation for all this comes from the fact that
> git for-each-ref is very nicely written :) Look at how it breaks
> everything up into atoms and lazily gets the information it needs to
> display.
If you can put energy into this, I suggest you improve pretty.c a bit,
adding new format_ref_message(), similar to format_commit_message(),
except that it takes a ref. You can extend "struct
format_commit_context" to contain what "struct refinfo" does. I think
pretty.c only lacks a few specifiers that for-each-ref has. I think I
mentioned it in my for-each-ref series already, but we need to think
how to specify "align to the left with the best width" and how
format_ref_message() can figure the width out. A callback function
might do.
--
Duy
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-05-19 12:05 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-17 14:55 [PATCH] for-each-ref: introduce color format Ramkumar Ramachandra
2013-05-17 17:20 ` Junio C Hamano
2013-05-17 17:23 ` Ramkumar Ramachandra
2013-05-19 12:05 ` Duy Nguyen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).