Re: [RFC PATCH 5/5] show: Simplify new text formatter code

2012-01-25 Thread Austin Clements
Quoth Dmitry Kurochkin on Jan 24 at  5:49 am:
 On Wed, 11 Jan 2012 20:49:33 -0500, Austin Clements amdra...@mit.edu wrote:
  This makes the text formatter take advantage of the new code
  structure.  The previously duplicated header logic is now unified,
  several things that we used to compute repeatedly across different
  callbacks are now computed once, and the code is generally simplified.
  
  Unifying the header logic causes this to format some dates slightly
  differently, so the two affected test cases are updated.
 
 Thanks for these patches, Austin.  They are a definite improvement for
 notmuch show code.  I hope it would soon get pushed to master.  And I
 hope more patches would follow :)
 
 Few minor comments below.
 
  ---
   notmuch-show.c |   84 
  
   test/crypto|2 +-
   test/thread-naming |   16 +-
   3 files changed, 28 insertions(+), 74 deletions(-)
  
  diff --git a/notmuch-show.c b/notmuch-show.c
  index 3241965..1689222 100644
  --- a/notmuch-show.c
  +++ b/notmuch-show.c
  @@ -175,67 +175,42 @@ format_part_text (const void *ctx, mime_node_t *node,
   GMimeObject *meta = node-envelope_part ?
  GMIME_OBJECT (node-envelope_part) : node-part;
   GMimeContentType *content_type = g_mime_object_get_content_type (meta);
  +GMimeContentDisposition *disposition =
  +   g_mime_object_get_content_disposition (meta);
  +notmuch_bool_t attachment = disposition 
  +   strcmp (disposition-disposition, GMIME_DISPOSITION_ATTACHMENT) == 0;
 
 If I did not miss anything, attachment is used only as following:
 
   attachment ? attachment : part
 
 Please make it const char[] and set to attachment or part.

Ooh, good catch.  But I'll do you one better.  I introduced a const
char *part_type that's set to attachment, part, *or* message in
the if structure below.  That eliminates the attachment variable,
moves the disposition variable back down into one of the early if
bodies, and eliminates the entire conditional the figures out what to
print to close the block.

  +notmuch_bool_t leaf = GMIME_IS_PART (node-part);
 
 Please add const where possible to local variables (e.g. attachment, leaf).

Generally I consider these overkill on local non-pointer variables,
but okay.  leaf wound up being the only one I had to add it to.

   int i;
   
   if (node-envelope_file) {
  notmuch_message_t *message = node-envelope_file;
  -   const char *headers[] = {
  -   Subject, From, To, Cc, Bcc, Date
  -   };
  -   const char *name, *value;
  -   unsigned int i;
   
  -   printf (\fmessage{ );
  -   printf (id:%s depth:%d match:%d filename:%s\n,
  +   printf (\fmessage{ id:%s depth:%d match:%d filename:%s\n,
  notmuch_message_get_message_id (message),
  indent,
  notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
  notmuch_message_get_filename (message));
  -
  -   printf (\fheader{\n);
  -
  -   printf (%s\n, _get_one_line_summary (ctx, message));
  -
  -   for (i = 0; i  ARRAY_SIZE (headers); i++) {
  -   name = headers[i];
  -   value = notmuch_message_get_header (message, name);
  -   if (value  strlen (value))
  -   printf (%s: %s\n, name, value);
  -   }
  -   printf (\fheader}\n);
 
 Yay!  Only one header-printing code left :)
 
   } else {
  -   GMimeContentDisposition *disposition = 
  g_mime_object_get_content_disposition (meta);
  const char *cid = g_mime_object_get_content_id (meta);
  +   const char *filename = leaf ?
  +   g_mime_part_get_filename (GMIME_PART (node-part)) : NULL;
   
  -   if (disposition 
  -   strcmp (disposition-disposition, GMIME_DISPOSITION_ATTACHMENT) == 
  0)
  -   {
  -   printf (\fattachment{ ID: %d, node-part_num);
  -
  -   } else {
  -
  -   printf (\fpart{ ID: %d, node-part_num);
  -   }
  -
  -   if (GMIME_IS_PART (node-part))
  -   {
  -   const char *filename = g_mime_part_get_filename (GMIME_PART 
  (node-part));
  -   if (filename)
  -   printf (, Filename: %s, filename);
  -   }
  -
  +   printf (\f%s{ ID: %d, attachment ? attachment : part, 
  node-part_num);
  +   if (filename)
 
 I always forget about it, can we declare variables inside if condition
 like in C++?  I.e.:
 
   if (const char *filename = leaf ? g_mime_part_get_filename (GMIME_PART 
 (node-part)) : NULL)
 
 If yes, I would prefer to use this style where possible.

You can do that in C++?  You definitely can't do that in C.

  +   printf (, Filename: %s, filename);
  if (cid)
  printf (, Content-id: %s, cid);
  -
 
 I would revert blank line changes.  But I do not insist :)

Eh.  I'm generally opposed to drive-by fixes, but this code inherited
a lot of funny formatting that just interrupts the whole flow when the
code is all in one place.

  printf (, Content-type: %s\n, g_mime_content_type_to_string 
  (content_type));
   }
   
  -if 

Re: [RFC PATCH 5/5] show: Simplify new text formatter code

2012-01-24 Thread Dmitry Kurochkin
On Wed, 11 Jan 2012 20:49:33 -0500, Austin Clements amdra...@mit.edu wrote:
 This makes the text formatter take advantage of the new code
 structure.  The previously duplicated header logic is now unified,
 several things that we used to compute repeatedly across different
 callbacks are now computed once, and the code is generally simplified.
 
 Unifying the header logic causes this to format some dates slightly
 differently, so the two affected test cases are updated.

Thanks for these patches, Austin.  They are a definite improvement for
notmuch show code.  I hope it would soon get pushed to master.  And I
hope more patches would follow :)

Few minor comments below.

 ---
  notmuch-show.c |   84 
 
  test/crypto|2 +-
  test/thread-naming |   16 +-
  3 files changed, 28 insertions(+), 74 deletions(-)
 
 diff --git a/notmuch-show.c b/notmuch-show.c
 index 3241965..1689222 100644
 --- a/notmuch-show.c
 +++ b/notmuch-show.c
 @@ -175,67 +175,42 @@ format_part_text (const void *ctx, mime_node_t *node,
  GMimeObject *meta = node-envelope_part ?
   GMIME_OBJECT (node-envelope_part) : node-part;
  GMimeContentType *content_type = g_mime_object_get_content_type (meta);
 +GMimeContentDisposition *disposition =
 + g_mime_object_get_content_disposition (meta);
 +notmuch_bool_t attachment = disposition 
 + strcmp (disposition-disposition, GMIME_DISPOSITION_ATTACHMENT) == 0;

If I did not miss anything, attachment is used only as following:

  attachment ? attachment : part

Please make it const char[] and set to attachment or part.

 +notmuch_bool_t leaf = GMIME_IS_PART (node-part);

Please add const where possible to local variables (e.g. attachment, leaf).

  int i;
  
  if (node-envelope_file) {
   notmuch_message_t *message = node-envelope_file;
 - const char *headers[] = {
 - Subject, From, To, Cc, Bcc, Date
 - };
 - const char *name, *value;
 - unsigned int i;
  
 - printf (\fmessage{ );
 - printf (id:%s depth:%d match:%d filename:%s\n,
 + printf (\fmessage{ id:%s depth:%d match:%d filename:%s\n,
   notmuch_message_get_message_id (message),
   indent,
   notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_MATCH),
   notmuch_message_get_filename (message));
 -
 - printf (\fheader{\n);
 -
 - printf (%s\n, _get_one_line_summary (ctx, message));
 -
 - for (i = 0; i  ARRAY_SIZE (headers); i++) {
 - name = headers[i];
 - value = notmuch_message_get_header (message, name);
 - if (value  strlen (value))
 - printf (%s: %s\n, name, value);
 - }
 - printf (\fheader}\n);

Yay!  Only one header-printing code left :)

  } else {
 - GMimeContentDisposition *disposition = 
 g_mime_object_get_content_disposition (meta);
   const char *cid = g_mime_object_get_content_id (meta);
 + const char *filename = leaf ?
 + g_mime_part_get_filename (GMIME_PART (node-part)) : NULL;
  
 - if (disposition 
 - strcmp (disposition-disposition, GMIME_DISPOSITION_ATTACHMENT) == 
 0)
 - {
 - printf (\fattachment{ ID: %d, node-part_num);
 -
 - } else {
 -
 - printf (\fpart{ ID: %d, node-part_num);
 - }
 -
 - if (GMIME_IS_PART (node-part))
 - {
 - const char *filename = g_mime_part_get_filename (GMIME_PART 
 (node-part));
 - if (filename)
 - printf (, Filename: %s, filename);
 - }
 -
 + printf (\f%s{ ID: %d, attachment ? attachment : part, 
 node-part_num);
 + if (filename)

I always forget about it, can we declare variables inside if condition
like in C++?  I.e.:

  if (const char *filename = leaf ? g_mime_part_get_filename (GMIME_PART 
(node-part)) : NULL)

If yes, I would prefer to use this style where possible.

 + printf (, Filename: %s, filename);
   if (cid)
   printf (, Content-id: %s, cid);
 -

I would revert blank line changes.  But I do not insist :)

   printf (, Content-type: %s\n, g_mime_content_type_to_string 
 (content_type));
  }
  
 -if (node-envelope_part) {
 +if (GMIME_IS_MESSAGE (node-part)) {

This condition is repeated at least twice.  Please consider moving the
message variable below to the top level and using it in the conditions.

Regards,
  Dmitry

   GMimeMessage *message = GMIME_MESSAGE (node-part);
   InternetAddressList *recipients;
   const char *recipients_string;
  
   printf (\fheader{\n);
 + if (node-envelope_file)
 + printf (%s\n, _get_one_line_summary (ctx, node-envelope_file));
   printf (Subject: %s\n, g_mime_message_get_subject (message));
   printf (From: %s\n, g_mime_message_get_sender (message));
   recipients = g_mime_message_get_recipients (message, 
 GMIME_RECIPIENT_TYPE_TO);
 @@ -248,9 +223,11 @@ format_part_text (const void *ctx, mime_node_t *node,