From 330ca4d3cbd702f4f0e7689a1da4f77945f768e0 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Wed, 9 Nov 2011 18:29:22 +0100 Subject: Use a dynamic method to print appointments to stdout Add a flexible helper function print_apoint() and use it whenever we print appointments to stdout. This reduces the number of copy-pasted code and eventually allows for specifying custom format strings. Following format specifiers are supported: * s: Print the start time of the appointment as UNIX time stamp * S: Print the start time of the appointment using the "hh:mm" format * d: Print the duration of the appointment in seconds * e: Print the end time of the appointment as UNIX time stamp * E: Print the end time of the appointment using the "hh:mm" format * m: Print the description of the item * n: Print the name of the note file belonging to the item Signed-off-by: Lukas Fleischer --- src/utils.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 3c7f595..4628798 100644 --- a/src/utils.c +++ b/src/utils.c @@ -953,3 +953,52 @@ press_any_key (void) fflush (stdin); fputs ("\r\n", stdout); } + +/* Print a formatted appointment to stdout. */ +void +print_apoint (const char *format, long day, struct apoint *apt) +{ + const char *p; + char str_start[HRMIN_SIZE], str_end[HRMIN_SIZE]; + + apoint_sec2str (apt, day, str_start, str_end); + + for (p = format; *p; p++) + { + if (*p == '%') { + p++; + switch (*p) + { + case 's': + printf ("%ld", apt->start); + break; + case 'S': + printf ("%s", str_start); + break; + case 'd': + printf ("%ld", apt->dur); + break; + case 'e': + printf ("%ld", apt->start + apt->dur); + break; + case 'E': + printf ("%s", str_end); + break; + case 'm': + printf ("%s", apt->mesg); + break; + case 'n': + printf ("%s", apt->note); + break; + case '\0': + return; + break; + default: + putchar ('?'); + break; + } + } + else + putchar (*p); + } +} -- cgit v1.2.3-54-g00ecf