Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add total size and total file count to file listing #1721

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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