From b97ec93748902c90f25524116d3d189f1b263474 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 8 Feb 2023 17:12:23 +0100 Subject: make ls cooler --- core/ls.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/ls.c b/core/ls.c index 6a25a3f..cd96ce7 100644 --- a/core/ls.c +++ b/core/ls.c @@ -3,6 +3,8 @@ #include "../lib/io/io.h" #include "../lib/list/list.h" #include "../lib/arg/arg.h" +#include "../lib/tctl/tctl.h" +#include "../lib/cstr/cstr.h" #define BUF_SIZE sizeof(struct dirent) * 32 @@ -43,6 +45,10 @@ int main(int argc, const char *argv[]) char d_type; char buf[BUF_SIZE]; long nread; + u8 istty = isatty(); + i64 maxwidth = istty ? tctl_get_window_size().width : -1; + i64 current_width = 0; + struct dirent *d; dirs = new_list(); @@ -65,8 +71,21 @@ int main(int argc, const char *argv[]) for (u64 bpos = 0; bpos < nread;) { d = (struct dirent*) (buf + bpos); - if (filter_entry(d)) - wstdf("%s\n", d->d_name); + if (filter_entry(d)) { + if (!istty) { + wstdf("%s\n", d->d_name); + } else { + current_width += cstr_length(d->d_name) + 2; + if (current_width > maxwidth) { + wstd("\n"); + wstd(d->d_name); + wstd(" "); + current_width = cstr_length(d->d_name) + 2; + } else { + wstdf("%s ", d->d_name); + } + } + } bpos += d->d_reclen; } } @@ -74,5 +93,8 @@ int main(int argc, const char *argv[]) close(fd); } + if (istty) + wstd("\n"); + return 0; } -- cgit v1.2.3-70-g09d2