diff --git a/pkg/analyze/file.go b/pkg/analyze/file.go index 21e8f0662..481944fa7 100644 --- a/pkg/analyze/file.go +++ b/pkg/analyze/file.go @@ -15,6 +15,7 @@ type Item interface { GetFlag() rune IsDir() bool GetSize() int64 + GetType() string GetUsage() int64 GetItemCount() int GetParent() *Dir @@ -31,6 +32,11 @@ type File struct { Parent *Dir } +// GetName returns name of dir +func (f *File) GetName() string { + return f.Name +} + // IsDir returns false for file func (f *File) IsDir() bool { return false @@ -61,6 +67,15 @@ func (f *File) GetUsage() int64 { return f.Usage } +// GetType returns name type of item +func (f *File) GetType() string { + switch f.Flag { + case '@': + return "Other" + } + return "File" +} + // GetItemCount returns 1 for file func (f *File) GetItemCount() int { return 1 @@ -94,9 +109,9 @@ type Dir struct { Files Files } -// GetName returns name of dir -func (f *File) GetName() string { - return f.Name +// GetType returns name type of item +func (f *Dir) GetType() string { + return "Directory" } // GetItemCount returns number of files in dir diff --git a/pkg/analyze/file_test.go b/pkg/analyze/file_test.go index a92fcba2a..fbd916259 100644 --- a/pkg/analyze/file_test.go +++ b/pkg/analyze/file_test.go @@ -27,6 +27,33 @@ func TestIsDir(t *testing.T) { assert.False(t, file.IsDir()) } +func TestGetType(t *testing.T) { + dir := Dir{ + File: &File{ + Name: "xxx", + Size: 5, + }, + ItemCount: 2, + } + file := &File{ + Name: "yyy", + Size: 2, + Parent: &dir, + Flag: ' ', + } + file2 := &File{ + Name: "yyy", + Size: 2, + Parent: &dir, + Flag: '@', + } + dir.Files = Files{file, file2} + + assert.Equal(t, "Directory", dir.GetType()) + assert.Equal(t, "File", file.GetType()) + assert.Equal(t, "Other", file2.GetType()) +} + func TestFind(t *testing.T) { dir := Dir{ File: &File{ diff --git a/tui/actions.go b/tui/actions.go index 74f4f524d..a5663d3cf 100644 --- a/tui/actions.go +++ b/tui/actions.go @@ -2,6 +2,7 @@ package tui import ( "bufio" + "fmt" "os" "path/filepath" "runtime" @@ -212,3 +213,47 @@ func (ui *UI) showFile() *tview.TextView { return file } + +func (ui *UI) showInfo() { + var content, numberColor string + row, column := ui.table.GetSelection() + selectedFile := ui.table.GetCell(row, column).GetReference().(analyze.Item) + + if selectedFile == ui.currentDir.Parent { + return + } + + if ui.UseColors { + numberColor = "[#e67100::b]" + } else { + numberColor = "[::b]" + } + + text := tview.NewTextView().SetDynamicColors(true) + text.SetBorder(true).SetBorderPadding(2, 2, 2, 2) + text.SetBorderColor(tcell.ColorDefault) + text.SetTitle(" Item info ") + + content += "[::b]Name:[::-] " + selectedFile.GetName() + "\n" + content += "[::b]Path:[::-] " + selectedFile.GetPath() + "\n" + content += "[::b]Type:[::-] " + selectedFile.GetType() + "\n\n" + + content += " [::b]Disk usage:[::-] " + content += numberColor + ui.formatSize(selectedFile.GetUsage(), false, true) + content += fmt.Sprintf(" (%s%d[-::] B)", numberColor, selectedFile.GetUsage()) + "\n" + content += "[::b]Apparent size:[::-] " + content += numberColor + ui.formatSize(selectedFile.GetSize(), false, true) + content += fmt.Sprintf(" (%s%d[-::] B)", numberColor, selectedFile.GetSize()) + "\n" + + text.SetText(content) + + flex := tview.NewFlex(). + AddItem(nil, 0, 1, false). + AddItem(tview.NewFlex().SetDirection(tview.FlexRow). + AddItem(nil, 0, 1, false). + AddItem(text, 13, 1, false). + AddItem(nil, 0, 1, false), 80, 1, false). + AddItem(nil, 0, 1, false) + + ui.pages.AddPage("info", flex, true, true) +} diff --git a/tui/actions_test.go b/tui/actions_test.go index a8655e741..6daf972bf 100644 --- a/tui/actions_test.go +++ b/tui/actions_test.go @@ -218,6 +218,60 @@ func TestViewFile(t *testing.T) { assert.Equal(t, 'j', event.Rune()) } +func TestShowInfo(t *testing.T) { + fin := testdir.CreateTestDir() + defer fin() + + app := testapp.CreateMockedApp(true) + ui := CreateUI(app, false, true) + ui.done = make(chan struct{}) + ui.AnalyzePath("test_dir", nil) + + <-ui.done // wait for analyzer + + assert.Equal(t, "test_dir", ui.currentDir.Name) + + for _, f := range ui.app.(*testapp.MockedApp).UpdateDraws { + f() + } + + ui.keyPressed(tcell.NewEventKey(tcell.KeyRight, 'l', 0)) + ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'i', 0)) + ui.table.Select(2, 0) + ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'i', 0)) + + assert.True(t, ui.pages.HasPage("info")) + + ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'q', 0)) + + assert.False(t, ui.pages.HasPage("info")) +} + +func TestShowInfoBW(t *testing.T) { + fin := testdir.CreateTestDir() + defer fin() + + app := testapp.CreateMockedApp(true) + ui := CreateUI(app, true, false) + ui.done = make(chan struct{}) + ui.AnalyzePath("test_dir", nil) + + <-ui.done // wait for analyzer + + assert.Equal(t, "test_dir", ui.currentDir.Name) + + for _, f := range ui.app.(*testapp.MockedApp).UpdateDraws { + f() + } + + ui.keyPressed(tcell.NewEventKey(tcell.KeyRight, 'l', 0)) + ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'i', 0)) + ui.table.Select(2, 0) + ui.keyPressed(tcell.NewEventKey(tcell.KeyRune, 'i', 0)) + + assert.True(t, ui.pages.HasPage("info")) +} + func TestExitViewFile(t *testing.T) { fin := testdir.CreateTestDir() defer fin() diff --git a/tui/keys.go b/tui/keys.go index 482687953..8c2b59215 100644 --- a/tui/keys.go +++ b/tui/keys.go @@ -17,6 +17,12 @@ func (ui *UI) keyPressed(key *tcell.EventKey) *tcell.EventKey { ui.app.SetFocus(page) return nil } + if ui.pages.HasPage("info") { + ui.pages.RemovePage("info") + _, page := ui.pages.GetFrontPage() + ui.app.SetFocus(page) + return nil + } } switch key.Rune() { @@ -27,7 +33,10 @@ func (ui *UI) keyPressed(key *tcell.EventKey) *tcell.EventKey { ui.showHelp() } - if ui.pages.HasPage("confirm") || ui.pages.HasPage("progress") || ui.pages.HasPage("deleting") { + if ui.pages.HasPage("confirm") || + ui.pages.HasPage("progress") || + ui.pages.HasPage("deleting") || + ui.pages.HasPage("info") { return key } @@ -46,6 +55,8 @@ func (ui *UI) keyPressed(key *tcell.EventKey) *tcell.EventKey { ui.handleDelete() case 'v': ui.showFile() + case 'i': + ui.showInfo() case 'a': ui.ShowApparentSize = !ui.ShowApparentSize if ui.currentDir != nil { diff --git a/tui/tui.go b/tui/tui.go index 1c23aa73d..d0f753c9b 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -22,6 +22,7 @@ const helpTextColorized = ` [red]left, h [white]Go to parent directory [red]d [white]Delete selected file or directory [red]v [white]Show content of selected file + [red]i [white]Show info about selected item [red]r [white]Rescan current directory [red]a [white]Toggle between showing disk usage and apparent size [red]c [white]Show/hide file count @@ -37,6 +38,7 @@ const helpText = ` [::b]left, h [white:black:-]Go to parent directory [::b]d [white:black:-]Delete selected file or directory [::b]v [white:black:-]Show content of selected file + [::b]i [white:black:-]Show info about selected item [::b]r [white:black:-]Rescan current directory [::b]a [white:black:-]Toggle between showing disk usage and apparent size [::b]c [white:black:-]Show/hide file count @@ -378,7 +380,7 @@ func (ui *UI) showHelp() { AddItem(nil, 0, 1, false). AddItem(tview.NewFlex().SetDirection(tview.FlexRow). AddItem(nil, 0, 1, false). - AddItem(text, 20, 1, false). + AddItem(text, 21, 1, false). AddItem(nil, 0, 1, false), 80, 1, false). AddItem(nil, 0, 1, false) diff --git a/tui/tui_test.go b/tui/tui_test.go index 1b3b4c789..26dc5bc9b 100644 --- a/tui/tui_test.go +++ b/tui/tui_test.go @@ -87,7 +87,7 @@ func TestHelp(t *testing.T) { b, _, _ := simScreen.GetContents() - cells := b[256 : 256+9] + cells := b[306 : 306+9] text := []byte("directory") for i, r := range cells { @@ -106,7 +106,7 @@ func TestHelpBw(t *testing.T) { b, _, _ := simScreen.GetContents() - cells := b[256 : 256+9] + cells := b[306 : 306+9] text := []byte("directory") for i, r := range cells {