Use utflen in cols(1)

This commit is contained in:
Brandon Mulcahy 2014-12-05 16:32:08 -05:00 committed by sin
parent 5214191155
commit c4014b730e
2 changed files with 10 additions and 7 deletions

3
cols.1
View File

@ -27,8 +27,7 @@ This implementation of
.B cols .B cols
assumes that every byte is a character assumes that every byte is a character
which takes up one column on the screen. which takes up one column on the screen.
It does not handle non-ASCII UTF-8 runes It does not handle TAB characters correctly.
or TAB characters correctly.
.B cols .B cols
currently mangles files which contain embedded NULs. currently mangles files which contain embedded NULs.
.B cols .B cols

14
cols.c
View File

@ -7,6 +7,7 @@
#include <unistd.h> #include <unistd.h>
#include "text.h" #include "text.h"
#include "utf.h"
#include "util.h" #include "util.h"
static long chars = 65; static long chars = 65;
@ -26,7 +27,7 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
long i, l, col; long i, l, col;
size_t len; size_t len, bytes;
int maxlen = 0; int maxlen = 0;
struct winsize w; struct winsize w;
FILE *fp; FILE *fp;
@ -59,9 +60,12 @@ main(int argc, char *argv[])
} }
for (l = 0; l < b.nlines; ++l) { for (l = 0; l < b.nlines; ++l) {
len = strlen(b.lines[l]); len = utflen(b.lines[l]);
if (len > 0 && b.lines[l][len-1] == '\n') bytes = strlen(b.lines[l]);
b.lines[l][--len] = '\0'; if (len > 0 && b.lines[l][bytes-1] == '\n') {
b.lines[l][bytes-1] = '\0';
--len;
}
if (len > maxlen) if (len > maxlen)
maxlen = len; maxlen = len;
if (maxlen > (chars - 1) / 2) if (maxlen > (chars - 1) / 2)
@ -79,7 +83,7 @@ main(int argc, char *argv[])
n_rows = (b.nlines + (n_columns - 1)) / n_columns; n_rows = (b.nlines + (n_columns - 1)) / n_columns;
for (i = 0; i < n_rows; ++i) { for (i = 0; i < n_rows; ++i) {
for (l = i, col = 1; l < b.nlines; l += n_rows, ++col) { for (l = i, col = 1; l < b.nlines; l += n_rows, ++col) {
len = strlen(b.lines[l]); len = utflen(b.lines[l]);
fputs(b.lines[l], stdout); fputs(b.lines[l], stdout);
if (col < n_columns) if (col < n_columns)
printf("%*s", maxlen + 1 - (int)len, ""); printf("%*s", maxlen + 1 - (int)len, "");