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