Re: [PATCH 2/3] help.c: make term_columns() cached and export it

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2012/2/10 Zbigniew Jędrzejewski-Szmek <zbyszek@xxxxxxxxx>:
> Since term_columns() will usually fail, when a pager is installed,
> the cache is primed before the pager is installed. If a pager is not
> installed, then the cache will be set on first use.

Conflict alert. term_columns() is also moved out of help.c in
nd/columns series on pu, commit cb0850f (Save terminal width before
setting up pager - 2012-02-04)

>
> Conforms to The Single UNIX Specification, Version 2
> (http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003).
>
> Signed-off-by: Zbigniew Jędrzejewski-Szmek <zbyszek@xxxxxxxxx>
> ---
>  help.c  |   37 +++++++++++++++++++++++++++++--------
>  help.h  |    2 ++
>  pager.c |    5 +++++
>  3 files changed, 36 insertions(+), 8 deletions(-)
>
> diff --git a/help.c b/help.c
> index bc15066..5d1cb1d 100644
> --- a/help.c
> +++ b/help.c
> @@ -5,26 +5,47 @@
>  #include "help.h"
>  #include "common-cmds.h"
>
> -/* most GUI terminals set COLUMNS (although some don't export it) */
> -static int term_columns(void)
> +/*
> + * Cache for term_columns() value. Set on first use or when
> + * installing a pager and replacing stdout.
> + */
> +static int term_columns_cache;
> +
> +/*
> + * Return cached value (if set) or $COLUMNS (if set and positive) or
> + * ioctl(1, TIOCGWINSZ).ws_col (if positive) or 80.
> + *
> + * $COLUMNS even if set, is usually not exported, so
> + * the variable can be used to override autodection.
> + */
> +int term_columns(void)
>  {
> -       char *col_string = getenv("COLUMNS");
> +       char *col_string;
>        int n_cols;
>
> -       if (col_string && (n_cols = atoi(col_string)) > 0)
> -               return n_cols;
> +       if (term_columns_cache)
> +               return term_columns_cache;
> +
> +       col_string = getenv("COLUMNS");
> +       if (col_string && (n_cols = atoi(col_string)) > 0) {
> +               term_columns_cache = n_cols;
> +               return term_columns_cache;
> +       }
>
>  #ifdef TIOCGWINSZ
>        {
>                struct winsize ws;
>                if (!ioctl(1, TIOCGWINSZ, &ws)) {
> -                       if (ws.ws_col)
> -                               return ws.ws_col;
> +                       if (ws.ws_col) {
> +                               term_columns_cache = ws.ws_col;
> +                               return term_columns_cache;
> +                       }
>                }
>        }
>  #endif
>
> -       return 80;
> +       term_columns_cache = 80;
> +       return term_columns_cache;
>  }
>
>  void add_cmdname(struct cmdnames *cmds, const char *name, int len)
> diff --git a/help.h b/help.h
> index b6b12d5..880a4b4 100644
> --- a/help.h
> +++ b/help.h
> @@ -29,4 +29,6 @@ extern void list_commands(const char *title,
>                          struct cmdnames *main_cmds,
>                          struct cmdnames *other_cmds);
>
> +extern int term_columns(void);
> +
>  #endif /* HELP_H */
> diff --git a/pager.c b/pager.c
> index 975955b..e7032de 100644
> --- a/pager.c
> +++ b/pager.c
> @@ -1,6 +1,7 @@
>  #include "cache.h"
>  #include "run-command.h"
>  #include "sigchain.h"
> +#include "help.h"
>
>  #ifndef DEFAULT_PAGER
>  #define DEFAULT_PAGER "less"
> @@ -76,6 +77,10 @@ void setup_pager(void)
>        if (!pager)
>                return;
>
> +       /* prime the term_columns() cache before it is too
> +        * late and stdout is replaced */
> +       (void) term_columns();
> +
>        setenv("GIT_PAGER_IN_USE", "true", 1);
>
>        /* spawn the pager */
> --
> 1.7.9.263.g4be11.dirty
>



-- 
Duy
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]