column.con commit column: show auto columns when pager is active (b2d3fd2)
   1#include "cache.h"
   2#include "config.h"
   3#include "column.h"
   4#include "string-list.h"
   5#include "parse-options.h"
   6#include "run-command.h"
   7#include "utf8.h"
   8#include "pager.c"
   9
  10#define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \
  11                            (x) * (d)->rows + (y) : \
  12                            (y) * (d)->cols + (x))
  13
  14struct column_data {
  15        const struct string_list *list;
  16        unsigned int colopts;
  17        struct column_options opts;
  18
  19        int rows, cols;
  20        int *len;               /* cell length */
  21        int *width;           /* index to the longest row in column */
  22};
  23
  24/* return length of 's' in letters, ANSI escapes stripped */
  25static int item_length(unsigned int colopts, const char *s)
  26{
  27        int len, i = 0;
  28        struct strbuf str = STRBUF_INIT;
  29
  30        strbuf_addstr(&str, s);
  31        while ((s = strstr(str.buf + i, "\033[")) != NULL) {
  32                int len = strspn(s + 2, "0123456789;");
  33                i = s - str.buf;
  34                strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
  35        }
  36        len = utf8_strwidth(str.buf);
  37        strbuf_release(&str);
  38        return len;
  39}
  40
  41/*
  42 * Calculate cell width, rows and cols for a table of equal cells, given
  43 * table width and how many spaces between cells.
  44 */
  45static void layout(struct column_data *data, int *width)
  46{
  47        int i;
  48
  49        *width = 0;
  50        for (i = 0; i < data->list->nr; i++)
  51                if (*width < data->len[i])
  52                        *width = data->len[i];
  53
  54        *width += data->opts.padding;
  55
  56        data->cols = (data->opts.width - strlen(data->opts.indent)) / *width;
  57        if (data->cols == 0)
  58                data->cols = 1;
  59
  60        data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
  61}
  62
  63static void compute_column_width(struct column_data *data)
  64{
  65        int i, x, y;
  66        for (x = 0; x < data->cols; x++) {
  67                data->width[x] = XY2LINEAR(data, x, 0);
  68                for (y = 0; y < data->rows; y++) {
  69                        i = XY2LINEAR(data, x, y);
  70                        if (i < data->list->nr &&
  71                            data->len[data->width[x]] < data->len[i])
  72                                data->width[x] = i;
  73                }
  74        }
  75}
  76
  77/*
  78 * Shrink all columns by shortening them one row each time (and adding
  79 * more columns along the way). Hopefully the longest cell will be
  80 * moved to the next column, column is shrunk so we have more space
  81 * for new columns. The process ends when the whole thing no longer
  82 * fits in data->total_width.
  83 */
  84static void shrink_columns(struct column_data *data)
  85{
  86        REALLOC_ARRAY(data->width, data->cols);
  87        while (data->rows > 1) {
  88                int x, total_width, cols, rows;
  89                rows = data->rows;
  90                cols = data->cols;
  91
  92                data->rows--;
  93                data->cols = DIV_ROUND_UP(data->list->nr, data->rows);
  94                if (data->cols != cols)
  95                        REALLOC_ARRAY(data->width, data->cols);
  96                compute_column_width(data);
  97
  98                total_width = strlen(data->opts.indent);
  99                for (x = 0; x < data->cols; x++) {
 100                        total_width += data->len[data->width[x]];
 101                        total_width += data->opts.padding;
 102                }
 103                if (total_width > data->opts.width) {
 104                        data->rows = rows;
 105                        data->cols = cols;
 106                        break;
 107                }
 108        }
 109        compute_column_width(data);
 110}
 111
 112/* Display without layout when not enabled */
 113static void display_plain(const struct string_list *list,
 114                          const char *indent, const char *nl)
 115{
 116        int i;
 117
 118        for (i = 0; i < list->nr; i++)
 119                printf("%s%s%s", indent, list->items[i].string, nl);
 120}
 121
 122/* Print a cell to stdout with all necessary leading/traling space */
 123static int display_cell(struct column_data *data, int initial_width,
 124                        const char *empty_cell, int x, int y)
 125{
 126        int i, len, newline;
 127
 128        i = XY2LINEAR(data, x, y);
 129        if (i >= data->list->nr)
 130                return -1;
 131
 132        len = data->len[i];
 133        if (data->width && data->len[data->width[x]] < initial_width) {
 134                /*
 135                 * empty_cell has initial_width chars, if real column
 136                 * is narrower, increase len a bit so we fill less
 137                 * space.
 138                 */
 139                len += initial_width - data->len[data->width[x]];
 140                len -= data->opts.padding;
 141        }
 142
 143        if (COL_LAYOUT(data->colopts) == COL_COLUMN)
 144                newline = i + data->rows >= data->list->nr;
 145        else
 146                newline = x == data->cols - 1 || i == data->list->nr - 1;
 147
 148        printf("%s%s%s",
 149               x == 0 ? data->opts.indent : "",
 150               data->list->items[i].string,
 151               newline ? data->opts.nl : empty_cell + len);
 152        return 0;
 153}
 154
 155/* Display COL_COLUMN or COL_ROW */
 156static void display_table(const struct string_list *list,
 157                          unsigned int colopts,
 158                          const struct column_options *opts)
 159{
 160        struct column_data data;
 161        int x, y, i, initial_width;
 162        char *empty_cell;
 163
 164        memset(&data, 0, sizeof(data));
 165        data.list = list;
 166        data.colopts = colopts;
 167        data.opts = *opts;
 168
 169        ALLOC_ARRAY(data.len, list->nr);
 170        for (i = 0; i < list->nr; i++)
 171                data.len[i] = item_length(colopts, list->items[i].string);
 172
 173        layout(&data, &initial_width);
 174
 175        if (colopts & COL_DENSE)
 176                shrink_columns(&data);
 177
 178        empty_cell = xmallocz(initial_width);
 179        memset(empty_cell, ' ', initial_width);
 180        for (y = 0; y < data.rows; y++) {
 181                for (x = 0; x < data.cols; x++)
 182                        if (display_cell(&data, initial_width, empty_cell, x, y))
 183                                break;
 184        }
 185
 186        free(data.len);
 187        free(data.width);
 188        free(empty_cell);
 189}
 190
 191void print_columns(const struct string_list *list, unsigned int colopts,
 192                   const struct column_options *opts)
 193{
 194        struct column_options nopts;
 195
 196        if (!list->nr)
 197                return;
 198        assert((colopts & COL_ENABLE_MASK) != COL_AUTO);
 199
 200        memset(&nopts, 0, sizeof(nopts));
 201        nopts.indent = opts && opts->indent ? opts->indent : "";
 202        nopts.nl = opts && opts->nl ? opts->nl : "\n";
 203        nopts.padding = opts ? opts->padding : 1;
 204        nopts.width = opts && opts->width ? opts->width : term_columns() - 1;
 205        if (!column_active(colopts)) {
 206                display_plain(list, "", "\n");
 207                return;
 208        }
 209        switch (COL_LAYOUT(colopts)) {
 210        case COL_PLAIN:
 211                display_plain(list, nopts.indent, nopts.nl);
 212                break;
 213        case COL_ROW:
 214        case COL_COLUMN:
 215                display_table(list, colopts, &nopts);
 216                break;
 217        default:
 218                die("BUG: invalid layout mode %d", COL_LAYOUT(colopts));
 219        }
 220}
 221
 222int finalize_colopts(unsigned int *colopts, int stdout_is_tty)
 223{
 224        if ((*colopts & COL_ENABLE_MASK) == COL_AUTO) {
 225                if (stdout_is_tty < 0)
 226                        stdout_is_tty = isatty(1);
 227                *colopts &= ~COL_ENABLE_MASK;
 228                if (stdout_is_tty || pager_in_use())
 229                        *colopts |= COL_ENABLED;
 230        }
 231        return 0;
 232}
 233
 234struct colopt {
 235        const char *name;
 236        unsigned int value;
 237        unsigned int mask;
 238};
 239
 240#define LAYOUT_SET 1
 241#define ENABLE_SET 2
 242
 243static int parse_option(const char *arg, int len, unsigned int *colopts,
 244                        int *group_set)
 245{
 246        struct colopt opts[] = {
 247                { "always", COL_ENABLED,  COL_ENABLE_MASK },
 248                { "never",  COL_DISABLED, COL_ENABLE_MASK },
 249                { "auto",   COL_AUTO,     COL_ENABLE_MASK },
 250                { "plain",  COL_PLAIN,    COL_LAYOUT_MASK },
 251                { "column", COL_COLUMN,   COL_LAYOUT_MASK },
 252                { "row",    COL_ROW,      COL_LAYOUT_MASK },
 253                { "dense",  COL_DENSE,    0 },
 254        };
 255        int i;
 256
 257        for (i = 0; i < ARRAY_SIZE(opts); i++) {
 258                int set = 1, arg_len = len, name_len;
 259                const char *arg_str = arg;
 260
 261                if (!opts[i].mask) {
 262                        if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
 263                                arg_str += 2;
 264                                arg_len -= 2;
 265                                set = 0;
 266                        }
 267                }
 268
 269                name_len = strlen(opts[i].name);
 270                if (arg_len != name_len ||
 271                    strncmp(arg_str, opts[i].name, name_len))
 272                        continue;
 273
 274                switch (opts[i].mask) {
 275                case COL_ENABLE_MASK:
 276                        *group_set |= ENABLE_SET;
 277                        break;
 278                case COL_LAYOUT_MASK:
 279                        *group_set |= LAYOUT_SET;
 280                        break;
 281                }
 282
 283                if (opts[i].mask)
 284                        *colopts = (*colopts & ~opts[i].mask) | opts[i].value;
 285                else {
 286                        if (set)
 287                                *colopts |= opts[i].value;
 288                        else
 289                                *colopts &= ~opts[i].value;
 290                }
 291                return 0;
 292        }
 293
 294        return error("unsupported option '%s'", arg);
 295}
 296
 297static int parse_config(unsigned int *colopts, const char *value)
 298{
 299        const char *sep = " ,";
 300        int group_set = 0;
 301
 302        while (*value) {
 303                int len = strcspn(value, sep);
 304                if (len) {
 305                        if (parse_option(value, len, colopts, &group_set))
 306                                return -1;
 307
 308                        value += len;
 309                }
 310                value += strspn(value, sep);
 311        }
 312        /*
 313         * If none of "always", "never", and "auto" is specified, then setting
 314         * layout implies "always".
 315         *
 316         * Current value in COL_ENABLE_MASK is disregarded. This means if
 317         * you set column.ui = auto and pass --column=row, then "auto"
 318         * will become "always".
 319         */
 320        if ((group_set & LAYOUT_SET) && !(group_set & ENABLE_SET))
 321                *colopts = (*colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
 322        return 0;
 323}
 324
 325static int column_config(const char *var, const char *value,
 326                         const char *key, unsigned int *colopts)
 327{
 328        if (!value)
 329                return config_error_nonbool(var);
 330        if (parse_config(colopts, value))
 331                return error("invalid column.%s mode %s", key, value);
 332        return 0;
 333}
 334
 335int git_column_config(const char *var, const char *value,
 336                      const char *command, unsigned int *colopts)
 337{
 338        const char *it;
 339
 340        if (!skip_prefix(var, "column.", &it))
 341                return 0;
 342
 343        if (!strcmp(it, "ui"))
 344                return column_config(var, value, "ui", colopts);
 345
 346        if (command && !strcmp(it, command))
 347                return column_config(var, value, it, colopts);
 348
 349        return 0;
 350}
 351
 352int parseopt_column_callback(const struct option *opt,
 353                             const char *arg, int unset)
 354{
 355        unsigned int *colopts = opt->value;
 356        *colopts |= COL_PARSEOPT;
 357        *colopts &= ~COL_ENABLE_MASK;
 358        if (unset)              /* --no-column == never */
 359                return 0;
 360        /* --column == always unless "arg" states otherwise */
 361        *colopts |= COL_ENABLED;
 362        if (arg)
 363                return parse_config(colopts, arg);
 364
 365        return 0;
 366}
 367
 368static int fd_out = -1;
 369static struct child_process column_process = CHILD_PROCESS_INIT;
 370
 371int run_column_filter(int colopts, const struct column_options *opts)
 372{
 373        struct argv_array *argv;
 374
 375        if (fd_out != -1)
 376                return -1;
 377
 378        child_process_init(&column_process);
 379        argv = &column_process.args;
 380
 381        argv_array_push(argv, "column");
 382        argv_array_pushf(argv, "--raw-mode=%d", colopts);
 383        if (opts && opts->width)
 384                argv_array_pushf(argv, "--width=%d", opts->width);
 385        if (opts && opts->indent)
 386                argv_array_pushf(argv, "--indent=%s", opts->indent);
 387        if (opts && opts->padding)
 388                argv_array_pushf(argv, "--padding=%d", opts->padding);
 389
 390        fflush(stdout);
 391        column_process.in = -1;
 392        column_process.out = dup(1);
 393        column_process.git_cmd = 1;
 394
 395        if (start_command(&column_process))
 396                return -2;
 397
 398        fd_out = dup(1);
 399        close(1);
 400        dup2(column_process.in, 1);
 401        close(column_process.in);
 402        return 0;
 403}
 404
 405int stop_column_filter(void)
 406{
 407        if (fd_out == -1)
 408                return -1;
 409
 410        fflush(stdout);
 411        close(1);
 412        finish_command(&column_process);
 413        dup2(fd_out, 1);
 414        close(fd_out);
 415        fd_out = -1;
 416        return 0;
 417}