Skip to content

Commit

Permalink
refactor: adjust style (chenquan#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenquan authored Aug 10, 2022
1 parent d4a99f0 commit c0478be
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 35 deletions.
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ module github.com/chenquan/diskusage
go 1.18

require (
github.com/fatih/color v1.13.0 // indirect
github.com/fatih/color v1.13.0
github.com/jedib0t/go-pretty/v6 v6.3.6
github.com/spf13/cobra v1.5.0
github.com/spf13/pflag v1.0.5
)

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/spf13/cobra v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jedib0t/go-pretty/v6 v6.3.6 h1:A6w2BuyPMtf7M82BGRBys9bAba2C26ZX9lrlrZ7uH6U=
github.com/jedib0t/go-pretty/v6 v6.3.6/go.mod h1:MgmISkTWDSFu0xOqiZ0mKNntMQ2mDgOcwOkwBEkMDJI=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
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/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
103 changes: 71 additions & 32 deletions internal/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@ import (
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/chenquan/diskusage/internal/worker"
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/v6/list"
flag "github.com/spf13/pflag"

"github.com/spf13/cobra"
)

type file struct {
sub []file
name string
isDir bool
size int64
modifyTime time.Time
mode fs.FileMode
}
type (
file struct {
sub []file
name string
isDir bool
size int64
}

infoFile struct {
size float64
str string
usageRate float64
uint string
isDir bool
}
)

const (
Bytes = 1
Expand Down Expand Up @@ -132,7 +141,17 @@ func Stat(cmd *cobra.Command, _ []string) error {
header := fmt.Sprintf("Total: %0.3f%s\t%s", val, reduceUnit, color.HiGreenString(dir))
colorPrintln(header)
colorPrintln(strings.Repeat("-", len(header)+2))
printFiles(files, 0, depth, unit, all)
l := list.NewWriter()
l.SetStyle(list.StyleConnectedLight)
infoFiles := printFiles(l, files, 0, depth, unit, totalSize, all)
maxLen := 0
for _, info := range infoFiles {
size := len(info.str)
if maxLen < size {
maxLen = size
}
}
printTree(l.Render(), infoFiles, maxLen)
errChan <- nil
}()

Expand Down Expand Up @@ -185,10 +204,8 @@ func find(dir string, filter func(info fs.FileInfo) bool) ([]file, error) {

if !entry.IsDir() {
fileChan <- file{
name: entry.Name(),
size: fileInfo.Size(),
modifyTime: fileInfo.ModTime(),
mode: fileInfo.Mode(),
name: entry.Name(),
size: fileInfo.Size(),
}
continue
}
Expand All @@ -211,12 +228,10 @@ func find(dir string, filter func(info fs.FileInfo) bool) ([]file, error) {
}

fileChan <- file{
sub: subFiles,
name: entry.Name(),
isDir: true,
size: totalSize,
modifyTime: fileInfo.ModTime(),
mode: fileInfo.Mode(),
sub: subFiles,
name: entry.Name(),
isDir: true,
size: totalSize,
}
}
w.Run(do)
Expand All @@ -233,32 +248,41 @@ func find(dir string, filter func(info fs.FileInfo) bool) ([]file, error) {
return files, nil
}

func printFiles(files []file, n, depth int, unit string, all bool) {
func printFiles(l list.Writer, files []file, n, depth int, unit string, totalSize int64, all bool) []infoFile {
if n == depth {
return
return nil
}

bar := strings.Repeat(" ", n) + "|--"
var infoFiles []infoFile
for _, f := range files {
if f.isDir && f.size == 0 && !all {
continue
}

val, reduceUnit := getReduce(unit, f.size)
part1 := fmt.Sprintf("%s %s %9.3f%s", f.modifyTime.Format("20060102 15:04:05"), f.mode, val, reduceUnit)
part2 := color.HiGreenString(f.name)
var s = bar
infoFiles = append(infoFiles, infoFile{
size: val,
uint: reduceUnit,
usageRate: float64(f.size) / float64(totalSize) * 100,
str: fmt.Sprintf("%0.1f", val),
isDir: f.isDir,
})

name := f.name
if f.isDir {
s += color.HiBlueString(part1) + " " + part2
} else {
s += part1 + " " + part2
name = color.HiGreenString(name)
}
colorPrintln(s)
l.AppendItem(name)

if f.isDir {
printFiles(f.sub, n+1, depth, unit, all)
l.Indent()
subUsageSizes := printFiles(l, f.sub, n+1, depth, unit, totalSize, all)
infoFiles = append(infoFiles, subUsageSizes...)
l.UnIndent()
}
}

return infoFiles
}

func getReduce(unit string, n int64) (float64, string) {
Expand All @@ -280,7 +304,7 @@ func getReduce(unit string, n int64) (float64, string) {
break
}

if int(float64(n)/float64(units[reduce])*1000) > 0 {
if int(float64(n)/float64(units[reduce])*10) > 0 {
break
}

Expand All @@ -298,3 +322,18 @@ func colorPrintln(a ...any) {
func accessDenied(err error) bool {
return err == errorAccessDenied
}

func printTree(content string, infoFiles []infoFile, maxLen int) {
for i, line := range strings.Split(content, "\n") {
info := infoFiles[i]

str := " %" + strconv.Itoa(maxLen) + ".1f%s %4.1f%%"
str = fmt.Sprintf(str, info.size, info.uint, info.usageRate)
if info.isDir {
str = color.HiRedString(str)
}

colorPrintln(str, line)
}
colorPrintln()
}

0 comments on commit c0478be

Please sign in to comment.