term_columns() checks for terminal width via ioctl(2). After redirecting, stdin is no longer terminal to get terminal width. Check terminal width and save it before redirect stdin in setup_pager() and let term_columns() reuse the value. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> --- Makefile | 1 + column.h | 6 ++++++ help.c | 23 +---------------------- pager.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 column.h diff --git a/Makefile b/Makefile index c457c34..cbbc699 100644 --- a/Makefile +++ b/Makefile @@ -2114,6 +2114,7 @@ builtin/prune.o builtin/reflog.o reachable.o: reachable.h builtin/commit.o builtin/revert.o wt-status.o: wt-status.h builtin/tar-tree.o archive-tar.o: tar.h connect.o transport.o url.o http-backend.o: url.h +help.o pager.o: column.h http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h diff --git a/column.h b/column.h new file mode 100644 index 0000000..55d8067 --- /dev/null +++ b/column.h @@ -0,0 +1,6 @@ +#ifndef COLUMN_H +#define COLUMN_H + +extern int term_columns(void); + +#endif diff --git a/help.c b/help.c index cbbe966..672561b 100644 --- a/help.c +++ b/help.c @@ -4,28 +4,7 @@ #include "levenshtein.h" #include "help.h" #include "common-cmds.h" - -/* most GUI terminals set COLUMNS (although some don't export it) */ -static int term_columns(void) -{ - char *col_string = getenv("COLUMNS"); - int n_cols; - - if (col_string && (n_cols = atoi(col_string)) > 0) - return n_cols; - -#ifdef TIOCGWINSZ - { - struct winsize ws; - if (!ioctl(1, TIOCGWINSZ, &ws)) { - if (ws.ws_col) - return ws.ws_col; - } - } -#endif - - return 80; -} +#include "column.h" void add_cmdname(struct cmdnames *cmds, const char *name, int len) { diff --git a/pager.c b/pager.c index 975955b..772a5a6 100644 --- a/pager.c +++ b/pager.c @@ -6,6 +6,21 @@ #define DEFAULT_PAGER "less" #endif +static int spawned_pager; +static int max_columns; + +static int retrieve_terminal_width(void) +{ +#ifdef TIOCGWINSZ + struct winsize ws; + if (ioctl(1, TIOCGWINSZ, &ws)) /* e.g., ENOSYS */ + return 0; + return ws.ws_col; +#else + return 0; +#endif +} + /* * This is split up from the rest of git so that we can do * something different on Windows. @@ -72,12 +87,17 @@ const char *git_pager(int stdout_is_tty) void setup_pager(void) { const char *pager = git_pager(isatty(1)); + int width; if (!pager) return; setenv("GIT_PAGER_IN_USE", "true", 1); + width = retrieve_terminal_width(); + if (width) + max_columns = width; + /* spawn the pager */ pager_argv[0] = pager; pager_process.use_shell = 1; @@ -110,3 +130,18 @@ int pager_in_use(void) env = getenv("GIT_PAGER_IN_USE"); return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0; } + +int term_columns() +{ + char *col_string = getenv("COLUMNS"); + int n_cols; + + if (col_string && (n_cols = atoi(col_string)) > 0) + return n_cols; + + if (spawned_pager && max_columns) + return max_columns; + + n_cols = retrieve_terminal_width(); + return n_cols ? n_cols : 80; +} -- 1.7.8.36.g69ee2 -- 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