diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-02-08 17:12:23 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-02-08 17:12:23 +0100 |
| commit | b97ec93748902c90f25524116d3d189f1b263474 (patch) | |
| tree | c1ef2d49937c0e5df28fefe4e88222783adaf3cf /core | |
| parent | 262322f9742ee925fe135038a9bbb631acd56b36 (diff) | |
make ls cooler
Diffstat (limited to 'core')
| -rw-r--r-- | core/ls.c | 26 |
1 files changed, 24 insertions, 2 deletions
@@ -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; } |