pager.con commit Merge branch 'ls/hint-rev-list-count' into maint (3afcec9)
   1#include "cache.h"
   2#include "run-command.h"
   3#include "sigchain.h"
   4
   5#ifndef DEFAULT_PAGER
   6#define DEFAULT_PAGER "less"
   7#endif
   8
   9/*
  10 * This is split up from the rest of git so that we can do
  11 * something different on Windows.
  12 */
  13
  14static const char *pager_argv[] = { NULL, NULL };
  15static struct child_process pager_process = CHILD_PROCESS_INIT;
  16
  17static void wait_for_pager(void)
  18{
  19        fflush(stdout);
  20        fflush(stderr);
  21        /* signal EOF to pager */
  22        close(1);
  23        close(2);
  24        finish_command(&pager_process);
  25}
  26
  27static void wait_for_pager_signal(int signo)
  28{
  29        wait_for_pager();
  30        sigchain_pop(signo);
  31        raise(signo);
  32}
  33
  34const char *git_pager(int stdout_is_tty)
  35{
  36        const char *pager;
  37
  38        if (!stdout_is_tty)
  39                return NULL;
  40
  41        pager = getenv("GIT_PAGER");
  42        if (!pager) {
  43                if (!pager_program)
  44                        git_config(git_default_config, NULL);
  45                pager = pager_program;
  46        }
  47        if (!pager)
  48                pager = getenv("PAGER");
  49        if (!pager)
  50                pager = DEFAULT_PAGER;
  51        if (!*pager || !strcmp(pager, "cat"))
  52                pager = NULL;
  53
  54        return pager;
  55}
  56
  57void setup_pager(void)
  58{
  59        const char *pager = git_pager(isatty(1));
  60
  61        if (!pager)
  62                return;
  63
  64        /*
  65         * force computing the width of the terminal before we redirect
  66         * the standard output to the pager.
  67         */
  68        (void) term_columns();
  69
  70        setenv("GIT_PAGER_IN_USE", "true", 1);
  71
  72        /* spawn the pager */
  73        pager_argv[0] = pager;
  74        pager_process.use_shell = 1;
  75        pager_process.argv = pager_argv;
  76        pager_process.in = -1;
  77        if (!getenv("LESS"))
  78                argv_array_push(&pager_process.env_array, "LESS=FRX");
  79        if (!getenv("LV"))
  80                argv_array_push(&pager_process.env_array, "LV=-c");
  81        argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
  82        if (start_command(&pager_process))
  83                return;
  84
  85        /* original process continues, but writes to the pipe */
  86        dup2(pager_process.in, 1);
  87        if (isatty(2))
  88                dup2(pager_process.in, 2);
  89        close(pager_process.in);
  90
  91        /* this makes sure that the parent terminates after the pager */
  92        sigchain_push_common(wait_for_pager_signal);
  93        atexit(wait_for_pager);
  94}
  95
  96int pager_in_use(void)
  97{
  98        const char *env;
  99        env = getenv("GIT_PAGER_IN_USE");
 100        return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
 101}
 102
 103/*
 104 * Return cached value (if set) or $COLUMNS environment variable (if
 105 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
 106 * and default to 80 if all else fails.
 107 */
 108int term_columns(void)
 109{
 110        static int term_columns_at_startup;
 111
 112        char *col_string;
 113        int n_cols;
 114
 115        if (term_columns_at_startup)
 116                return term_columns_at_startup;
 117
 118        term_columns_at_startup = 80;
 119
 120        col_string = getenv("COLUMNS");
 121        if (col_string && (n_cols = atoi(col_string)) > 0)
 122                term_columns_at_startup = n_cols;
 123#ifdef TIOCGWINSZ
 124        else {
 125                struct winsize ws;
 126                if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
 127                        term_columns_at_startup = ws.ws_col;
 128        }
 129#endif
 130
 131        return term_columns_at_startup;
 132}
 133
 134/*
 135 * How many columns do we need to show this number in decimal?
 136 */
 137int decimal_width(uintmax_t number)
 138{
 139        int width;
 140
 141        for (width = 1; number >= 10; width++)
 142                number /= 10;
 143        return width;
 144}
 145
 146/* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
 147int check_pager_config(const char *cmd)
 148{
 149        int want = -1;
 150        struct strbuf key = STRBUF_INIT;
 151        const char *value = NULL;
 152        strbuf_addf(&key, "pager.%s", cmd);
 153        if (!git_config_get_value(key.buf, &value)) {
 154                int b = git_config_maybe_bool(key.buf, value);
 155                if (b >= 0)
 156                        want = b;
 157                else {
 158                        want = 1;
 159                        pager_program = xstrdup(value);
 160                }
 161        }
 162        strbuf_release(&key);
 163        return want;
 164}