mirror of https://github.com/djcb/mu.git
* cleanup, improve, refactor message summary
This commit is contained in:
parent
aa1c7e9831
commit
c09cb1303b
|
@ -18,10 +18,12 @@
|
|||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mu-msg-str.h"
|
||||
#include "mu-msg-flags.h"
|
||||
|
||||
|
||||
const char*
|
||||
mu_msg_str_date_s (time_t t)
|
||||
{
|
||||
|
@ -106,3 +108,44 @@ mu_msg_str_prio (MuMsgPrio prio)
|
|||
}
|
||||
|
||||
|
||||
char*
|
||||
mu_msg_str_summarize (const char* str, size_t max_lines)
|
||||
{
|
||||
char *summary;
|
||||
size_t nl_seen;
|
||||
unsigned i,j;
|
||||
gboolean last_was_blank;
|
||||
|
||||
g_return_val_if_fail (str, NULL);
|
||||
g_return_val_if_fail (max_lines > 0, NULL);
|
||||
|
||||
/* len for summary <= original len */
|
||||
summary = g_new (gchar, strlen(str) + 1);
|
||||
|
||||
/* copy the string up to max_lines lines, replace CR/LF/tab with
|
||||
* single space */
|
||||
for (i = j = 0, nl_seen = 0, last_was_blank = TRUE;
|
||||
nl_seen < max_lines && str[i] != '\0'; ++i) {
|
||||
|
||||
if (str[i] == '\n' || str[i] == '\r' ||
|
||||
str[i] == '\t' || str[i] == ' ' ) {
|
||||
|
||||
if (str[i] == '\n')
|
||||
++nl_seen;
|
||||
|
||||
/* no double-blanks or blank at end of str */
|
||||
if (!last_was_blank && str[i+1] != '\0')
|
||||
summary[j++] = ' ';
|
||||
|
||||
last_was_blank = TRUE;
|
||||
} else {
|
||||
|
||||
summary[j++] = str[i];
|
||||
last_was_blank = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
summary[j] = '\0';
|
||||
return summary;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||
**
|
||||
** This program is free software; you can redistribute it and/or modify
|
||||
** it under the terms of the GNU General Public License as published by
|
||||
|
@ -26,7 +26,7 @@
|
|||
#include "mu-msg.h"
|
||||
#include "mu-msg-flags.h"
|
||||
|
||||
/**
|
||||
/**
|
||||
* get a display string for a given time_t;
|
||||
* use the preferred date/time for the current locale
|
||||
* (ie., '%c' in strftime).
|
||||
|
@ -44,7 +44,7 @@ const char* mu_msg_str_date_s (time_t t) G_GNUC_CONST;
|
|||
char* mu_msg_str_date (time_t t) G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
|
||||
/**
|
||||
/**
|
||||
* get a display size for a given size_t; uses M for sizes >
|
||||
* 1000*1000, k for smaller sizes. Note: this function use the
|
||||
* 10-based SI units, _not_ the powers-of-2 based ones.
|
||||
|
@ -61,7 +61,7 @@ char* mu_msg_str_date (time_t t) G_GNUC_WARN_UNUSED_RESULT;
|
|||
const char* mu_msg_str_size_s (size_t s) G_GNUC_CONST;
|
||||
char* mu_msg_str_size (size_t s) G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
/**
|
||||
/**
|
||||
* get a display string for a given set of flags, OR'ed in
|
||||
* @param flags; one character per flag:
|
||||
* D=draft,F=flagged,N=new,P=passed,R=replied,S=seen,T=trashed
|
||||
|
@ -80,7 +80,7 @@ const char* mu_msg_str_flags_s (MuMsgFlags flags) G_GNUC_CONST;
|
|||
char* mu_msg_str_flags (MuMsgFlags flags) G_GNUC_WARN_UNUSED_RESULT;
|
||||
|
||||
|
||||
/**
|
||||
/**
|
||||
* get a display string for a message priority; either
|
||||
* high, low or normal
|
||||
*
|
||||
|
@ -92,4 +92,15 @@ char* mu_msg_str_flags (MuMsgFlags flags) G_GNUC_WARN_UNUSED_RESULT;
|
|||
const char* mu_msg_str_prio (MuMsgPrio prio) G_GNUC_CONST;
|
||||
|
||||
|
||||
/**
|
||||
* get a 'summary' of the string, ie. the first /n/ lines of the
|
||||
* strings, with all newlines removed, replaced by single spaces
|
||||
*
|
||||
* @param str the source string
|
||||
* @param max_lines the maximum number of lines to include in the summary
|
||||
*
|
||||
* @return a newly allocated string with the summary. use g_free to free it.
|
||||
*/
|
||||
char* mu_msg_str_summarize (const char* str, size_t max_lines);
|
||||
|
||||
#endif /*__MU_MSG_STR_H__*/
|
||||
|
|
42
src/mu-msg.c
42
src/mu-msg.c
|
@ -27,6 +27,7 @@
|
|||
#include <ctype.h>
|
||||
|
||||
#include "mu-util.h"
|
||||
#include "mu-msg-str.h"
|
||||
|
||||
#include "mu-msg-priv.h" /* include before mu-msg.h */
|
||||
#include "mu-msg.h"
|
||||
|
@ -706,44 +707,6 @@ mu_msg_get_body_text (MuMsg *msg)
|
|||
return msg->_fields[TEXT_FIELD] = get_body (msg, FALSE);
|
||||
}
|
||||
|
||||
/* maybe move to str functions file? */
|
||||
static char*
|
||||
summarize (const char* str, size_t max_lines)
|
||||
{
|
||||
char *summary;
|
||||
size_t nl_seen;
|
||||
int i;
|
||||
|
||||
if (!str || max_lines == 0)
|
||||
return NULL;
|
||||
|
||||
/* len for summary <= original len */
|
||||
summary = g_new (gchar, strlen(str) + 1);
|
||||
|
||||
/* copy the string up to max_lines lines, replace CR/LF/tab with
|
||||
* single space */
|
||||
for (i = 0, nl_seen = 0; nl_seen < max_lines && str[i] != '\0'; ++i) {
|
||||
switch (str[i]) {
|
||||
case '\n':
|
||||
++nl_seen;
|
||||
summary[i] = ' ';
|
||||
break;
|
||||
case '\r':
|
||||
case '\t':
|
||||
summary[i] = ' ';
|
||||
break;
|
||||
default:
|
||||
summary[i] = str[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* FIXME: word-wrap the string */
|
||||
|
||||
summary[i] = '\0';
|
||||
return summary;
|
||||
}
|
||||
|
||||
|
||||
const char*
|
||||
mu_msg_get_summary (MuMsg *msg, size_t max_lines)
|
||||
{
|
||||
|
@ -761,7 +724,8 @@ mu_msg_get_summary (MuMsg *msg, size_t max_lines)
|
|||
if (!body)
|
||||
return NULL; /* there was no text body */
|
||||
|
||||
return msg->_fields[SUMMARY_FIELD] = summarize (body, max_lines);
|
||||
return msg->_fields[SUMMARY_FIELD] =
|
||||
mu_msg_str_summarize (body, max_lines);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -164,7 +164,7 @@ test_mu_query_04 (void)
|
|||
g_assert_cmpstr (mu_msg_get_subject(msg),==,
|
||||
"Greetings from Lothlórien");
|
||||
g_assert_cmpstr (mu_msg_get_summary(msg,5),==,
|
||||
" Let's write some fünkÿ text using umlauts. Foo. ");
|
||||
"Let's write some fünkÿ text using umlauts. Foo.");
|
||||
|
||||
mu_msg_destroy (msg);
|
||||
mu_msg_iter_destroy (iter);
|
||||
|
@ -189,3 +189,4 @@ main (int argc, char *argv[])
|
|||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue