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