[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] fix new format when printing multiple lines
From: |
Sami Kerola |
Subject: |
[PATCH] fix new format when printing multiple lines |
Date: |
Wed, 4 Dec 2013 22:02:34 +0000 |
* Makefile.am: add test
* src/hello.c: add new function print_box()
* tests/multiline-box-1: test multiline printing
---
Makefile.am | 1 +
src/hello.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++-----
tests/multiline-box-1 | 41 ++++++++++++++++++++++++++++++++++++
3 files changed, 95 insertions(+), 5 deletions(-)
create mode 100755 tests/multiline-box-1
diff --git a/Makefile.am b/Makefile.am
index 9b45d9b..f7aa0fb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -54,6 +54,7 @@ TESTS = \
tests/greeting-2 \
tests/hello-1 \
tests/last-1 \
+ tests/multiline-box-1 \
tests/traditional-1
EXTRA_DIST += $(TESTS)
diff --git a/src/hello.c b/src/hello.c
index 8c36915..b033a1d 100644
--- a/src/hello.c
+++ b/src/hello.c
@@ -40,6 +40,7 @@ typedef enum
/* Forward declarations. */
static void print_help (void);
static void print_version (void);
+static void print_box (wchar_t * mb_greeting);
static void print_frame (const size_t len);
int
@@ -120,17 +121,64 @@ main (int argc, char *argv[])
if (g != greet_new)
wprintf (L"%ls\n", mb_greeting);
else
- {
- print_frame (len);
- wprintf (L"| %ls |\n", mb_greeting);
- print_frame (len);
- }
+ print_box(mb_greeting);
free(mb_greeting);
exit (EXIT_SUCCESS);
}
+/* New format message in box. */
+
+void
+print_box (wchar_t * greeting)
+{
+ wchar_t *ignored;
+ size_t longest_line = 0;
+
+ struct parts
+ {
+ wchar_t *str;
+ size_t len;
+ struct parts *next;
+ };
+ struct parts *first, *p;
+
+ first = xmalloc (sizeof (struct parts));
+ first->next = NULL;
+ p = first;
+
+ p->str = wcstok (greeting, L"\n", &ignored);
+ p->len = wcslen (p->str);
+ while (p->str != NULL)
+ {
+ size_t i, len_tabs = 0;
+ for (i = 0; *(p->str + i) != '\0'; i++)
+ {
+ if (*(p->str + i) == '\t')
+ len_tabs += 8 - (len_tabs + 2) % 8;
+ else
+ len_tabs++;
+ }
+ p->len = len_tabs - i;
+ if (longest_line < len_tabs)
+ longest_line = len_tabs;
+ p->next = xmalloc (sizeof (struct parts));
+ p = p->next;
+ p->str = wcstok (NULL, L"\n", &ignored);
+ }
+
+ print_frame (longest_line);
+ for (p = first; p->str != NULL; p = p->next)
+ {
+ wprintf (L"| %-*ls |\n", longest_line - p->len, p->str);
+ free (p);
+ }
+ print_frame (longest_line);
+ free (p);
+}
+
+
/* Print new format upper and lower frame. */
void
diff --git a/tests/multiline-box-1 b/tests/multiline-box-1
new file mode 100755
index 0000000..960fe99
--- /dev/null
+++ b/tests/multiline-box-1
@@ -0,0 +1,41 @@
+#! /bin/sh
+# Test that last greeting option specified is what counts.
+#
+# Copyright 2013 Free Software Foundation, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# are permitted in any medium without royalty provided the copyright
+# notice and this notice are preserved.
+# This script takes one argument.
+
+trap 'rm -fr $tmpfiles' 1 2 3 15
+
+# We force the C locale here, since we are checking normal output,
+# which will be translated.
+
+LANGUAGE=
+LC_ALL=C
+LC_MESSAGES=
+LANG=
+export LANGUAGE LC_ALL LC_MESSAGES LANG
+
+tmpfiles="multiline-box-test1.ok"
+cat <<EOF > multiline-box-test1.ok
++----------+
+| abcd |
+| 1 23 |
++----------+
+EOF
+
+tmpfiles="$tmpfiles multiline-box-test1.out"
+: ${HELLO=hello}
+${HELLO} -n -g "$(printf abcd\\n1\\t23\\n)" | tr -d '\r' \
+| tr -d '\r' >multiline-box-test1.out
+
+: ${DIFF=diff}
+${DIFF} multiline-box-test1.ok multiline-box-test1.out
+result=$?
+
+rm -fr $tmpfiles
+
+exit $result
--
1.8.4.2
- [PATCH] fix new format when printing multiple lines,
Sami Kerola <=
- Re: [PATCH] fix new format when printing multiple lines, Reuben Thomas, 2013/12/04
- Re: [PATCH] fix new format when printing multiple lines, Sami Kerola, 2013/12/04
- Re: [PATCH] fix new format when printing multiple lines, Sami Kerola, 2013/12/10
- Re: [PATCH] fix new format when printing multiple lines, Jim Meyering, 2013/12/22
- Re: [PATCH] fix new format when printing multiple lines, Karl Berry, 2013/12/23
- Re: [PATCH] fix new format when printing multiple lines, Reuben Thomas, 2013/12/23
- Re: [PATCH] fix new format when printing multiple lines, Reuben Thomas, 2013/12/23
- Re: [PATCH] fix new format when printing multiple lines, Karl Berry, 2013/12/23
- Re: [PATCH] fix new format when printing multiple lines, Karl Berry, 2013/12/23
- Re: [PATCH] fix new format when printing multiple lines, Reuben Thomas, 2013/12/23