diff --git a/core/commands/dag/dag.go b/core/commands/dag/dag.go index 6827e46fab1..caf7a5474eb 100644 --- a/core/commands/dag/dag.go +++ b/core/commands/dag/dag.go @@ -7,6 +7,7 @@ import ( "io" "path" + "github.com/dustin/go-humanize" "github.com/ipfs/kubo/core/commands/cmdenv" "github.com/ipfs/kubo/core/commands/cmdutils" @@ -349,7 +350,11 @@ type DagStatSummary struct { } func (s *DagStatSummary) String() string { - return fmt.Sprintf("Total Size: %d\nUnique Blocks: %d\nShared Size: %d\nRatio: %f", s.TotalSize, s.UniqueBlocks, s.SharedSize, s.Ratio) + return fmt.Sprintf("Total Size: %d (%s)\nUnique Blocks: %d\nShared Size: %d (%s)\nRatio: %f", + s.TotalSize, humanize.Bytes(s.TotalSize), + s.UniqueBlocks, + s.SharedSize, humanize.Bytes(s.SharedSize), + s.Ratio) } func (s *DagStatSummary) incrementTotalSize(size uint64) { @@ -384,7 +389,7 @@ Note: This command skips duplicate blocks in reporting both size and the number cmds.StringArg("root", true, true, "CID of a DAG root to get statistics for").EnableStdin(), }, Options: []cmds.Option{ - cmds.BoolOption(progressOptionName, "p", "Return progressive data while reading through the DAG").WithDefault(true), + cmds.BoolOption(progressOptionName, "p", "Show progress on stderr. Auto-detected if stderr is a terminal."), }, Run: dagStat, Type: DagStatSummary{}, diff --git a/core/commands/dag/stat.go b/core/commands/dag/stat.go index bb9be7e0d90..916aae71a6b 100644 --- a/core/commands/dag/stat.go +++ b/core/commands/dag/stat.go @@ -5,6 +5,7 @@ import ( "io" "os" + "github.com/dustin/go-humanize" mdag "github.com/ipfs/boxo/ipld/merkledag" "github.com/ipfs/boxo/ipld/merkledag/traverse" cid "github.com/ipfs/go-cid" @@ -19,7 +20,11 @@ import ( // to compute the new state func dagStat(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { - progressive := req.Options[progressOptionName].(bool) + // Default to true (emit intermediate states) for HTTP/RPC clients that want progress + progressive := true + if val, specified := req.Options[progressOptionName].(bool); specified { + progressive = val + } api, err := cmdenv.GetApi(env, req) if err != nil { return err @@ -84,6 +89,18 @@ func dagStat(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) } func finishCLIStat(res cmds.Response, re cmds.ResponseEmitter) error { + // Determine whether to show progress based on TTY detection or explicit flag + var showProgress bool + val, specified := res.Request().Options[progressOptionName] + if !specified { + // Auto-detect: show progress only if stderr is a TTY + if errStat, err := os.Stderr.Stat(); err == nil { + showProgress = (errStat.Mode() & os.ModeCharDevice) != 0 + } + } else { + showProgress = val.(bool) + } + var dagStats *DagStatSummary for { v, err := res.Next() @@ -96,17 +113,26 @@ func finishCLIStat(res cmds.Response, re cmds.ResponseEmitter) error { switch out := v.(type) { case *DagStatSummary: dagStats = out - if dagStats.Ratio == 0 { - length := len(dagStats.DagStatsArray) - if length > 0 { - currentStat := dagStats.DagStatsArray[length-1] - fmt.Fprintf(os.Stderr, "CID: %s, Size: %d, NumBlocks: %d\n", currentStat.Cid, currentStat.Size, currentStat.NumBlocks) + // Ratio == 0 means this is a progress update (not final result) + if showProgress && dagStats.Ratio == 0 { + // Sum up total progress across all DAGs being scanned + var totalBlocks int64 + var totalSize uint64 + for _, stat := range dagStats.DagStatsArray { + totalBlocks += stat.NumBlocks + totalSize += stat.Size } + fmt.Fprintf(os.Stderr, "Fetched/Processed %d blocks, %d bytes (%s)\r", totalBlocks, totalSize, humanize.Bytes(totalSize)) } default: return e.TypeErr(out, v) - } } + + // Clear the progress line before final output + if showProgress { + fmt.Fprint(os.Stderr, "\033[2K\r") + } + return re.Emit(dagStats) } diff --git a/docs/changelogs/v0.40.md b/docs/changelogs/v0.40.md index 29780937f4b..5ead6befa63 100644 --- a/docs/changelogs/v0.40.md +++ b/docs/changelogs/v0.40.md @@ -11,7 +11,8 @@ This release was brought to you by the [Shipyard](https://ipshipyard.com/) team. - [Overview](#overview) - [๐Ÿ”ฆ Highlights](#-highlights) - [Routing V1 HTTP API now exposed by default](#routing-v1-http-api-now-exposed-by-default) - - [Track total size when adding pins](#track-total-size-when-adding-pins] + - [Track total size when adding pins](#track-total-size-when-adding-pins) + - [Improved `ipfs dag stat` output](#improved-ipfs-dag-stat-output) - [๐Ÿ“ Changelog](#-changelog) - [๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributors](#-contributors) @@ -32,6 +33,30 @@ Example output: Fetched/Processed 336 nodes (83 MB) ``` +#### Improved `ipfs dag stat` output + +The `ipfs dag stat` command has been improved for better terminal UX: + +- Progress output now uses a single line with carriage return, avoiding terminal flooding +- Progress is auto-detected: shown only in interactive terminals by default +- Human-readable sizes are now displayed alongside raw byte counts + +Example progress (interactive terminal): +``` +Fetched/Processed 84 blocks, 2097152 bytes (2.1 MB) +``` + +Example summary output: +``` +Summary +Total Size: 2097152 (2.1 MB) +Unique Blocks: 42 +Shared Size: 1048576 (1.0 MB) +Ratio: 1.500000 +``` + +Use `--progress=true` to force progress even when piped, or `--progress=false` to disable it. + ### ๐Ÿ“ Changelog ### ๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributors diff --git a/test/cli/fixtures/TestDagStatExpectedOutput.txt b/test/cli/fixtures/TestDagStatExpectedOutput.txt index 9e709f4a215..87bc405a1d7 100644 --- a/test/cli/fixtures/TestDagStatExpectedOutput.txt +++ b/test/cli/fixtures/TestDagStatExpectedOutput.txt @@ -4,9 +4,9 @@ bafyreibmdfd7c5db4kls4ty57zljfhqv36gi43l6txl44pi423wwmeskwy 2 53 bafyreie3njilzdi4ixumru4nzgecsnjtu7fzfcwhg7e6s4s5i7cnbslvn4 2 53 Summary -Total Size: 99 +Total Size: 99 (99 B) Unique Blocks: 3 -Shared Size: 7 +Shared Size: 7 (7 B) Ratio: 1.070707