summaryrefslogtreecommitdiffstats
path: root/stdout
diff options
context:
space:
mode:
authorDaniel Milde <daniel@milde.cz>2021-07-16 23:51:51 +0200
committerDaniel Milde <daniel@milde.cz>2021-07-20 22:00:34 +0200
commit5f31cd1dc557ed94a6f5b1b3914940550fbca39c (patch)
tree43ce657dbfb28d78857012f63d23e647c221616e /stdout
parenta8837935e69ca541f5c002888e4685344653bea1 (diff)
read analysis from file
Diffstat (limited to 'stdout')
-rw-r--r--stdout/stdout.go85
-rw-r--r--stdout/stdout_test.go39
2 files changed, 122 insertions, 2 deletions
diff --git a/stdout/stdout.go b/stdout/stdout.go
index 0d246ad..b572e9a 100644
--- a/stdout/stdout.go
+++ b/stdout/stdout.go
@@ -6,6 +6,7 @@ import (
"math"
"os"
"path/filepath"
+ "runtime"
"sort"
"sync"
"time"
@@ -13,6 +14,7 @@ import (
"github.com/dundee/gdu/v5/internal/common"
"github.com/dundee/gdu/v5/pkg/analyze"
"github.com/dundee/gdu/v5/pkg/device"
+ "github.com/dundee/gdu/v5/report"
"github.com/fatih/color"
)
@@ -25,6 +27,8 @@ type UI struct {
blue *color.Color
}
+var progressRunes = []rune(`⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧`)
+
// CreateStdoutUI creates UI for stdout
func CreateStdoutUI(output io.Writer, useColors bool, showProgress bool, showApparentSize bool) *UI {
ui := &UI{
@@ -141,6 +145,12 @@ func (ui *UI) AnalyzePath(path string, _ *analyze.Dir) error {
wait.Wait()
+ ui.showDir(dir)
+
+ return nil
+}
+
+func (ui *UI) showDir(dir *analyze.Dir) {
sort.Sort(dir.Files)
var lineFormat string
@@ -173,18 +183,89 @@ func (ui *UI) AnalyzePath(path string, _ *analyze.Dir) error {
file.GetName())
}
}
+}
+
+// ReadAnalysis reads analysis report from JSON file
+func (ui *UI) ReadAnalysis(input io.Reader) error {
+ var (
+ dir *analyze.Dir
+ wait sync.WaitGroup
+ err error
+ doneChan chan struct{}
+ )
+
+ if ui.ShowProgress {
+ wait.Add(1)
+ doneChan = make(chan struct{})
+ go func() {
+ defer wait.Done()
+ ui.showReadingProgress(doneChan)
+ }()
+ }
+
+ wait.Add(1)
+ go func() {
+ defer wait.Done()
+ dir, err = report.ReadAnalysis(input)
+ if err != nil {
+ if ui.ShowProgress {
+ doneChan <- struct{}{}
+ }
+ return
+ }
+ runtime.GC()
+
+ links := make(analyze.AlreadyCountedHardlinks, 10)
+ dir.UpdateStats(links)
+
+ if ui.ShowProgress {
+ doneChan <- struct{}{}
+ }
+ }()
+
+ wait.Wait()
+
+ if err != nil {
+ return err
+ }
+
+ ui.showDir(dir)
return nil
}
+func (ui *UI) showReadingProgress(doneChan chan struct{}) {
+ emptyRow := "\r"
+ for j := 0; j < 40; j++ {
+ emptyRow += " "
+ }
+
+ i := 0
+ for {
+ fmt.Fprint(ui.output, emptyRow)
+
+ select {
+ case <-doneChan:
+ fmt.Fprint(ui.output, "\r")
+ return
+ default:
+ }
+
+ fmt.Fprintf(ui.output, "\r %s ", string(progressRunes[i]))
+ fmt.Fprint(ui.output, "Reading analysis from file...")
+
+ time.Sleep(100 * time.Millisecond)
+ i++
+ i %= 10
+ }
+}
+
func (ui *UI) updateProgress() {
emptyRow := "\r"
for j := 0; j < 100; j++ {
emptyRow += " "
}
- progressRunes := []rune(`⠇⠏⠋⠙⠹⠸⠼⠴⠦⠧`)
-
progressChan := ui.Analyzer.GetProgressChan()
doneChan := ui.Analyzer.GetDoneChan()
diff --git a/stdout/stdout_test.go b/stdout/stdout_test.go
index 1022285..fe514ef 100644
--- a/stdout/stdout_test.go
+++ b/stdout/stdout_test.go
@@ -2,6 +2,7 @@ package stdout
import (
"bytes"
+ "os"
"testing"
log "github.com/sirupsen/logrus"
@@ -127,6 +128,44 @@ func TestShowDevicesWithColor(t *testing.T) {
assert.Contains(t, output.String(), "xxx")
}
+func TestReadAnalysisWithColor(t *testing.T) {
+ input, err := os.OpenFile("../internal/testdata/test.json", os.O_RDONLY, 0644)
+ assert.Nil(t, err)
+
+ output := bytes.NewBuffer(make([]byte, 10))
+
+ ui := CreateStdoutUI(output, true, true, true)
+ err = ui.ReadAnalysis(input)
+
+ assert.Nil(t, err)
+ assert.Contains(t, output.String(), "main.go")
+}
+
+func TestReadAnalysisBw(t *testing.T) {
+ input, err := os.OpenFile("../internal/testdata/test.json", os.O_RDONLY, 0644)
+ assert.Nil(t, err)
+
+ output := bytes.NewBuffer(make([]byte, 10))
+
+ ui := CreateStdoutUI(output, false, false, false)
+ err = ui.ReadAnalysis(input)
+
+ assert.Nil(t, err)
+ assert.Contains(t, output.String(), "main.go")
+}
+
+func TestReadAnalysisWithWrongFile(t *testing.T) {
+ input, err := os.OpenFile("../internal/testdata/wrong.json", os.O_RDONLY, 0644)
+ assert.Nil(t, err)
+
+ output := bytes.NewBuffer(make([]byte, 10))
+
+ ui := CreateStdoutUI(output, true, true, true)
+ err = ui.ReadAnalysis(input)
+
+ assert.NotNil(t, err)
+}
+
func TestMaxInt(t *testing.T) {
assert.Equal(t, 5, maxInt(2, 5))
assert.Equal(t, 4, maxInt(4, 2))