Convert print_aligned_vertical, and its helper function, to use
table formatting in place of hardcoded ASCII characters.

Signed-off-by: Roger Leigh <rle...@debian.org>
---
 src/bin/psql/print.c |  141 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 87 insertions(+), 54 deletions(-)

diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 84f6bdc..e4e9f01 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -995,6 +995,68 @@ print_aligned_text(const printTableContent *cont, const 
printTextFormat *format,
                ClosePager(fout);
 }
 
+static inline void
+print_aligned_vertical_line(const printTableContent *cont,
+                           unsigned long record,
+                           unsigned int hwidth,
+                           unsigned int dwidth,
+                           printTextRule pos,
+                           const printTextFormat *format,
+                           FILE *fout)
+{
+       unsigned short  opt_border = cont->opt->border;
+       unsigned int    i;
+       int             reclen = 0;
+       const printTextLineFormat       *lformat = &format->lrule[pos];
+
+       if (opt_border == 2)
+       {
+               fputs(lformat->leftvrule, fout);
+               fputs(format->hrule, fout);
+       }
+       else
+               fputs(format->hrule, fout);
+
+       if (record)
+       {
+               if (opt_border == 0)
+                       reclen = fprintf(fout, "* Record %lu", record);
+               else
+                       reclen = fprintf(fout, "[ RECORD %lu ]", record);
+       }
+       if (opt_border != 2)
+               reclen++;
+       if (reclen < 0)
+               reclen = 0;
+       for (i = reclen; i < hwidth; i++)
+               fputs(opt_border > 0 ? format->hrule : " ", fout);
+       reclen -= hwidth;
+
+       if (opt_border > 0)
+       {
+               if (--reclen <= 0)
+                       fputs(format->hrule, fout);
+               if (--reclen <= 0)
+                       fputs(lformat->midvrule, fout);
+               if (--reclen <= 0)
+                       fputs(format->hrule, fout);
+       }
+       else
+       {
+               if (reclen-- > 0)
+                       fputs(" ", fout);
+       }
+       if (reclen < 0)
+               reclen = 0;
+       for (i = reclen; i < dwidth; i++)
+               fputs(opt_border > 0 ? format->hrule : " ", fout);
+       if (opt_border == 2)
+       {
+               fputs(format->hrule, fout);
+               fputs(lformat->rightvrule, fout);
+       }
+       fputc('\n', fout);
+}
 
 static void
 print_aligned_vertical(const printTableContent *cont,
@@ -1014,7 +1076,6 @@ print_aligned_vertical(const printTableContent *cont,
                                dheight = 1,
                                hformatsize = 0,
                                dformatsize = 0;
-       char       *divider;
        struct lineptr *hlineptr,
                           *dlineptr;
 
@@ -1082,21 +1143,6 @@ print_aligned_vertical(const printTableContent *cont,
        dlineptr->ptr = pg_local_malloc(dformatsize);
        hlineptr->ptr = pg_local_malloc(hformatsize);
 
-       /* make horizontal border */
-       divider = pg_local_malloc(hwidth + dwidth + 10);
-       divider[0] = '\0';
-       if (opt_border == 2)
-               strcat(divider, "+-");
-       for (i = 0; i < hwidth; i++)
-               strcat(divider, opt_border > 0 ? "-" : " ");
-       if (opt_border > 0)
-               strcat(divider, "-+-");
-       else
-               strcat(divider, " ");
-       for (i = 0; i < dwidth; i++)
-               strcat(divider, opt_border > 0 ? "-" : " ");
-       if (opt_border == 2)
-               strcat(divider, "-+");
 
        if (cont->opt->start_table)
        {
@@ -1108,40 +1154,25 @@ print_aligned_vertical(const printTableContent *cont,
        /* print records */
        for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
        {
-               int                     line_count,
-                                       dcomplete,
-                                       hcomplete;
+         int   line_count,
+           dcomplete,
+           hcomplete;
+               printTextRule pos = PRINT_RULE_MIDDLE;
+               if (i == 0)
+                       pos = PRINT_RULE_TOP;
+               else if (!(*(ptr+1)))
+                       pos = PRINT_RULE_BOTTOM;
+
+               if (cancel_pressed)
+                       break;
 
                if (i % cont->ncolumns == 0)
                {
-                       if (cancel_pressed)
-                               break;
-                       if (!opt_tuples_only)
-                       {
-                               char            record_str[64];
-                               size_t          record_str_len;
-
-                               if (opt_border == 0)
-                                       snprintf(record_str, 64, "* Record 
%lu", record++);
-                               else
-                                       snprintf(record_str, 64, "[ RECORD %lu 
]", record++);
-                               record_str_len = strlen(record_str);
-
-                               if (record_str_len + opt_border > 
strlen(divider))
-                                       fprintf(fout, "%.*s%s\n", opt_border, 
divider, record_str);
-                               else
-                               {
-                                       char       *div_copy = 
pg_strdup(divider);
-
-                                       strncpy(div_copy + opt_border, 
record_str, record_str_len);
-                                       fprintf(fout, "%s\n", div_copy);
-                                       free(div_copy);
-                               }
-                       }
+                       if (!opt_tuples_only)
+                               print_aligned_vertical_line(cont, record++, 
hwidth, dwidth, pos, format, fout);
                        else if (i != 0 || !cont->opt->start_table || 
opt_border == 2)
-                               fprintf(fout, "%s\n", divider);
+                               print_aligned_vertical_line(cont, 0, hwidth, 
dwidth, pos, format, fout);
                }
-
                /* Format the header */
                pg_wcsformat((unsigned char *) cont->headers[i % 
cont->ncolumns],
                                         strlen(cont->headers[i % 
cont->ncolumns]),
@@ -1155,7 +1186,10 @@ print_aligned_vertical(const printTableContent *cont,
                while (!dcomplete || !hcomplete)
                {
                        if (opt_border == 2)
-                               fputs("| ", fout);
+                       {
+                               fputs(format->vrule, fout);
+                               fputc(' ', fout);
+                       }
                        if (!hcomplete)
                        {
                                fprintf(fout, "%-s%*s", 
hlineptr[line_count].ptr,
@@ -1168,7 +1202,7 @@ print_aligned_vertical(const printTableContent *cont,
                                fprintf(fout, "%*s", hwidth, "");
 
                        if (opt_border > 0)
-                               fprintf(fout, " %c ", (line_count == 0) ? '|' : 
':');
+                               fprintf(fout, " %s ", (line_count == 0) ? 
format->vrule : ":");
                        else
                                fputs(" ", fout);
 
@@ -1181,8 +1215,8 @@ print_aligned_vertical(const printTableContent *cont,
                                        if (opt_border < 2)
                                                fprintf(fout, "%s\n", my_cell);
                                        else
-                                               fprintf(fout, "%-s%*s |\n", 
my_cell,
-                                                               (int) (dwidth - 
strlen(my_cell)), "");
+                                               fprintf(fout, "%-s%*s %s\n", 
my_cell,
+                                                               (int) (dwidth - 
strlen(my_cell)), "", format->vrule);
                                        free(my_cell);
                                }
                                else
@@ -1190,8 +1224,8 @@ print_aligned_vertical(const printTableContent *cont,
                                        if (opt_border < 2)
                                                fprintf(fout, "%s\n", 
dlineptr[line_count].ptr);
                                        else
-                                               fprintf(fout, "%-s%*s |\n", 
dlineptr[line_count].ptr,
-                                                               dwidth - 
dlineptr[line_count].width, "");
+                                               fprintf(fout, "%-s%*s %s\n", 
dlineptr[line_count].ptr,
+                                                               dwidth - 
dlineptr[line_count].width, "", format->vrule);
                                }
 
                                if (!dlineptr[line_count + 1].ptr)
@@ -1202,7 +1236,7 @@ print_aligned_vertical(const printTableContent *cont,
                                if (opt_border < 2)
                                        fputc('\n', fout);
                                else
-                                       fprintf(fout, "%*s |\n", dwidth, "");
+                                       fprintf(fout, "%*s %s\n", dwidth, "", 
format->vrule);
                        }
                        line_count++;
                }
@@ -1211,7 +1245,7 @@ print_aligned_vertical(const printTableContent *cont,
        if (cont->opt->stop_table)
        {
                if (opt_border == 2 && !cancel_pressed)
-                       fprintf(fout, "%s\n", divider);
+                       print_aligned_vertical_line(cont, 0, hwidth, dwidth, 
PRINT_RULE_BOTTOM, format, fout);
 
                /* print footers */
                if (!opt_tuples_only && cont->footers != NULL && 
!cancel_pressed)
@@ -1227,7 +1261,6 @@ print_aligned_vertical(const printTableContent *cont,
                fputc('\n', fout);
        }
 
-       free(divider);
        free(hlineptr->ptr);
        free(dlineptr->ptr);
        free(hlineptr);
-- 
1.6.3.3


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to