diff options
-rw-r--r-- | analyze/dir.go | 33 | ||||
-rw-r--r-- | analyze/dir_test.go | 19 | ||||
-rw-r--r-- | analyze/file.go | 17 | ||||
-rw-r--r-- | stdout/stdout.go | 6 | ||||
-rw-r--r-- | tui/tui.go | 6 |
5 files changed, 68 insertions, 13 deletions
diff --git a/analyze/dir.go b/analyze/dir.go index b0e5d1b..6d72a63 100644 --- a/analyze/dir.go +++ b/analyze/dir.go @@ -3,6 +3,7 @@ package analyze import ( "io/ioutil" "log" + "os" "path/filepath" "runtime" "sync" @@ -48,8 +49,19 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel log.Print(err.Error()) } + var flag rune + switch { + case err != nil: + flag = '!' + case len(files) == 0: + flag = 'e' + default: + flag = ' ' + } + dir := File{ Name: filepath.Base(path), + Flag: flag, IsDir: true, ItemCount: 1, Files: make([]*File, 0, len(files)), @@ -71,15 +83,36 @@ func processDir(path string, progress *CurrentProgress, concurrencyLimitChannel concurrencyLimitChannel <- true subdir := processDir(entryPath, progress, concurrencyLimitChannel, wait, ignoreDir) subdir.Parent = &dir + mutex.Lock() + dir.Files = append(dir.Files, subdir) + + switch subdir.Flag { + case '!', '.': + if dir.Flag != '!' { + dir.Flag = '.' + } + } + mutex.Unlock() + <-concurrencyLimitChannel wait.Done() }() } else { + switch { + case f.Mode()&os.ModeSymlink != 0: + fallthrough + case f.Mode()&os.ModeSocket != 0: + flag = '@' + default: + flag = ' ' + } + file = &File{ Name: f.Name(), + Flag: flag, Size: f.Size(), Usage: getUsage(f), ItemCount: 1, diff --git a/analyze/dir_test.go b/analyze/dir_test.go index 0101d77..dcf0bf7 100644 --- a/analyze/dir_test.go +++ b/analyze/dir_test.go @@ -1,6 +1,7 @@ package analyze import ( + "os" "sync" "testing" @@ -45,6 +46,24 @@ func TestIgnoreDir(t *testing.T) { assert.Equal(t, 1, dir.ItemCount) } +func TestFlags(t *testing.T) { + fin := testdir.CreateTestDir() + defer fin() + + os.Symlink("test_dir/nested/file2", "test_dir/nested/file3") + + dir := ProcessDir("test_dir", &CurrentProgress{Mutex: &sync.Mutex{}}, func(_ string) bool { return false }) + + assert.Equal(t, int64(28+4096*3), dir.Size) + assert.Equal(t, 6, dir.ItemCount) + + // test file3 + assert.Equal(t, "file3", dir.Files[0].Files[1].Name) + assert.Equal(t, int64(21), dir.Files[0].Files[1].Size) + assert.Equal(t, int64(0), dir.Files[0].Files[1].Usage) + assert.Equal(t, '@', dir.Files[0].Files[1].Flag) +} + func BenchmarkProcessDir(b *testing.B) { fin := testdir.CreateTestDir() defer fin() diff --git a/analyze/file.go b/analyze/file.go index 25637a0..c7da602 100644 --- a/analyze/file.go +++ b/analyze/file.go @@ -7,14 +7,15 @@ import ( // File struct type File struct { - Name string - BasePath string - Size int64 - Usage int64 - ItemCount int - IsDir bool - Files Files - Parent *File + Name string + BasePath string + Flag rune + Size int64 + Usage int64 + ItemCount int + IsDir bool + Files Files + Parent *File } // Path retruns absolute path of the file diff --git a/stdout/stdout.go b/stdout/stdout.go index e15d8c5..8776750 100644 --- a/stdout/stdout.go +++ b/stdout/stdout.go @@ -135,9 +135,9 @@ func (ui *UI) AnalyzePath(path string, analyzer analyze.Analyzer, _ *analyze.Fil var lineFormat string if ui.useColors { - lineFormat = "%20s %s\n" + lineFormat = "%s %20s %s\n" } else { - lineFormat = "%9s %s\n" + lineFormat = "%s %9s %s\n" } var size int64 @@ -152,11 +152,13 @@ func (ui *UI) AnalyzePath(path string, analyzer analyze.Analyzer, _ *analyze.Fil if file.IsDir { fmt.Fprintf(ui.output, lineFormat, + string(file.Flag), ui.formatSize(size), ui.blue.Sprintf("/"+file.Name)) } else { fmt.Fprintf(ui.output, lineFormat, + string(file.Flag), ui.formatSize(size), file.Name) } @@ -537,12 +537,12 @@ func (ui *UI) formatFileRow(item *analyze.File) string { part = int(float64(item.Usage) / float64(item.Parent.Usage) * 10.0) } - var row string + row := string(item.Flag) if ui.useColors { - row = "[#e67100:black:b]" + row += "[#e67100:black:b]" } else { - row = "[white:black:b]" + row += "[white:black:b]" } if ui.showApparentSize { |