commit: 7c2ab19971775259821d4a6b515aa7cb0573461a
parent d63fe3715f147d03848136380ffcf4b757f818af
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sun, 10 Mar 2024 11:43:56 +0100
cmd/df: align columns with basic printf
Not perfect as when fsname and mnt_type are too large columns are de-aligned.
Would need to get table formatting but the library situation isn't great:
- util-linux has libsmartcols but unavailable outside of Linux, notably BSDs
- libxo seems perfect for this but isn't ubiquitous (yet?)
And I'd rather not roll my own.
Diffstat:
2 files changed, 27 insertions(+), 16 deletions(-)
diff --git a/cmd/df.1 b/cmd/df.1
@@ -16,9 +16,9 @@
displays the current usage and mountpoints of mounted filesystems in a space-separated list containing:
Filesystem, Total, Used, Available, Use percentage, Mountpoint.
.Pp
-Unlike most implementations this one doesn't prints a proper table. You'll need a utility such as
+Unlike some implementations the table formatting isn't perfect, you might want to use
.Xr column 1
-for this feature.
+to get more readable outputs.
.Sh OPTIONS
.Bl -tag -width Ds
.It Fl a
diff --git a/cmd/df.c b/cmd/df.c
@@ -33,6 +33,7 @@ main(int argc, char *argv[])
{
bool opt_P = false, opt_h = false, opt_a = false, opt_T = false;
size_t excluded_count = 0;
+ int fs_col_width = 20;
char *excluded[4096];
int c = EOF;
@@ -52,7 +53,8 @@ main(int argc, char *argv[])
forced_bsize = 1024;
break;
case 'h':
- opt_h = true;
+ opt_h = true;
+ fs_col_width = 30;
break;
case 'T':
opt_T = true;
@@ -69,32 +71,34 @@ main(int argc, char *argv[])
}
}
- if(opt_T && opt_P)
+ if(opt_P && opt_T)
{
- fprintf(stderr, "df: Options -T and -P are incompatible\n");
+ fprintf(stderr, "df: Options -P and -T are incompatible\n");
return 1;
}
+ if(opt_P) opt_h = false;
+
argc -= optind;
argv += optind;
// Begin: Print header
- printf("Filesystem ");
+ printf("%-*s ", fs_col_width, "Filesystem");
- if(opt_T) printf("Type ");
+ if(opt_T) printf("%10s ", "Type");
if(forced_bsize != 0)
- printf("%zd-blocks ", forced_bsize);
+ printf("%3zd-blocks ", forced_bsize);
else
- printf("Total ");
+ printf("%10s ", "Total");
- printf("Used Available ");
+ printf("%10s %10s ", "Used", "Available");
if(opt_P)
printf("Capacity Mounted on\n");
else
- printf("Used%% Mountpoint\n");
+ printf("Use%% Mountpoint\n");
// End: Print header
@@ -125,7 +129,14 @@ main(int argc, char *argv[])
static_escape(mntent->mnt_fsname);
static_escape(mntent->mnt_dir);
- if(opt_a) printf("%s - - - - %s\n", mntent->mnt_fsname, mntent->mnt_dir);
+ if(opt_a)
+ {
+ printf("%-*s ", fs_col_width, mntent->mnt_fsname);
+
+ if(opt_T) printf("%10s ", mntent->mnt_type);
+
+ printf("%10s %10s %10s %4s %s\n", "-", "-", "-", "-", mntent->mnt_dir);
+ }
continue;
}
@@ -197,9 +208,9 @@ main(int argc, char *argv[])
used /= forced_bsize;
}
- printf("%s ", mntent->mnt_fsname);
+ printf("%-*s ", fs_col_width, mntent->mnt_fsname);
- if(opt_T) printf("%s ", mntent->mnt_type);
+ if(opt_T) printf("%10s ", mntent->mnt_type);
if(opt_h && !opt_P)
{
@@ -208,7 +219,7 @@ main(int argc, char *argv[])
struct si_scale free_scl = dtosi(free, true);
// clang-format off
- printf("%.2f%s %.2f%s %.2f%s %zd%% %s\n",
+ printf("%7.2f%-3s %7.2f%-3s %7.2f%-3s %3zd%% %s\n",
total_scl.number, total_scl.prefix,
used_scl.number, used_scl.prefix,
free_scl.number, free_scl.prefix,
@@ -220,7 +231,7 @@ main(int argc, char *argv[])
else
{
// clang-format off
- printf("%zd %zd %zd %zd%% %s\n",
+ printf("%10zd %10zd %10zd %3zd%% %s\n",
total,
used,
free,