From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 06/12] Add helpers to redirect stdout to "git column"
Date: Sun, 7 Mar 2010 19:09:39 +0700 [thread overview]
Message-ID: <1267963785-473-7-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1267963785-473-1-git-send-email-pclouds@gmail.com>
While the columnizer can be used directly, it requires output code
change. The helpers instead redirect all output to "git column" and
let "git column" do all the layout work. This keeps code change
minimum. All we need is:
start_columnizer();
/* do some complex computation and output to stdout */
stop_columnizer();
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
column.h | 2 +
| 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 65 insertions(+), 0 deletions(-)
diff --git a/column.h b/column.h
index 28122e0..6eb19e3 100644
--- a/column.h
+++ b/column.h
@@ -21,3 +21,5 @@ struct columnizer {
};
extern int feed_columnizer(struct columnizer *cp, char *cell);
+extern int start_columnizer(const char **argv);
+extern int stop_columnizer();
--git a/pager.c b/pager.c
index ad447cf..9df727d 100644
--- a/pager.c
+++ b/pager.c
@@ -13,6 +13,8 @@
static int spawned_pager;
static int max_columns;
+static int fd_out = -1;
+static struct child_process column_process;
#ifndef WIN32
static void pager_preexec(void)
@@ -148,3 +150,64 @@ int term_columns()
#endif
return 80;
}
+
+int start_columnizer(const char **argv)
+{
+ int new_argc, argc = 0;
+ const char **column_argv;
+ char columns_param[32];
+
+ if (fd_out != -1)
+ return -1;
+
+ if (argv) {
+ while (argv[argc])
+ argc++;
+ }
+ new_argc = argc + 1; /* argv[0] = "column" */
+ if (spawned_pager && max_columns) {
+ sprintf(columns_param, "--width=%d", max_columns);
+ new_argc++;
+ }
+
+ column_argv = xmalloc(sizeof(*column_argv)*(new_argc+1));
+ column_argv[0] = "column";
+ if (spawned_pager && max_columns)
+ column_argv[1] = columns_param;
+ if (argv)
+ memcpy(column_argv + (new_argc-argc), argv, argc*sizeof(*argv));
+ column_argv[new_argc] = 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 = column_argv;
+
+ if (start_command(&column_process)) {
+ free(column_argv);
+ return -2;
+ }
+ free(column_argv);
+
+ fd_out = dup(1);
+ close(1);
+ dup2(column_process.in, 1);
+ close(column_process.in);
+ return 0;
+}
+
+int stop_columnizer()
+{
+ 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;
+}
--
1.7.0.1.370.gd3c5
next prev parent reply other threads:[~2010-03-07 12:13 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-03-07 12:09 [PATCH 00/12] Support columinized output in tag/branch/ls-files/grep Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 01/12] Move term_columns() to pager.c Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 02/12] setup_pager(): save terminal width before redirecting stdout Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 03/12] Add columnizer Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 04/12] help: use columnizer Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 05/12] Add builtin command "column" Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` Nguyễn Thái Ngọc Duy [this message]
2010-03-07 12:09 ` [PATCH 07/12] add core.columns Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 08/12] tag: support column output with --columns Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 09/12] branch: " Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 10/12] ls-files: " Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 11/12] grep: do not return early in cmd_grep() if there is no error Nguyễn Thái Ngọc Duy
2010-03-07 12:09 ` [PATCH 12/12] grep: support column output with --columns Nguyễn Thái Ngọc Duy
2010-03-08 14:08 ` [PATCH 00/12] Support columinized output in tag/branch/ls-files/grep René Scharfe
2010-03-08 14:32 ` Nguyen Thai Ngoc Duy
2010-03-09 16:49 ` René Scharfe
2010-03-10 0:27 ` Nguyen Thai Ngoc Duy
2010-03-10 7:26 ` Johannes Sixt
2010-03-10 12:12 ` Nguyen Thai Ngoc Duy
2010-03-11 21:13 ` René Scharfe
2010-03-12 4:22 ` Nguyen Thai Ngoc Duy
2010-03-08 23:08 ` Junio C Hamano
2010-03-09 2:06 ` Nguyen Thai Ngoc Duy
2010-03-09 2:14 ` Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1267963785-473-7-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).