Skip to content

Commit

Permalink
formatting byte count to be more human readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillermo Fernández committed Jan 19, 2024
1 parent a703e94 commit 2593796
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
4 changes: 2 additions & 2 deletions fileinfo/fileInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package fileinfo

import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"sync"

"github.com/charmbracelet/bubbles/table"
"github.com/frogfreg/godu/utilities"
)

type FileInfo struct {
Expand Down Expand Up @@ -62,7 +62,7 @@ func getSize(entry string) (int, error) {
func FileInfosToRow(fis []FileInfo) []table.Row {
var rows []table.Row
for _, fi := range fis {
rows = append(rows, []string{filepath.Base(fi.Name), fi.FileType, fmt.Sprintf("%v", fi.Size)})
rows = append(rows, []string{filepath.Base(fi.Name), fi.FileType, utilities.HumanReadableByteString(fi.Size)})
}
return rows
}
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func getFileInfoCmd(dir string) tea.Cmd {
func getInitialTable() table.Model {
t := table.New(table.WithColumns(
[]table.Column{
{Title: "Name", Width: 15},
{Title: "Name", Width: 30},
{Title: "Type", Width: 7},
{Title: "Size", Width: 15},
}),
Expand Down
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

A simple CLI tool to view and delete files and directories


![output](https://github.com/frogfreg/godu/assets/34764631/2ae06557-ec81-46b7-b92c-dd5a17ed5e49)

## TODO

- add error message
- use human readable format for byte count
- add toggle to show/hide controls
28 changes: 28 additions & 0 deletions utilities/formatting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utilities

import "fmt"

func HumanReadableByteString(byteCount int) string {
var quantity float64
var suffix string

kilobytes := float64(byteCount) / 1024
megabytes := kilobytes / 1024
gigabytes := megabytes / 1024

if gigabytes >= 1 {
quantity = gigabytes
suffix = "GB"
} else if megabytes >= 1 {
quantity = megabytes
suffix = "MB"
} else if kilobytes >= 1 {
quantity = kilobytes
suffix = "KB"
} else {
quantity = float64(byteCount)
suffix = "Bytes"
}

return fmt.Sprintf("%.2f %v", quantity, suffix)
}
31 changes: 31 additions & 0 deletions utilities/formatting_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utilities

import (
"fmt"
"testing"
)

func TestHumanReadableByteString(t *testing.T) {
tests := []struct {
byteCount int
expected string
}{
{1024, "1.00 KB"},
{2048, "2.00 KB"},
{1024 * 1024, "1.00 MB"},
{2 * 1024 * 1024, "2.00 MB"},
{1024 * 1024 * 1024, "1.00 GB"},
{2 * 1024 * 1024 * 1024, "2.00 GB"},
{0, "0.00 Bytes"},
{500, "500.00 Bytes"},
}

for _, test := range tests {
t.Run(fmt.Sprintf("Input_%d", test.byteCount), func(t *testing.T) {
result := HumanReadableByteString(test.byteCount)
if result != test.expected {
t.Errorf("Expected %s, but got %s", test.expected, result)
}
})
}
}

0 comments on commit 2593796

Please sign in to comment.