Skip to content

Commit

Permalink
Print bytes in human format
Browse files Browse the repository at this point in the history
  • Loading branch information
angelsolaorbaiceta committed Sep 5, 2024
1 parent 2787c37 commit 9006029
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
8 changes: 7 additions & 1 deletion archive/header_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/binary"
"fmt"
"io"

"github.com/dustin/go-humanize"
)

// HeaderFileEntry represents a single file's metadata in the archive.
Expand All @@ -30,7 +32,11 @@ func NewHeaderFileEntry(name string, size uint32) *HeaderFileEntry {

// String returns a string representation of the HeaderFileEntry.
func (f *HeaderFileEntry) String() string {
return fmt.Sprintf("%s (Offset: %d bytes, Compressed size: %d bytes)", f.Name, f.Offset, f.Size)
var (
off = humanize.Bytes(uint64(f.Offset))
size = humanize.Bytes(uint64(f.Size))
)
return fmt.Sprintf("%s (Offset: %s, Compressed size: %s)", f.Name, off, size)
}

// nameLength returns the length of the file name in bytes.
Expand Down
10 changes: 8 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/angelsolaorbaiceta/aar/archive"
"github.com/dustin/go-humanize"
)

func CreateArchive(outFileName string, inFileNames []string) {
Expand All @@ -29,7 +30,12 @@ func CreateArchive(outFileName string, inFileNames []string) {
os.Exit(1)
}

var (
archSize = humanize.Bytes(uint64(archive.TotalSize()))
headerSize = humanize.Bytes(uint64(archive.Header.HeaderLength))
)

fmt.Fprintf(os.Stderr, "Archive created successfully.\n")
fmt.Fprintf(os.Stderr, " > Archive size = %d bytes.\n", archive.TotalSize())
fmt.Fprintf(os.Stderr, " > Header size = %d bytes.\n", archive.Header.HeaderLength)
fmt.Fprintf(os.Stderr, " > Archive size = %s.\n", archSize)
fmt.Fprintf(os.Stderr, " > Header size = %s.\n", headerSize)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/angelsolaorbaiceta/aar
go 1.22.0

require (
github.com/dustin/go-humanize v1.0.1
github.com/stretchr/testify v1.9.0
github.com/ulikunitz/xz v0.5.12
golang.org/x/crypto v0.26.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down

0 comments on commit 9006029

Please sign in to comment.