Merge branch 'nd/grep-true-path'
[gitweb.git] / column.c
index 05fa3e361c72d80210d05646c78ebc3a419b92dc..9367ba5db589e63b770c431121176ff046603d06 100644 (file)
--- a/column.c
+++ b/column.c
@@ -2,6 +2,7 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "run-command.h"
 #include "utf8.h"
 
 #define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \
@@ -15,6 +16,7 @@ struct column_data {
 
        int rows, cols;
        int *len;               /* cell length */
+       int *width;           /* index to the longest row in column */
 };
 
 /* return length of 's' in letters, ANSI escapes stripped */
@@ -56,6 +58,57 @@ static void layout(struct column_data *data, int *width)
        data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
 }
 
+static void compute_column_width(struct column_data *data)
+{
+       int i, x, y;
+       for (x = 0; x < data->cols; x++) {
+               data->width[x] = XY2LINEAR(data, x, 0);
+               for (y = 0; y < data->rows; y++) {
+                       i = XY2LINEAR(data, x, y);
+                       if (i < data->list->nr &&
+                           data->len[data->width[x]] < data->len[i])
+                               data->width[x] = i;
+               }
+       }
+}
+
+/*
+ * Shrink all columns by shortening them one row each time (and adding
+ * more columns along the way). Hopefully the longest cell will be
+ * moved to the next column, column is shrunk so we have more space
+ * for new columns. The process ends when the whole thing no longer
+ * fits in data->total_width.
+ */
+static void shrink_columns(struct column_data *data)
+{
+       data->width = xrealloc(data->width,
+                              sizeof(*data->width) * data->cols);
+       while (data->rows > 1) {
+               int x, total_width, cols, rows;
+               rows = data->rows;
+               cols = data->cols;
+
+               data->rows--;
+               data->cols = DIV_ROUND_UP(data->list->nr, data->rows);
+               if (data->cols != cols)
+                       data->width = xrealloc(data->width,
+                                              sizeof(*data->width) * data->cols);
+               compute_column_width(data);
+
+               total_width = strlen(data->opts.indent);
+               for (x = 0; x < data->cols; x++) {
+                       total_width += data->len[data->width[x]];
+                       total_width += data->opts.padding;
+               }
+               if (total_width > data->opts.width) {
+                       data->rows = rows;
+                       data->cols = cols;
+                       break;
+               }
+       }
+       compute_column_width(data);
+}
+
 /* Display without layout when not enabled */
 static void display_plain(const struct string_list *list,
                          const char *indent, const char *nl)
@@ -75,7 +128,18 @@ static int display_cell(struct column_data *data, int initial_width,
        i = XY2LINEAR(data, x, y);
        if (i >= data->list->nr)
                return -1;
+
        len = data->len[i];
+       if (data->width && data->len[data->width[x]] < initial_width) {
+               /*
+                * empty_cell has initial_width chars, if real column
+                * is narrower, increase len a bit so we fill less
+                * space.
+                */
+               len += initial_width - data->len[data->width[x]];
+               len -= data->opts.padding;
+       }
+
        if (COL_LAYOUT(data->colopts) == COL_COLUMN)
                newline = i + data->rows >= data->list->nr;
        else
@@ -108,6 +172,9 @@ static void display_table(const struct string_list *list,
 
        layout(&data, &initial_width);
 
+       if (colopts & COL_DENSE)
+               shrink_columns(&data);
+
        empty_cell = xmalloc(initial_width + 1);
        memset(empty_cell, ' ', initial_width);
        empty_cell[initial_width] = '\0';
@@ -118,6 +185,7 @@ static void display_table(const struct string_list *list,
        }
 
        free(data.len);
+       free(data.width);
        free(empty_cell);
 }
 
@@ -183,13 +251,22 @@ static int parse_option(const char *arg, int len, unsigned int *colopts,
                { "plain",  COL_PLAIN,    COL_LAYOUT_MASK },
                { "column", COL_COLUMN,   COL_LAYOUT_MASK },
                { "row",    COL_ROW,      COL_LAYOUT_MASK },
+               { "dense",  COL_DENSE,    0 },
        };
        int i;
 
        for (i = 0; i < ARRAY_SIZE(opts); i++) {
-               int arg_len = len, name_len;
+               int set = 1, arg_len = len, name_len;
                const char *arg_str = arg;
 
+               if (!opts[i].mask) {
+                       if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
+                               arg_str += 2;
+                               arg_len -= 2;
+                               set = 0;
+                       }
+               }
+
                name_len = strlen(opts[i].name);
                if (arg_len != name_len ||
                    strncmp(arg_str, opts[i].name, name_len))
@@ -206,6 +283,12 @@ static int parse_option(const char *arg, int len, unsigned int *colopts,
 
                if (opts[i].mask)
                        *colopts = (*colopts & ~opts[i].mask) | opts[i].value;
+               else {
+                       if (set)
+                               *colopts |= opts[i].value;
+                       else
+                               *colopts &= ~opts[i].value;
+               }
                return 0;
        }
 
@@ -281,3 +364,71 @@ int parseopt_column_callback(const struct option *opt,
 
        return 0;
 }
+
+static int fd_out = -1;
+static struct child_process column_process;
+
+int run_column_filter(int colopts, const struct column_options *opts)
+{
+       const char *av[10];
+       int ret, ac = 0;
+       struct strbuf sb_colopt  = STRBUF_INIT;
+       struct strbuf sb_width   = STRBUF_INIT;
+       struct strbuf sb_padding = STRBUF_INIT;
+
+       if (fd_out != -1)
+               return -1;
+
+       av[ac++] = "column";
+       strbuf_addf(&sb_colopt, "--raw-mode=%d", colopts);
+       av[ac++] = sb_colopt.buf;
+       if (opts && opts->width) {
+               strbuf_addf(&sb_width, "--width=%d", opts->width);
+               av[ac++] = sb_width.buf;
+       }
+       if (opts && opts->indent) {
+               av[ac++] = "--indent";
+               av[ac++] = opts->indent;
+       }
+       if (opts && opts->padding) {
+               strbuf_addf(&sb_padding, "--padding=%d", opts->padding);
+               av[ac++] = sb_padding.buf;
+       }
+       av[ac] = NULL;
+
+       fflush(stdout);
+       memset(&column_process, 0, sizeof(column_process));
+       column_process.in = -1;
+       column_process.out = dup(1);
+       column_process.git_cmd = 1;
+       column_process.argv = av;
+
+       ret = start_command(&column_process);
+
+       strbuf_release(&sb_colopt);
+       strbuf_release(&sb_width);
+       strbuf_release(&sb_padding);
+
+       if (ret)
+               return -2;
+
+       fd_out = dup(1);
+       close(1);
+       dup2(column_process.in, 1);
+       close(column_process.in);
+       return 0;
+}
+
+int stop_column_filter(void)
+{
+       if (fd_out == -1)
+               return -1;
+
+       fflush(stdout);
+       close(1);
+       finish_command(&column_process);
+       dup2(fd_out, 1);
+       close(fd_out);
+       fd_out = -1;
+       return 0;
+}