mirror of git://git.suckless.org/sbase
cols: simplify filling with spaces
Use printf("%*s", n, "") instead of allocating a string filled with spaces.
This commit is contained in:
parent
2e1580ed2d
commit
7f7e7dcbb9
15
cols.c
15
cols.c
|
@ -26,8 +26,8 @@ int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
long i, l, col;
|
long i, l, col;
|
||||||
size_t maxlen = 0;
|
size_t len;
|
||||||
char *space;
|
int maxlen = 0;
|
||||||
struct winsize w;
|
struct winsize w;
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
for(l = 0; l < b.nlines; ++l) {
|
for(l = 0; l < b.nlines; ++l) {
|
||||||
size_t len = strlen(b.lines[l]);
|
len = strlen(b.lines[l]);
|
||||||
if(len > 0 && b.lines[l][len-1] == '\n')
|
if(len > 0 && b.lines[l][len-1] == '\n')
|
||||||
b.lines[l][--len] = '\0';
|
b.lines[l][--len] = '\0';
|
||||||
if(len > maxlen)
|
if(len > maxlen)
|
||||||
|
@ -76,21 +76,16 @@ main(int argc, char *argv[])
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!(space = malloc(maxlen + 2)))
|
|
||||||
eprintf("malloc:");
|
|
||||||
memset(space, ' ', maxlen + 1);
|
|
||||||
space[maxlen + 1] = '\0';
|
|
||||||
|
|
||||||
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]);
|
||||||
fputs(b.lines[l], stdout);
|
fputs(b.lines[l], stdout);
|
||||||
if(col < n_columns)
|
if(col < n_columns)
|
||||||
fputs(space + strlen(b.lines[l]), stdout);
|
printf("%*s", maxlen + 1 - (int)len, "");
|
||||||
}
|
}
|
||||||
fputs("\n", stdout);
|
fputs("\n", stdout);
|
||||||
}
|
}
|
||||||
free(space);
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue