column.con commit git-prompt.sh: document GIT_PS1_STATESEPARATOR (ab7fade)
   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        data->width = xrealloc(data->width,
  85                               sizeof(*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                        data->width = xrealloc(data->width,
  95                                               sizeof(*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        data.len = xmalloc(sizeof(*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 = xmalloc(initial_width + 1);
 179        memset(empty_cell, ' ', initial_width);
 180        empty_cell[initial_width] = '\0';
 181        for (y = 0; y < data.rows; y++) {
 182                for (x = 0; x < data.cols; x++)
 183                        if (display_cell(&data, initial_width, empty_cell, x, y))
 184                                break;
 185        }
 186
 187        free(data.len);
 188        free(data.width);
 189        free(empty_cell);
 190}
 191
 192void print_columns(const struct string_list *list, unsigned int colopts,
 193                   const struct column_options *opts)
 194{
 195        struct column_options nopts;
 196
 197        if (!list->nr)
 198                return;
 199        assert((colopts & COL_ENABLE_MASK) != COL_AUTO);
 200
 201        memset(&nopts, 0, sizeof(nopts));
 202        nopts.indent = opts && opts->indent ? opts->indent : "";
 203        nopts.nl = opts && opts->nl ? opts->nl : "\n";
 204        nopts.padding = opts ? opts->padding : 1;
 205        nopts.width = opts && opts->width ? opts->width : term_columns() - 1;
 206        if (!column_active(colopts)) {
 207                display_plain(list, "", "\n");
 208                return;
 209        }
 210        switch (COL_LAYOUT(colopts)) {
 211        case COL_PLAIN:
 212                display_plain(list, nopts.indent, nopts.nl);
 213                break;
 214        case COL_ROW:
 215        case COL_COLUMN:
 216                display_table(list, colopts, &nopts);
 217                break;
 218        default:
 219                die("BUG: invalid layout mode %d", COL_LAYOUT(colopts));
 220        }
 221}
 222
 223int finalize_colopts(unsigned int *colopts, int stdout_is_tty)
 224{
 225        if ((*colopts & COL_ENABLE_MASK) == COL_AUTO) {
 226                if (stdout_is_tty < 0)
 227                        stdout_is_tty = isatty(1);
 228                *colopts &= ~COL_ENABLE_MASK;
 229                if (stdout_is_tty)
 230                        *colopts |= COL_ENABLED;
 231        }
 232        return 0;
 233}
 234
 235struct colopt {
 236        const char *name;
 237        unsigned int value;
 238        unsigned int mask;
 239};
 240
 241#define LAYOUT_SET 1
 242#define ENABLE_SET 2
 243
 244static int parse_option(const char *arg, int len, unsigned int *colopts,
 245                        int *group_set)
 246{
 247        struct colopt opts[] = {
 248                { "always", COL_ENABLED,  COL_ENABLE_MASK },
 249                { "never",  COL_DISABLED, COL_ENABLE_MASK },
 250                { "auto",   COL_AUTO,     COL_ENABLE_MASK },
 251                { "plain",  COL_PLAIN,    COL_LAYOUT_MASK },
 252                { "column", COL_COLUMN,   COL_LAYOUT_MASK },
 253                { "row",    COL_ROW,      COL_LAYOUT_MASK },
 254                { "dense",  COL_DENSE,    0 },
 255        };
 256        int i;
 257
 258        for (i = 0; i < ARRAY_SIZE(opts); i++) {
 259                int set = 1, arg_len = len, name_len;
 260                const char *arg_str = arg;
 261
 262                if (!opts[i].mask) {
 263                        if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
 264                                arg_str += 2;
 265                                arg_len -= 2;
 266                                set = 0;
 267                        }
 268                }
 269
 270                name_len = strlen(opts[i].name);
 271                if (arg_len != name_len ||
 272                    strncmp(arg_str, opts[i].name, name_len))
 273                        continue;
 274
 275                switch (opts[i].mask) {
 276                case COL_ENABLE_MASK:
 277                        *group_set |= ENABLE_SET;
 278                        break;
 279                case COL_LAYOUT_MASK:
 280                        *group_set |= LAYOUT_SET;
 281                        break;
 282                }
 283
 284                if (opts[i].mask)
 285                        *colopts = (*colopts & ~opts[i].mask) | opts[i].value;
 286                else {
 287                        if (set)
 288                                *colopts |= opts[i].value;
 289                        else
 290                                *colopts &= ~opts[i].value;
 291                }
 292                return 0;
 293        }
 294
 295        return error("unsupported option '%s'", arg);
 296}
 297
 298static int parse_config(unsigned int *colopts, const char *value)
 299{
 300        const char *sep = " ,";
 301        int group_set = 0;
 302
 303        while (*value) {
 304                int len = strcspn(value, sep);
 305                if (len) {
 306                        if (parse_option(value, len, colopts, &group_set))
 307                                return -1;
 308
 309                        value += len;
 310                }
 311                value += strspn(value, sep);
 312        }
 313        /*
 314         * Setting layout implies "always" if neither always, never
 315         * nor auto is specified.
 316         *
 317         * Current value in COL_ENABLE_MASK is disregarded. This means if
 318         * you set column.ui = auto and pass --column=row, then "auto"
 319         * will become "always".
 320         */
 321        if ((group_set & LAYOUT_SET) && !(group_set & ENABLE_SET))
 322                *colopts = (*colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
 323        return 0;
 324}
 325
 326static int column_config(const char *var, const char *value,
 327                         const char *key, unsigned int *colopts)
 328{
 329        if (!value)
 330                return config_error_nonbool(var);
 331        if (parse_config(colopts, value))
 332                return error("invalid column.%s mode %s", key, value);
 333        return 0;
 334}
 335
 336int git_column_config(const char *var, const char *value,
 337                      const char *command, unsigned int *colopts)
 338{
 339        const char *it = skip_prefix(var, "column.");
 340        if (!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;
 370
 371int run_column_filter(int colopts, const struct column_options *opts)
 372{
 373        const char *av[10];
 374        int ret, ac = 0;
 375        struct strbuf sb_colopt  = STRBUF_INIT;
 376        struct strbuf sb_width   = STRBUF_INIT;
 377        struct strbuf sb_padding = STRBUF_INIT;
 378
 379        if (fd_out != -1)
 380                return -1;
 381
 382        av[ac++] = "column";
 383        strbuf_addf(&sb_colopt, "--raw-mode=%d", colopts);
 384        av[ac++] = sb_colopt.buf;
 385        if (opts && opts->width) {
 386                strbuf_addf(&sb_width, "--width=%d", opts->width);
 387                av[ac++] = sb_width.buf;
 388        }
 389        if (opts && opts->indent) {
 390                av[ac++] = "--indent";
 391                av[ac++] = opts->indent;
 392        }
 393        if (opts && opts->padding) {
 394                strbuf_addf(&sb_padding, "--padding=%d", opts->padding);
 395                av[ac++] = sb_padding.buf;
 396        }
 397        av[ac] = NULL;
 398
 399        fflush(stdout);
 400        memset(&column_process, 0, sizeof(column_process));
 401        column_process.in = -1;
 402        column_process.out = dup(1);
 403        column_process.git_cmd = 1;
 404        column_process.argv = av;
 405
 406        ret = start_command(&column_process);
 407
 408        strbuf_release(&sb_colopt);
 409        strbuf_release(&sb_width);
 410        strbuf_release(&sb_padding);
 411
 412        if (ret)
 413                return -2;
 414
 415        fd_out = dup(1);
 416        close(1);
 417        dup2(column_process.in, 1);
 418        close(column_process.in);
 419        return 0;
 420}
 421
 422int stop_column_filter(void)
 423{
 424        if (fd_out == -1)
 425                return -1;
 426
 427        fflush(stdout);
 428        close(1);
 429        finish_command(&column_process);
 430        dup2(fd_out, 1);
 431        close(fd_out);
 432        fd_out = -1;
 433        return 0;
 434}