From c02bba742ac9c2875682b5e6eb1df79ccd1d2e52 Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Tue, 5 Nov 2024 12:09:31 +0100 Subject: [PATCH] Tweak file listing to omit `.git` directory In almost all cases, the `.git` directory content is not directly helpful in the file listing and it leads to a lot of noise in the output. Do not show `.git` files in file listing, but show a summary of size and file count at the end in the footer section. Signed-off-by: Matthias Diester --- pkg/util/filelisting.go | 49 ++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/pkg/util/filelisting.go b/pkg/util/filelisting.go index 6dcea1cda..693be282f 100644 --- a/pkg/util/filelisting.go +++ b/pkg/util/filelisting.go @@ -19,21 +19,34 @@ import ( "github.com/jedib0t/go-pretty/v6/text" ) +type details struct { + name string + count int + size int64 +} + // ListFiles prints all files in a given directory to the provided writer func ListFiles(w io.Writer, dir string) error { var totalBytes int64 var totalFiles int + var footerDetails = map[string]details{} t := table.NewWriter() defer func() { t.AppendSeparator() - t.AppendRow( - table.Row{ + for _, specialDir := range footerDetails { + t.AppendRow(table.Row{ "", "", "", "", - humanReadableSize(totalBytes), - fmt.Sprintf("%d files", totalFiles), - }, - ) + humanReadableSize(specialDir.size), + fmt.Sprintf("%d files in %s", specialDir.count, specialDir.name), + }) + } + + t.AppendRow(table.Row{ + "", "", "", "", + humanReadableSize(totalBytes), + fmt.Sprintf("%d files in total", totalFiles), + }) t.Render() }() @@ -60,6 +73,24 @@ func ListFiles(w io.Writer, dir string) error { path = l } + // update the total count and size + totalBytes += info.Size() + totalFiles++ + + // special handling for the .git directory, which would otherwise + // mostly clutter the output with potentially useless information + if strings.HasPrefix(path, ".git/") || path == ".git" { + dotGitDetails, ok := footerDetails[".git"] + if !ok { + dotGitDetails = details{name: ".git"} + } + + dotGitDetails.size += info.Size() + dotGitDetails.count++ + footerDetails[".git"] = dotGitDetails + return nil + } + // if possible, try to obtain nlink count and user/group details var nlink, user, group string = "?", "?", "?" if stat, ok := info.Sys().(*syscall.Stat_t); ok { @@ -68,16 +99,12 @@ func ListFiles(w io.Writer, dir string) error { nlink = strconv.FormatUint(uint64(stat.Nlink), 10) } - var size = info.Size() - totalBytes += size - totalFiles++ - t.AppendRow(table.Row{ filemode(info), nlink, user, group, - humanReadableSize(size), + humanReadableSize(info.Size()), path, })