unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH] ANSI escapes in "new" only when output is a tty
@ 2009-11-23  0:54 Adrian Perez
  2009-11-23  5:12 ` Carl Worth
  0 siblings, 1 reply; 4+ messages in thread
From: Adrian Perez @ 2009-11-23  0:54 UTC (permalink / raw)
  To: notmuch

When running "notmuch new --verbose", ANSI escapes are used. This may not be
desirable when the output of the command is *not* being sent to a terminal
(e.g. when piping output into another command). In that case each file
processed is printed in a new line and ANSI escapes are not used at all.
---
 notmuch-client.h |    1 +
 notmuch-new.c    |   12 ++++++++----
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index f105c8b..4fe182e 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -74,6 +74,7 @@ typedef void (*add_files_callback_t) (notmuch_message_t *message);
 typedef struct {
     int ignore_read_only_directories;
     int saw_read_only_directory;
+    int output_is_a_tty;
     int verbose;
 
     int total_files;
diff --git a/notmuch-new.c b/notmuch-new.c
index a2b30bd..8172b49 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -180,10 +180,15 @@ add_files_recursive (notmuch_database_t *notmuch,
 		state->processed_files++;
 
 		if (state->verbose) {
-		    printf ("\r\033[K%i/%i: %s\r",
+		    if (state->output_is_a_tty)
+			printf("\r\033[K");
+
+		    printf ("%i/%i: %s",
 			    state->processed_files,
 			    state->total_files,
 			    next);
+
+		    putchar((state->output_is_a_tty) ? '\r' : '\n');
 		    fflush (stdout);
 		}
 
@@ -282,9 +287,7 @@ add_files (notmuch_database_t *notmuch,
 	return NOTMUCH_STATUS_FILE_ERROR;
     }
 
-    if (isatty (fileno (stdout)) && ! debugger_is_active ()
-	&& ! state->verbose)
-    {
+    if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
 	/* Setup our handler for SIGALRM */
 	memset (&action, 0, sizeof (struct sigaction));
 	action.sa_handler = handle_sigalrm;
@@ -405,6 +408,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     int i;
 
     add_files_state.verbose = 0;
+    add_files_state.output_is_a_tty = isatty (fileno (stdout));
 
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
 	if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
-- 
1.6.5.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] ANSI escapes in "new" only when output is a tty
  2009-11-23  0:54 [PATCH] ANSI escapes in "new" only when output is a tty Adrian Perez
@ 2009-11-23  5:12 ` Carl Worth
  2009-11-23  7:35   ` Keith Packard
  0 siblings, 1 reply; 4+ messages in thread
From: Carl Worth @ 2009-11-23  5:12 UTC (permalink / raw)
  To: Adrian Perez, notmuch

On Mon, 23 Nov 2009 01:54:35 +0100, Adrian Perez <aperez@igalia.com> wrote:
> When running "notmuch new --verbose", ANSI escapes are used. This may not be
> desirable when the output of the command is *not* being sent to a terminal
> (e.g. when piping output into another command). In that case each file
> processed is printed in a new line and ANSI escapes are not used at
> all.

I've pushed this now.

And I've noticed two things with it:

1. The filenames are often quite long, and if longer than my terminal
width, then I get one line per output rather than one line continually
erased and reprinted. Any good idea about how to fix that without going
too crazy?

2. On any run after the first, the output says "1/0", "2/0", etc.
So we should fix that to either not print the "/0" when total_messages
is 0 *or* we can just make "notmuch new" unconditionally count the
number of files to be processed, (which I've been thinking I wanted to
do anyway).

I'm trying to eliminate any remaining differences between "notmuch new"
on an initial run and "notmuch new" run later. An important reason to
eliminate these differences is that we want to support the user
interrupting the initial database build and then starting it up again
without losing anything.

Besides the file counting, (which would only make such a user lose the
countdown timer on the subsequent runs), the only other thing we have
different about the first run is that it doesn't ignore read-only
directories. And that one's a problem because it means that if someone
(like me) that has a bunch of read-only directories does the following:

      notmuch dump notmuch.dump
      rm -rf ~/mail/notmuch
      notmuch new # then interrupt this with control-C
      notmuch new # oops! This missed most of my mail
      notmuch restore notmuch.dump

Things go badly wrong here as any mail in read-only directories that
didn't get picked up in the first "notmuch new" run will forever more be
ignored by notmuch new.

So that's totally broken and we should come up with a way to fix it.

-Carl

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ANSI escapes in "new" only when output is a tty
  2009-11-23  5:12 ` Carl Worth
@ 2009-11-23  7:35   ` Keith Packard
  2009-11-26 18:27     ` Carl Worth
  0 siblings, 1 reply; 4+ messages in thread
From: Keith Packard @ 2009-11-23  7:35 UTC (permalink / raw)
  To: Carl Worth, Adrian Perez, notmuch

[-- Attachment #1: Type: text/plain, Size: 419 bytes --]

On Mon, 23 Nov 2009 06:12:09 +0100, Carl Worth <cworth@cworth.org> wrote:

> So that's totally broken and we should come up with a way to fix it.

Tracking directory mtime instead of just looking at the read-only bit
would let us skip directories that haven't seen any change in content.

Tracking directory contents would let us easily detect name changes
(add/remove/rename).

-- 
keith.packard@intel.com

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] ANSI escapes in "new" only when output is a tty
  2009-11-23  7:35   ` Keith Packard
@ 2009-11-26 18:27     ` Carl Worth
  0 siblings, 0 replies; 4+ messages in thread
From: Carl Worth @ 2009-11-26 18:27 UTC (permalink / raw)
  To: Keith Packard, Adrian Perez, notmuch

On Sun, 22 Nov 2009 23:35:24 -0800, Keith Packard <keithp@keithp.com> wrote:
> Tracking directory mtime instead of just looking at the read-only bit
> would let us skip directories that haven't seen any change in content.

We've had directory mtime for a long time. I added the check for the
read-only bit afterwards to be able to avoid stats on a whole directory
hierarchy, (since mtime doesn't propagate upward of course).

> Tracking directory contents would let us easily detect name changes
> (add/remove/rename).

Do we want git tree objects for the directories? ...

-Carl

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-11-26 18:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-23  0:54 [PATCH] ANSI escapes in "new" only when output is a tty Adrian Perez
2009-11-23  5:12 ` Carl Worth
2009-11-23  7:35   ` Keith Packard
2009-11-26 18:27     ` Carl Worth

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

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 read-only IMAP folder(s) and NNTP newsgroup(s).