From e325a08c9e7a82584ff51294e7e936cda9747831 Mon Sep 17 00:00:00 2001 From: Matthias Diester Date: Mon, 4 Nov 2024 12:00:18 +0100 Subject: [PATCH] Add total size and total file count to file listing Follow-up from https://github.com/shipwright-io/build/pull/1702 to add the total size in bytes of all files and the total number of files to the file listing so that its immediately visible. Signed-off-by: Matthias Diester --- pkg/util/filelisting.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/util/filelisting.go b/pkg/util/filelisting.go index de89c52a7..6dcea1cda 100644 --- a/pkg/util/filelisting.go +++ b/pkg/util/filelisting.go @@ -21,13 +21,28 @@ import ( // 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 + t := table.NewWriter() - defer t.Render() + defer func() { + t.AppendSeparator() + t.AppendRow( + table.Row{ + "", "", "", "", + humanReadableSize(totalBytes), + fmt.Sprintf("%d files", totalFiles), + }, + ) + + t.Render() + }() t.SetOutputMirror(w) t.SetColumnConfigs([]table.ColumnConfig{{Number: 5, Align: text.AlignRight}}) t.Style().Options.DrawBorder = false t.Style().Options.SeparateColumns = false + t.Style().Box.MiddleHorizontal = "─" return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error { if err != nil { @@ -53,12 +68,16 @@ 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(info.Size()), + humanReadableSize(size), path, })