ref-filter: introduce format_ref_array_item()

To allow column display, we will need to first render the output in a string list to allow print_columns() to compute the proper size of each column before starting the actual output. Introduce the function format_ref_array_item() that does the formatting of a ref_array_item to an strbuf. show_ref_array_item() is kept as a convenience wrapper around it which obtains the strbuf and prints it the standard output. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jan 10, 2017 at 14:19 UTC 99c6a71d4f7f4197f5e8600b0edec2a6a9fd9988
2 files changed +15 -4
ref-filter.c
+12 -4
@@ -1833,10 +1833,10 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting
1833 }
1834 }
1835
1836 -void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1836 +void format_ref_array_item(struct ref_array_item *info, const char *format,
1837 + int quote_style, struct strbuf *final_buf)
1838 {
1839 const char *cp, *sp, *ep;
1839 - struct strbuf *final_buf;
1840 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
1841
1842 state.quote_style = quote_style;
@@ -1866,9 +1866,17 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
1866 }
1867 if (state.stack->prev)
1868 die(_("format: %%(end) atom missing"));
1869 - final_buf = &state.stack->output;
1870 - fwrite(final_buf->buf, 1, final_buf->len, stdout);
1869 + strbuf_addbuf(final_buf, &state.stack->output);
1870 pop_stack_element(&state.stack);
1871 +}
1872 +
1873 +void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style)
1874 +{
1875 + struct strbuf final_buf = STRBUF_INIT;
1876 +
1877 + format_ref_array_item(info, format, quote_style, &final_buf);
1878 + fwrite(final_buf.buf, 1, final_buf.len, stdout);
1879 + strbuf_release(&final_buf);
1880 putchar('\n');
1881 }
1882
ref-filter.h
+3
@@ -100,6 +100,9 @@ int parse_ref_filter_atom(const char *atom, const char *ep);
100 int verify_ref_format(const char *format);
101 /* Sort the given ref_array as per the ref_sorting provided */
102 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
103 +/* Based on the given format and quote_style, fill the strbuf */
104 +void format_ref_array_item(struct ref_array_item *info, const char *format,
105 + int quote_style, struct strbuf *final_buf);
106 /* Print the ref using the given format and quote_style */
107 void show_ref_array_item(struct ref_array_item *info, const char *format, int quote_style);
108 /* Callback function for parsing the sort option */