Skip to content

Commit

Permalink
Changing bytes to human readable format
Browse files Browse the repository at this point in the history
  • Loading branch information
Stef committed Oct 21, 2020
1 parent cd1649f commit 9ae5e32
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 3 additions & 3 deletions client/reciever.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Reciever(code string) {
addr, port := utils.GetIPAddrAndPort()
conn, err := grpc.Dial(addr+":"+port, grpc.WithInsecure())
if err != nil {
log.Fatalf("Error:- ", err)
log.Fatalf("Error:- %v", err)
return
}
defer conn.Close()
Expand All @@ -42,12 +42,12 @@ func Reciever(code string) {
log.Fatalf("Error:- %v", err)
return
}
fmt.Println("FileName: ", file.Name, " Hash: ", file.Hash, " Size: ", file.Size)
fmt.Println("FileName: ", file.Name, " Hash: ", file.Hash, " Size: ", utils.ByteCountDecimal(file.Size))
if utils.FileExists(file.Name) {
fmt.Println("File Exists!")
return
}
bar := progressbar.Default(file.Size)
bar := progressbar.DefaultBytes(file.Size)
ack, err := client.SharePublicKey(context.Background(), &proto.PublicKey{Key: key.Bytes(), Code: code})
if err != nil {
log.Fatalf("Error:- %v", err)
Expand Down
6 changes: 3 additions & 3 deletions client/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func Sender(filename string) {
addr, port := utils.GetIPAddrAndPort()
conn, err := grpc.Dial(addr+":"+port, grpc.WithInsecure())
if err != nil {
log.Fatalf("Error:- ", err)
log.Fatalf("Error:- %v", err)
return
}
defer conn.Close()
Expand All @@ -35,10 +35,10 @@ func Sender(filename string) {
}
defer file.Close()
fname, _ := os.Stat(filename)
bar := progressbar.Default(fname.Size())
bar := progressbar.DefaultBytes(fname.Size())
code, err := client.Sender(context.Background(), &proto.SenderRequest{Name: fname.Name(), Hash: utils.FileHash(filename), Size: fname.Size()})
if err != nil {
log.Fatalf("Error:- ", err)
log.Fatalf("Error:- %v", err)
return
}
fmt.Printf("On the other device type \n\n")
Expand Down
18 changes: 18 additions & 0 deletions utils/bytesToHuman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package utils

import (
"fmt"
)

func ByteCountDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}

0 comments on commit 9ae5e32

Please sign in to comment.