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

[PATCH v5 11/12] column: support piping stdout to external git-column process

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 4, 2012, 15:59 UTC
Message-ID
<1328371156-4009-12-git-send-email-pclouds@gmail.com>
In-Reply-To
<1328371156-4009-1-git-send-email-pclouds@gmail.com>

For too complicated output handling, it'd be easier to just spawn git-column and redirect stdout to it. This patch provides helpers to do that.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 column.c |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 column.h |    3 ++
 2 files changed, 72 insertions(+), 0 deletions(-)
diff --git a/column.c b/column.c
index c1233a7..4098f03 100644
--- a/column.c
+++ b/column.c
@@ -2,6 +2,7 @@
 #include "column.h"
 #include "string-list.h"
 #include "parse-options.h"
+#include "run-command.h"
 #include "color.h"
 #include "utf8.h"
 
@@ -409,3 +410,71 @@ int parseopt_column_callback(const struct option *opt,
 	*mode |= COL_ENABLED | COL_ENABLED_SET;
 	return 0;
 }
+
+static int fd_out = -1;
+static struct child_process column_process;
+
+int run_column_filter(int colopts, const struct column_options *opts)
+{
+	const char *av[10];
+	int ret, ac = 0;
+	struct strbuf sb_colopt  = STRBUF_INIT;
+	struct strbuf sb_width   = STRBUF_INIT;
+	struct strbuf sb_padding = STRBUF_INIT;
+
+	if (fd_out != -1)
+		return -1;
+
+	av[ac++] = "column";
+	strbuf_addf(&sb_colopt, "--rawmode=%d", colopts);
+	av[ac++] = sb_colopt.buf;
+	if (opts->width) {
+		strbuf_addf(&sb_width, "--width=%d", opts->width);
+		av[ac++] = sb_width.buf;
+	}
+	if (opts->indent) {
+		av[ac++] = "--indent";
+		av[ac++] = opts->indent;
+	}
+	if (opts->padding) {
+		strbuf_addf(&sb_padding, "--padding=%d", opts->padding);
+		av[ac++] = sb_padding.buf;
+	}
+	av[ac] = NULL;
+
+	fflush(stdout);
+	memset(&column_process, 0, sizeof(column_process));
+	column_process.in = -1;
+	column_process.out = dup(1);
+	column_process.git_cmd = 1;
+	column_process.argv = av;
+
+	ret = start_command(&column_process);
+
+	strbuf_release(&sb_colopt);
+	strbuf_release(&sb_width);
+	strbuf_release(&sb_padding);
+
+	if (ret)
+		return -2;
+
+	fd_out = dup(1);
+	close(1);
+	dup2(column_process.in, 1);
+	close(column_process.in);
+	return 0;
+}
+
+int stop_column_filter()
+{
+	if (fd_out == -1)
+		return -1;
+
+	fflush(stdout);
+	close(1);
+	finish_command(&column_process);
+	dup2(fd_out, 1);
+	close(fd_out);
+	fd_out = -1;
+	return 0;
+}
diff --git a/column.h b/column.h
index 3d2ed52..b9dec64 100644
--- a/column.h
+++ b/column.h
@@ -30,4 +30,7 @@ struct option;
 extern int parseopt_column_callback(const struct option *opt,
 				    const char *arg, int unset);
 
+extern int run_column_filter(int colopts, const struct column_options *);
+extern int stop_column_filter();
+
 #endif
-- 
1.7.8.36.g69ee2
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 41 of 43 in “Column display again”
  1. 00/13 Column display againNguyễn Thái Ngọc Duy, Feb 3, 2012
  2. 01/13 Save terminal width before setting up pagerNguyễn Thái Ngọc Duy, Feb 3, 2012
  3. 02/13 column: add API to print items in columnsNguyễn Thái Ngọc Duy, Feb 3, 2012
  4. Junio C HamanoFeb 3, 2012
  5. Junio C HamanoFeb 3, 2012
  6. 03/13 parseopt: make OPT_INTEGER support hexadecimal as wellNguyễn Thái Ngọc Duy, Feb 3, 2012
  7. Junio C HamanoFeb 3, 2012
  8. Nguyen Thai Ngoc DuyFeb 4, 2012
  9. Junio C HamanoFeb 4, 2012
  10. Nguyen Thai Ngoc DuyFeb 4, 2012
  11. 04/13 Add git-column and column mode parsingNguyễn Thái Ngọc Duy, Feb 3, 2012
  12. 05/13 Stop starting pager recursivelyNguyễn Thái Ngọc Duy, Feb 3, 2012
  13. 06/13 column: add columnar layoutNguyễn Thái Ngọc Duy, Feb 3, 2012
  14. 07/13 column: support columns with different widthsNguyễn Thái Ngọc Duy, Feb 3, 2012
  15. 08/13 column: add column.ui for default column output settingsNguyễn Thái Ngọc Duy, Feb 3, 2012
  16. Junio C HamanoFeb 3, 2012
  17. 09/13 help: reuse print_columns() for help -aNguyễn Thái Ngọc Duy, Feb 3, 2012
  18. Junio C HamanoFeb 3, 2012
  19. 10/13 branch: add --columnNguyễn Thái Ngọc Duy, Feb 3, 2012
  20. Junio C HamanoFeb 3, 2012
  21. Nguyen Thai Ngoc DuyFeb 4, 2012
  22. 11/13 status: add --columnNguyễn Thái Ngọc Duy, Feb 3, 2012
  23. Junio C HamanoFeb 3, 2012
  24. 12/13 column: support piping stdout to external git-column processNguyễn Thái Ngọc Duy, Feb 3, 2012
  25. 13/13 tag: add --columnNguyễn Thái Ngọc Duy, Feb 3, 2012
  26. Junio C HamanoFeb 3, 2012
  27. 00/12 Column displayNguyễn Thái Ngọc Duy, Feb 4, 2012
  28. 01/12 Save terminal width before setting up pagerNguyễn Thái Ngọc Duy, Feb 4, 2012
  29. 02/12 column: add API to print items in columnsNguyễn Thái Ngọc Duy, Feb 4, 2012
  30. 03/12 Add git-column and column mode parsingNguyễn Thái Ngọc Duy, Feb 4, 2012
  31. 03/12 Add git-column and column mode parsingNguyễn Thái Ngọc Duy, Feb 4, 2012
  32. 04/12 Stop starting pager recursivelyNguyễn Thái Ngọc Duy, Feb 4, 2012
  33. 05/12 column: add columnar layoutNguyễn Thái Ngọc Duy, Feb 4, 2012
  34. 06/12 column: support columns with different widthsNguyễn Thái Ngọc Duy, Feb 4, 2012
  35. 07/12 column: add column.ui for default column output settingsNguyễn Thái Ngọc Duy, Feb 4, 2012
  36. 07/12 column: add column.ui for default column output settingsNguyễn Thái Ngọc Duy, Feb 4, 2012
  37. 08/12 help: reuse print_columns() for help -aNguyễn Thái Ngọc Duy, Feb 4, 2012
  38. 09/12 branch: add --columnNguyễn Thái Ngọc Duy, Feb 4, 2012
  39. Junio C HamanoFeb 6, 2012
  40. 10/12 status: add --columnNguyễn Thái Ngọc Duy, Feb 4, 2012
  41. 11/12 column: support piping stdout to external git-column processNguyễn Thái Ngọc Duy, Feb 4, 2012
  42. 12/12 tag: add --columnNguyễn Thái Ngọc Duy, Feb 4, 2012
  43. Junio C HamanoFeb 6, 2012

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.