Skip to content

Commit 6950791

Browse files
committed
psql replaces tabelator by spaces when table is printed. So data in tabular
format should not to contains tabulators. But some other data (like function or procedures source code) should not be displayed in tabular format, and then tabelators can be printed. The tabs breaks horizontal movement, so pspg does described replacement too - similary like psql.
1 parent 4dce478 commit 6950791

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/table.c

+49
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ readfile(Options *opts, DataDesc *desc, StateData *state)
678678
bool progressive_load_mode;
679679
LineBuffer *rows;
680680
int clen = -1;
681+
void *tabptr;
681682

682683
#ifdef DEBUG_PIPE
683684

@@ -831,6 +832,54 @@ readfile(Options *opts, DataDesc *desc, StateData *state)
831832
}
832833

833834
read = remove_ansi_escape_seq(line, read);
835+
if ((tabptr = memchr(line, '\t', read)))
836+
{
837+
int tabcount = 1;
838+
void *endptr = line + read - 1;
839+
char *newline, *writeptr, *readptr;
840+
int total_dl = 0;
841+
842+
while ((tabptr = memchr(tabptr + 1, '\t', endptr - tabptr)))
843+
{
844+
tabcount += 1;
845+
}
846+
847+
/* allocate enough memory for new line */
848+
writeptr = newline = smalloc(read + tabcount * 8 + 1);
849+
readptr = line;
850+
851+
while (read > 0)
852+
{
853+
if (*readptr == '\t')
854+
{
855+
do
856+
{
857+
*writeptr++ = ' ';
858+
total_dl += 1;
859+
} while (total_dl % 8 != 0);
860+
861+
read -= 1;
862+
readptr += 1;
863+
}
864+
else
865+
{
866+
int cl = charlen(readptr);
867+
int dl = dsplen(readptr);
868+
869+
total_dl += dl;
870+
memcpy(writeptr, readptr, cl);
871+
writeptr += cl;
872+
readptr += cl;
873+
read -= cl;
874+
}
875+
}
876+
877+
*writeptr = '\0';
878+
879+
free(line);
880+
line = newline;
881+
read = writeptr - newline;
882+
}
834883

835884
/* In query stream node exit when you find row with only GS - Group Separator */
836885
if (opts->querystream && read == 1)

0 commit comments

Comments
 (0)