git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/7] Move term_columns() to pager.c and save terminal width before pager

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Feb 9, 2011, 05:14 UTC
Message-ID
<20110209051456.GA28159@elie>
In-Reply-To
<1297178541-31124-2-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy wrote:
> term_columns() checks for terminal width via ioctl(1).
ioctl(2). :)

I like the idea of checking width before launching a pager and exporting it, yes.

Show 7 quoted lines
> --- a/cache.h
> +++ b/cache.h
> @@ -1045,6 +1045,7 @@ extern void setup_pager(void);
>  extern const char *pager_program;
>  extern int pager_in_use(void);
>  extern int pager_use_color;
> +extern int term_columns();

Should say (void) rather than leaving the parameter list unspecified, no?

It will only be needed by a few translation units, I hope. Putting it in column.h can avoid some pointless recompilation.

[...]
Show 6 quoted lines
> +++ b/pager.c
> @@ -12,6 +12,7 @@
>   */
>  
>  static int spawned_pager;
> +static int max_columns;

Could be stored in the COLUMNS environment variable, but an integer variable like this is is simpler.

[...]
Show 13 quoted lines
> @@ -80,6 +81,15 @@ void setup_pager(void)
>  
>  	spawned_pager = 1; /* means we are emitting to terminal */
>  
> +#ifdef TIOCGWINSZ
> +	{
> +		struct winsize ws;
> +		if (!ioctl(1, TIOCGWINSZ, &ws)) {
> +			if (ws.ws_col)
> +				max_columns = ws.ws_col;
> +		}
> +	}
> +#endif
Mm, repeated code.

How about something like this on top? Not sure if the "if (ws.ws_col)" is useful. I've left it alone.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 Makefile |    1 +
 cache.h  |    1 -
 column.h |    6 ++++++
 help.c   |    1 +
 pager.c  |   48 ++++++++++++++++++++++++++----------------------
 5 files changed, 34 insertions(+), 23 deletions(-)
 create mode 100644 column.h
diff --git a/Makefile b/Makefile
index 775ee83..ed9e94b 100644
--- a/Makefile
+++ b/Makefile
@@ -1956,6 +1956,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 http-backend.o: url.h
+pager.o help.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/cache.h b/cache.h
index bcbd5f2..d83d68c 100644
--- a/cache.h
+++ b/cache.h
@@ -1045,7 +1045,6 @@ extern void setup_pager(void);
 extern const char *pager_program;
 extern int pager_in_use(void);
 extern int pager_use_color;
-extern int term_columns();
 
 extern const char *editor_program;
 extern const char *askpass_program;
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 1344208..62a479b 100644
--- a/help.c
+++ b/help.c
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "builtin.h"
 #include "exec_cmd.h"
+#include "column.h"
 #include "levenshtein.h"
 #include "help.h"
 #include "common-cmds.h"
diff --git a/pager.c b/pager.c
index ad447cf..e6f7d86 100644
--- a/pager.c
+++ b/pager.c
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "run-command.h"
 #include "sigchain.h"
+#include "column.h"
 
 #ifndef DEFAULT_PAGER
 #define DEFAULT_PAGER "less"
@@ -14,6 +15,21 @@
 static int spawned_pager;
 static int max_columns;
 
+#ifdef TIOCGWINSZ
+static int retrieve_terminal_width(void)
+{
+	struct winsize ws;
+	if (ioctl(1, TIOCGWINSZ, &ws))	/* e.g., ENOSYS */
+		return 0;
+	return ws.ws_col;
+}
+#else
+static int retrieve_terminal_width(void)
+{
+	return 0;
+}
+#endif
+
 #ifndef WIN32
 static void pager_preexec(void)
 {
@@ -75,21 +91,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;
 
 	spawned_pager = 1; /* means we are emitting to terminal */
 
-#ifdef TIOCGWINSZ
-	{
-		struct winsize ws;
-		if (!ioctl(1, TIOCGWINSZ, &ws)) {
-			if (ws.ws_col)
-				max_columns = ws.ws_col;
-		}
-	}
-#endif
+	width = retrieve_terminal_width();
+	if (width)
+		max_columns = width;
+
 	/* spawn the pager */
 	pager_argv[0] = pager;
 	pager_process.use_shell = 1;
@@ -127,24 +139,16 @@ int pager_in_use(void)
 	return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
 }
 
-int term_columns()
+int term_columns(void)
 {
 	char *col_string = getenv("COLUMNS");
 	int n_cols;
 
 	if (col_string && (n_cols = atoi(col_string)) > 0)
 		return n_cols;
-
-	else if (spawned_pager && max_columns)
+	if (spawned_pager && max_columns)
 		return max_columns;
-#ifdef TIOCGWINSZ
-	else {
-		struct winsize ws;
-		if (!ioctl(1, TIOCGWINSZ, &ws)) {
-			if (ws.ws_col)
-				return ws.ws_col;
-		}
-	}
-#endif
-	return 80;
+
+	n_cols = retrieve_terminal_width();
+	return n_cols ? n_cols : 80;
 }
-- 
1.7.4
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 3 of 20 in “Column output”
  1. 0/7 Column outputNguyễn Thái Ngọc Duy, Feb 8, 2011
  2. 1/7 Move term_columns() to pager.c and save terminal width before pagerNguyễn Thái Ngọc Duy, Feb 8, 2011
  3. Jonathan NiederFeb 9, 2011
  4. 2/7 Add column layoutNguyễn Thái Ngọc Duy, Feb 8, 2011
  5. Jonathan NiederFeb 9, 2011
  6. Nguyen Thai Ngoc DuyFeb 9, 2011
  7. 3/7 parseopt: OPT_COLUMN to set struct column_layout.modeNguyễn Thái Ngọc Duy, Feb 8, 2011
  8. 4/7 add core.columnNguyễn Thái Ngọc Duy, Feb 8, 2011
  9. 5/7 help: reuse struct column_layoutNguyễn Thái Ngọc Duy, Feb 8, 2011
  10. Jonathan NiederFeb 9, 2011
  11. Nguyen Thai Ngoc DuyFeb 9, 2011
  12. 6/7 tag: support column output with --columnNguyễn Thái Ngọc Duy, Feb 8, 2011
  13. Junio C HamanoFeb 9, 2011
  14. Nguyen Thai Ngoc DuyFeb 10, 2011
  15. Miles BaderFeb 10, 2011
  16. 7/7 branch: support column output with --columnNguyễn Thái Ngọc Duy, Feb 8, 2011
  17. Jeff KingFeb 8, 2011
  18. Nguyen Thai Ngoc DuyFeb 9, 2011
  19. Jonathan NiederFeb 9, 2011
  20. Nguyen Thai Ngoc DuyFeb 9, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.