Hi.

I've done some corrections for printing "newline" and "wrap" indicators.
Please review the attached patch.


2014-04-11 0:14 GMT+04:00 Sergey Muraviov <sergey.k.murav...@gmail.com>:

> Hi.
>
> Thanks for your tests.
>
> I've fixed problem with headers, but got new one with data.
> I'll try to solve it tomorrow.
>
>
> 2014-04-10 18:45 GMT+04:00 Greg Stark <st...@mit.edu>:
>
> Ok, So I've hacked on this a bit. Below is a test case showing the
>> problems I've found.
>>
>> 1) It isn't using the "newline" and "wrap" indicators or dividing lines.
>>
>> 2) The header is not being displayed properly when it contains a newline.
>>
>> I can hack in the newline and wrap indicators but the header
>> formatting requires reworking the logic a bit. The header and data
>> need to be stepped through in parallel rather than having a loop to
>> handle the wrapping within the handling of a single line. I don't
>> really have time for that today but if you can get to it that would be
>> fine,
>>
>
>
>
> --
> Best regards,
> Sergey Muraviov
>



-- 
Best regards,
Sergey Muraviov
From 5e0f44994d04a81523920a78d3a35603e919170c Mon Sep 17 00:00:00 2001
From: Sergey Muraviov <sergey.k.murav...@gmail.com>
Date: Fri, 11 Apr 2014 11:03:41 +0400
Subject: [PATCH] Using "newline" and "wrap" indicators

---
 src/bin/psql/print.c | 130 +++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 110 insertions(+), 20 deletions(-)

diff --git a/src/bin/psql/print.c b/src/bin/psql/print.c
index 79fc43e..6463162 100644
--- a/src/bin/psql/print.c
+++ b/src/bin/psql/print.c
@@ -1234,13 +1234,56 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
 			fprintf(fout, "%s\n", cont->title);
 	}
 
+		if (cont->opt->format == PRINT_WRAPPED)
+	{
+		int output_columns = 0;
+
+		/*
+		 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
+		 */
+		if (cont->opt->columns > 0)
+			output_columns = cont->opt->columns;
+		else
+		{
+			if (cont->opt->env_columns > 0)
+				output_columns = cont->opt->env_columns;
+#ifdef TIOCGWINSZ
+			else
+			{
+				struct winsize screen_size;
+
+				if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
+					output_columns = screen_size.ws_col;
+			}
+#endif
+		}
+
+		output_columns -= hwidth;
+
+		if (opt_border == 0)
+			output_columns -= 1;
+		else
+		{
+			output_columns -= 3; /* -+- */
+
+			if (opt_border > 1)
+				output_columns -= 4; /* +--+ */
+		}
+
+		if (output_columns > 0 && dwidth > output_columns)
+			dwidth = output_columns;
+	}
+
 	/* print records */
 	for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
 	{
 		printTextRule pos;
-		int			line_count,
+		int			dline,
+					hline,
 					dcomplete,
-					hcomplete;
+					hcomplete,
+					offset,
+					chars_to_output;
 
 		if (cancel_pressed)
 			break;
@@ -1270,48 +1313,95 @@ print_aligned_vertical(const printTableContent *cont, FILE *fout)
 		pg_wcsformat((const unsigned char *) *ptr, strlen(*ptr), encoding,
 					 dlineptr, dheight);
 
-		line_count = 0;
+		dline = hline = 0;
 		dcomplete = hcomplete = 0;
+		offset = 0;
+		chars_to_output = dlineptr[dline].width;
 		while (!dcomplete || !hcomplete)
 		{
+			/* Left border */
 			if (opt_border == 2)
 				fprintf(fout, "%s ", dformat->leftvrule);
+
+			/* Header */
 			if (!hcomplete)
 			{
-				fprintf(fout, "%-s%*s", hlineptr[line_count].ptr,
-						hwidth - hlineptr[line_count].width, "");
+				int swidth = hwidth - hlineptr[hline].width - 1;
+				fprintf(fout, "%-s", hlineptr[hline].ptr);
+				if (swidth > 0) /* spacer */
+					fprintf(fout, "%*s", swidth, "");
 
-				if (!hlineptr[line_count + 1].ptr)
+				if (!hlineptr[hline + 1].ptr)
+				{
+					fputs(" ", fout);
 					hcomplete = 1;
+				}
+				else 
+				{
+					fputs(format->nl_right, fout);
+					hline++;
+				}
 			}
 			else
-				fprintf(fout, "%*s", hwidth, "");
+				fprintf(fout, "%*s", hwidth + 1, "");
 
+			/* Separator */
 			if (opt_border > 0)
-				fprintf(fout, " %s ", dformat->midvrule);
-			else
-				fputc(' ', fout);
+				fprintf(fout, "%s", dformat->midvrule);
 
+			/* Data */
 			if (!dcomplete)
 			{
-				if (opt_border < 2)
-					fprintf(fout, "%s\n", dlineptr[line_count].ptr);
+				int target_width,
+					bytes_to_output,
+					swidth;
+
+				fputs(!dcomplete && !offset? " ": format->wrap_left, fout);
+
+				target_width = dwidth;
+				bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
+												   &target_width, encoding);
+				fputnbytes(fout, (char *)(dlineptr[dline].ptr + offset),
+						   bytes_to_output);
+
+				chars_to_output -= target_width;
+				offset += bytes_to_output;
+
+				/* spacer */
+				swidth = dwidth - target_width;
+				if (swidth > 0)
+					fprintf(fout, "%*s", swidth, "");
+
+				if (!chars_to_output)
+				{
+					if (!dlineptr[dline + 1].ptr)
+					{
+						fputs(" ", fout);
+						dcomplete = 1;
+					}
+					else
+					{
+						fputs(format->nl_right, fout);
+						dline++;
+						offset = 0;
+						chars_to_output = dlineptr[dline].width;
+					}
+				}
 				else
-					fprintf(fout, "%-s%*s %s\n", dlineptr[line_count].ptr,
-							dwidth - dlineptr[line_count].width, "",
-							dformat->rightvrule);
+					fputs(format->wrap_right, fout);
+
+				if (opt_border == 2)
+					fputs(dformat->rightvrule, fout);
 
-				if (!dlineptr[line_count + 1].ptr)
-					dcomplete = 1;
+				fputs("\n", fout);
 			}
 			else
 			{
 				if (opt_border < 2)
-					fputc('\n', fout);
+					fputs("\n", fout);
 				else
-					fprintf(fout, "%*s %s\n", dwidth, "", dformat->rightvrule);
+					fprintf(fout, "%*s  %s\n", dwidth, "", dformat->rightvrule);
 			}
-			line_count++;
 		}
 	}
 
-- 
1.8.5.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