Skip to content

Commit

Permalink
Add total size and total file count to file listing
Browse files Browse the repository at this point in the history
Follow-up from #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 <[email protected]>
  • Loading branch information
HeavyWombat committed Nov 4, 2024
1 parent c9b8280 commit e325a08
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/util/filelisting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
})

Expand Down

0 comments on commit e325a08

Please sign in to comment.