diff options
author | Daniel Milde <daniel.milde@firma.seznam.cz> | 2020-12-27 00:14:44 +0100 |
---|---|---|
committer | Daniel Milde <daniel.milde@firma.seznam.cz> | 2020-12-27 00:14:44 +0100 |
commit | d6112a68094a57f3d67a28ad76294f71b8a78ef3 (patch) | |
tree | 0f88f53ae9d6059a37aa45c0cb2d1f6b9e7cec34 | |
parent | d68117b1b4baff74c7938990dd0eeafd33ecf5d7 (diff) |
small refactoringv1.1.0
renaming, changing case, sorting functions
-rw-r--r-- | cli.go | 167 | ||||
-rw-r--r-- | cli_test.go | 12 | ||||
-rw-r--r-- | dir.go | 5 | ||||
-rw-r--r-- | dir_test.go | 2 | ||||
-rw-r--r-- | main.go | 4 |
5 files changed, 94 insertions, 96 deletions
@@ -22,7 +22,7 @@ type UI struct { footer *tview.TextView currentDirLabel *tview.TextView pages *tview.Pages - modal *tview.Modal + progress *tview.Modal help *tview.Flex dirContent *tview.Table currentDir *File @@ -31,8 +31,83 @@ type UI struct { askBeforeDelete bool } -// ItemSelected is called when table row is selected -func (ui *UI) ItemSelected(row, column int) { +// CreateUI creates the whole UI app +func CreateUI(topDirPath string, screen tcell.Screen) *UI { + ui := &UI{ + askBeforeDelete: true, + } + ui.topDirPath, _ = filepath.Abs(topDirPath) + + ui.app = tview.NewApplication() + ui.app.SetScreen(screen) + ui.app.SetInputCapture(ui.keyPressed) + + ui.header = tview.NewTextView() + ui.header.SetText("gdu ~ Use arrow keys to navigate, press ? for help") + ui.header.SetTextColor(tcell.ColorBlack) + ui.header.SetBackgroundColor(tcell.ColorWhite) + + ui.currentDirLabel = tview.NewTextView() + + ui.dirContent = tview.NewTable().SetSelectable(true, false) + ui.dirContent.SetSelectedFunc(ui.itemSelected) + + ui.footer = tview.NewTextView() + ui.footer.SetTextColor(tcell.ColorBlack) + ui.footer.SetBackgroundColor(tcell.ColorWhite) + ui.footer.SetText("No items to diplay.") + + grid := tview.NewGrid().SetRows(1, 1, 0, 1).SetColumns(0) + grid.AddItem(ui.header, 0, 0, 1, 1, 0, 0, false). + AddItem(ui.currentDirLabel, 1, 0, 1, 1, 0, 0, false). + AddItem(ui.dirContent, 2, 0, 1, 1, 0, 0, true). + AddItem(ui.footer, 3, 0, 1, 1, 0, 0, false) + + ui.progress = tview.NewModal().SetText("Scanning...") + + ui.pages = tview.NewPages(). + AddPage("background", grid, true, true). + AddPage("progress", ui.progress, true, true) + + ui.app.SetRoot(ui.pages, true) + + return ui +} + +// ShowDir shows content of the selected dir +func (ui *UI) ShowDir() { + ui.currentDirPath = ui.currentDir.path + ui.currentDirLabel.SetText("--- " + ui.currentDirPath + " ---") + + ui.dirContent.Clear() + + rowIndex := 0 + if ui.currentDirPath != ui.topDirPath { + cell := tview.NewTableCell(" /..") + cell.SetReference(ui.currentDir.parent) + ui.dirContent.SetCell(0, 0, cell) + rowIndex++ + } + + for i, item := range ui.currentDir.files { + cell := tview.NewTableCell(formatRow(item)) + cell.SetReference(ui.currentDir.files[i]) + ui.dirContent.SetCell(rowIndex, 0, cell) + rowIndex++ + } + + ui.dirContent.Select(0, 0) + ui.footer.SetText("Apparent size: " + formatSize(ui.currentDir.size) + " Items: " + fmt.Sprint(ui.currentDir.itemCount)) +} + +// StartUILoop starts tview application +func (ui *UI) StartUILoop() { + if err := ui.app.Run(); err != nil { + panic(err) + } +} + +func (ui *UI) itemSelected(row, column int) { selectedDir := ui.dirContent.GetCell(row, column).GetReference().(*File) if !selectedDir.isDir { return @@ -69,8 +144,7 @@ func (ui *UI) deleteSelected() { ui.ShowDir() } -// KeyPressed is called when user pressed any key -func (ui *UI) KeyPressed(key *tcell.EventKey) *tcell.EventKey { +func (ui *UI) keyPressed(key *tcell.EventKey) *tcell.EventKey { if (key.Key() == tcell.KeyEsc || key.Rune() == 'q') && ui.pages.HasPage("help") { ui.pages.RemovePage("help") return key @@ -80,7 +154,7 @@ func (ui *UI) KeyPressed(key *tcell.EventKey) *tcell.EventKey { return nil } if key.Rune() == '?' { - ui.ShowHelp() + ui.showHelp() } if key.Rune() == 'd' { if ui.askBeforeDelete { @@ -92,56 +166,6 @@ func (ui *UI) KeyPressed(key *tcell.EventKey) *tcell.EventKey { return key } -// CreateUI creates the whole UI app -func CreateUI(topDirPath string, screen tcell.Screen) *UI { - ui := &UI{ - askBeforeDelete: true, - } - ui.topDirPath, _ = filepath.Abs(topDirPath) - - ui.app = tview.NewApplication() - ui.app.SetScreen(screen) - ui.app.SetInputCapture(ui.KeyPressed) - - ui.header = tview.NewTextView() - ui.header.SetText("gdu ~ Use arrow keys to navigate, press ? for help") - ui.header.SetTextColor(tcell.ColorBlack) - ui.header.SetBackgroundColor(tcell.ColorWhite) - - ui.currentDirLabel = tview.NewTextView() - - ui.dirContent = tview.NewTable().SetSelectable(true, false) - ui.dirContent.SetSelectedFunc(ui.ItemSelected) - - ui.footer = tview.NewTextView() - ui.footer.SetTextColor(tcell.ColorBlack) - ui.footer.SetBackgroundColor(tcell.ColorWhite) - ui.footer.SetText("No items to diplay.") - - grid := tview.NewGrid().SetRows(1, 1, 0, 1).SetColumns(0) - grid.AddItem(ui.header, 0, 0, 1, 1, 0, 0, false). - AddItem(ui.currentDirLabel, 1, 0, 1, 1, 0, 0, false). - AddItem(ui.dirContent, 2, 0, 1, 1, 0, 0, true). - AddItem(ui.footer, 3, 0, 1, 1, 0, 0, false) - - ui.modal = tview.NewModal().SetText("Scanning...") - - ui.pages = tview.NewPages(). - AddPage("background", grid, true, true). - AddPage("modal", ui.modal, true, true) - - ui.app.SetRoot(ui.pages, true) - - return ui -} - -// StartUILoop starts tview application -func (ui *UI) StartUILoop() { - if err := ui.app.Run(); err != nil { - panic(err) - } -} - func (ui *UI) updateProgress(statusChannel chan CurrentProgress) { for { progress := <-statusChannel @@ -151,41 +175,14 @@ func (ui *UI) updateProgress(statusChannel chan CurrentProgress) { } ui.app.QueueUpdateDraw(func() { - ui.modal.SetText("Current item: " + progress.currentItemName) + ui.progress.SetText("Current item: " + progress.currentItemName) }) time.Sleep(100 * time.Millisecond) } } -// ShowDir shows content of the selected dir -func (ui *UI) ShowDir() { - ui.currentDirPath = ui.currentDir.path - ui.currentDirLabel.SetText("--- " + ui.currentDirPath + " ---") - - ui.dirContent.Clear() - - rowIndex := 0 - if ui.currentDirPath != ui.topDirPath { - cell := tview.NewTableCell(" /..") - cell.SetReference(ui.currentDir.parent) - ui.dirContent.SetCell(0, 0, cell) - rowIndex++ - } - - for i, item := range ui.currentDir.files { - cell := tview.NewTableCell(formatRow(item)) - cell.SetReference(ui.currentDir.files[i]) - ui.dirContent.SetCell(rowIndex, 0, cell) - rowIndex++ - } - - ui.dirContent.Select(0, 0) - ui.footer.SetText("Apparent size: " + formatSize(ui.currentDir.size) + " Items: " + fmt.Sprint(ui.currentDir.itemCount)) -} - -// ShowHelp shows help :) -func (ui *UI) ShowHelp() { +func (ui *UI) showHelp() { text := tview.NewTextView().SetText(helpText).SetDynamicColors(true) text.SetBorder(true).SetBorderPadding(2, 2, 2, 2) text.SetTitle("gdu help") diff --git a/cli_test.go b/cli_test.go index 1ec99d3..41cbd61 100644 --- a/cli_test.go +++ b/cli_test.go @@ -32,7 +32,7 @@ func TestFooter(t *testing.T) { ui.currentDir = &dir ui.ShowDir() - ui.pages.HidePage("modal") + ui.pages.HidePage("progress") ui.footer.Draw(simScreen) simScreen.Show() @@ -75,7 +75,7 @@ func TestHelp(t *testing.T) { simScreen.SetSize(50, 50) ui := CreateUI(".", simScreen) - ui.ShowHelp() + ui.showHelp() ui.help.Draw(simScreen) simScreen.Show() @@ -102,10 +102,10 @@ func TestDeleteDir(t *testing.T) { statusChannel := make(chan CurrentProgress) go ui.updateProgress(statusChannel) - ui.currentDir = processDir("test_dir", statusChannel) + ui.currentDir = ProcessDir("test_dir", statusChannel) ui.ShowDir() - ui.pages.HidePage("modal") + ui.pages.HidePage("progress") go func() { time.Sleep(100 * time.Millisecond) @@ -137,10 +137,10 @@ func TestShowConfirm(t *testing.T) { statusChannel := make(chan CurrentProgress) go ui.updateProgress(statusChannel) - ui.currentDir = processDir("test_dir", statusChannel) + ui.currentDir = ProcessDir("test_dir", statusChannel) ui.ShowDir() - ui.pages.HidePage("modal") + ui.pages.HidePage("progress") go func() { time.Sleep(100 * time.Millisecond) @@ -15,7 +15,8 @@ type CurrentProgress struct { done bool } -func processDir(path string, statusChannel chan CurrentProgress) *File { +// ProcessDir analyzes given path +func ProcessDir(path string, statusChannel chan CurrentProgress) *File { var file *File path, _ = filepath.Abs(path) @@ -40,7 +41,7 @@ func processDir(path string, statusChannel chan CurrentProgress) *File { if f.IsDir() { dirCount++ go func(subDirsChan chan *File, f os.FileInfo) { - file = processDir(filepath.Join(path, f.Name()), statusChannel) + file = ProcessDir(filepath.Join(path, f.Name()), statusChannel) file.parent = &dir subDirsChan <- file }(subDirsChan, f) diff --git a/dir_test.go b/dir_test.go index f7d0cf4..ec3e4b9 100644 --- a/dir_test.go +++ b/dir_test.go @@ -23,7 +23,7 @@ func TestProcessDir(t *testing.T) { statusChannel := make(chan CurrentProgress) - dir := processDir("test_dir", statusChannel) + dir := ProcessDir("test_dir", statusChannel) // test dir info assert.Equal(t, "test_dir", dir.name) @@ -25,11 +25,11 @@ func main() { go ui.updateProgress(statusChannel) go func() { - ui.currentDir = processDir(topDir, statusChannel) + ui.currentDir = ProcessDir(topDir, statusChannel) ui.app.QueueUpdateDraw(func() { ui.ShowDir() - ui.pages.HidePage("modal") + ui.pages.HidePage("progress") }) }() |