Handle ANSI Escape codes in column(1).

This commit is contained in:
Jonas 'Sortie' Termansen 2012-10-22 22:46:23 +02:00
parent a041c107d5
commit 7a8687e063
1 changed files with 24 additions and 1 deletions

View File

@ -21,6 +21,7 @@
*******************************************************************************/
#define _SORTIX_SOURCE
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -37,6 +38,28 @@ size_t lineslength = 0;
char** lines = 0;
size_t longestline = 0;
size_t measurelength(const char* line)
{
size_t len = 0;
bool escaped = false;
while ( char c = *line++ )
{
if ( escaped )
{
if ( isalpha(c) )
escaped = false;
continue;
}
if ( c == '\e' )
{
escaped = true;
continue;
}
len++;
}
return len;
}
bool processline(char* line)
{
if ( linesused == lineslength )
@ -50,7 +73,7 @@ bool processline(char* line)
}
lines[linesused++] = line;
size_t linelen = strlen(line);
size_t linelen = measurelength(line);
if ( longestline < linelen ) { longestline = linelen; }
return true;
}