Thread (25 messages) 25 messages, 4 authors, 2016-06-15

[PATCH v4 02/10] ref-filter: make the 'color' use ref_formatting_state

flat view
STALE3770d SUPERSEDED

From: Karthik Nayak <hidden>
Date: 2016-06-15 23:05:52
Subsystem: the rest · Maintainer: Linus Torvalds

Revision v4 of 6 in this series.

Revisions (6)
  1. v3 [diff vs current]
  2. v4 current
  3. v10 [diff vs current]
  4. v13 [diff vs current]
  5. v16 [diff vs current]
  6. v17 [diff vs current]
Make color which was considered as an atom, to use
ref_formatting_state and act as a pseudo atom. This allows
interchangeability between 'align' and 'color'.

Helped-by: Junio C Hamano [off-list ref]
Mentored-by: Christian Couder [off-list ref]
Mentored-by: Matthieu Moy [off-list ref]
Signed-off-by: Karthik Nayak <redacted>
---
 ref-filter.c | 16 ++++++++++++----
 ref-filter.h |  1 +
 2 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/ref-filter.c b/ref-filter.c
index 3c90ffc..fd13a23 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -663,7 +663,8 @@ static void populate_value(struct ref_formatting_state *state,
 
 			if (color_parse(name + 6, color) < 0)
 				die(_("unable to parse format"));
-			v->s = xstrdup(color);
+			state->color = xstrdup(color);
+			v->pseudo_atom = 1;
 			continue;
 		} else if (!strcmp(name, "flag")) {
 			char buf[256], *cp = buf;
@@ -1217,6 +1218,11 @@ void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
 static void ref_formatting(struct ref_formatting_state *state, struct atom_value *v,
 			   struct strbuf *value)
 {
+	if (state->color) {
+		strbuf_addstr(value, state->color);
+		free((void *)state->color);
+		state->color = NULL;
+	}
 	if (state->pad_to_right) {
 		if (!is_utf8(v->s))
 			strbuf_addf(value, "%-*s", state->pad_to_right, v->s);
@@ -1224,8 +1230,9 @@ static void ref_formatting(struct ref_formatting_state *state, struct atom_value
 			int len = strlen(v->s) - utf8_strwidth(v->s);
 			strbuf_addf(value, "%-*s", state->pad_to_right + len, v->s);
 		}
-	} else
-		strbuf_addf(value, "%s", v->s);
+		return;
+	}
+	strbuf_addf(value, "%s", v->s);
 }
 
 static void print_value(struct ref_formatting_state *state, struct atom_value *v)
@@ -1326,7 +1333,8 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
 
 		if (color_parse("reset", color) < 0)
 			die("BUG: couldn't parse 'reset' as a color");
-		resetv.s = color;
+		resetv.s = "";
+		state.color = xstrdup(color);
 		print_value(&state, &resetv);
 	}
 	putchar('\n');
diff --git a/ref-filter.h b/ref-filter.h
index ea2d0e6..bacbb23 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -31,6 +31,7 @@ struct ref_sorting {
 struct ref_formatting_state {
 	unsigned int pad_to_right; /*pad atoms to the right*/
 	int quote_style;
+	const char *color;
 };
 
 struct ref_array_item {
-- 
2.4.6
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help