From 5a61dd98edc24ec95b9cb5e14fd13d1e4eb9b80f Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Wed, 8 Jan 2020 17:50:52 -0800 Subject: [PATCH] Improve list browsing --- CHANGELOG | 3 +++ audio.go | 2 +- gui.go | 73 +++++++++++++++++++++++----------------------------- gui_key.go | 56 ++++++++++++++++++++++++++++------------ gui_list.go | 29 +++++++++++++++++++++ gui_mouse.go | 2 +- 6 files changed, 105 insertions(+), 60 deletions(-) create mode 100644 gui_list.go diff --git a/CHANGELOG b/CHANGELOG index 8da6a6f..24bd4f1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,2 +1,5 @@ +0.1.1: +- Improve list browsing + 0.1.0: - Initial release diff --git a/audio.go b/audio.go index 5d321bb..dc41092 100644 --- a/audio.go +++ b/audio.go @@ -154,7 +154,7 @@ func nextTrack() { if mainBufferCursor-1 < len(mainBufferFiles)-1 { mainBufferCursor++ - audioFile, err := openFile(path.Join(mainBufferDirectory, mainBufferFiles[mainBufferCursor-1].File.Name())) + audioFile, err := openFile(path.Join(mainBufferDirectory, selectedEntry().File.Name())) if err != nil { return } diff --git a/gui.go b/gui.go index 054aaa6..47ae6b4 100644 --- a/gui.go +++ b/gui.go @@ -26,10 +26,10 @@ var ( topstatusbuf *cview.TextView bottomstatusbuf *cview.TextView - mainBufferText string mainBufferFiles []*LibraryEntry mainBufferCursor int mainBufferDirectory string + mainBufferOrigin int seekStart, seekEnd int volumeStart, volumeEnd int @@ -86,14 +86,6 @@ func browseFolder(browse string) { mainBufferFiles = scanFolder(browse) - var b strings.Builder - b.WriteString("..") - for _, entry := range mainBufferFiles { - b.WriteRune('\n') - - b.WriteString(entry.String()) - } - if len(mainBufferFiles) > 0 { mainBufferCursor = 1 } else { @@ -101,8 +93,6 @@ func browseFolder(browse string) { } mainBufferDirectory = browse - mainBufferText = b.String() - app.QueueUpdateDraw(updateMain) } @@ -135,28 +125,34 @@ func updateMain() { var printed int var newBufferText string - if mainBufferCursor == 0 { - newBufferText += "[::r]" + if mainBufferOrigin == 0 { + if mainBufferCursor == 0 { + newBufferText += "[::r]" + } + var line string + if mainBufferDirectory == "/" { + line = "./" + } else { + line = "../" + } + newBufferText += line + for i := len(line); i < screenWidth-2; i++ { + newBufferText += " " + } + if mainBufferCursor == 0 { + newBufferText += "[-]" + } + printed++ } - var line string - if mainBufferDirectory == "/" { - line = "./" - } else { - line = "../" - } - newBufferText += line - for i := len(line); i < screenWidth-2; i++ { - newBufferText += " " - } - if mainBufferCursor == 0 { - newBufferText += "[-]" - } - if len(mainBufferFiles) > 0 { - newBufferText += "\n" - } - printed++ - for i, entry := range mainBufferFiles { + if i < mainBufferOrigin-1 || i-mainBufferOrigin-1 > mainBufHeight-1 { + continue + } + + if printed > 0 { + newBufferText += "\n" + } + if i == mainBufferCursor-1 { newBufferText += "[::r]" } @@ -175,13 +171,9 @@ func updateMain() { } printed++ - if printed == mainBufHeight { + if printed == mainBufHeight-2 { break } - - if i < len(mainBufferFiles)-1 { - newBufferText += "\n" - } } mainbuf.SetText(newBufferText) @@ -314,17 +306,16 @@ func selectTrack() { browseFolder(path.Join(mainBufferDirectory, "..")) return } - nextStreamer = nil nextFormat = beep.Format{} - selected := mainBufferFiles[mainBufferCursor-1] - if selected.File.IsDir() { - browseFolder(path.Join(mainBufferDirectory, path.Base(selected.File.Name()))) + entry := selectedEntry() + if entry.File.IsDir() { + browseFolder(path.Join(mainBufferDirectory, path.Base(entry.File.Name()))) return } - audioFile, err := openFile(path.Join(mainBufferDirectory, mainBufferFiles[mainBufferCursor-1].File.Name())) + audioFile, err := openFile(path.Join(mainBufferDirectory, entry.File.Name())) if err != nil { statusText = err.Error() go func() { diff --git a/gui_key.go b/gui_key.go index dea07e5..2542824 100644 --- a/gui_key.go +++ b/gui_key.go @@ -43,34 +43,28 @@ func handleKeyPress(event *tcell.EventKey) *tcell.EventKey { updateStatus() return nil case 'j': - if mainBufferCursor < len(mainBufferFiles) { - mainBufferCursor++ - } - updateMain() + listNext() return nil case 'k': - if mainBufferCursor > 0 { - mainBufferCursor-- - } - updateMain() + listPrevious() return nil case 'p': if mainBufferCursor > 1 { - if mainBufferFiles[mainBufferCursor-2].File.IsDir() { + if offsetEntry(-1).File.IsDir() { return nil } - mainBufferCursor-- + listPrevious() go selectTrack() } return nil case 'n': if mainBufferCursor < len(mainBufferFiles) { - if mainBufferFiles[mainBufferCursor].File.IsDir() { + if offsetEntry(1).File.IsDir() { return nil } - mainBufferCursor++ + listNext() go selectTrack() } return nil @@ -89,16 +83,44 @@ func handleKeyPress(event *tcell.EventKey) *tcell.EventKey { case tcell.KeyEnter: go selectTrack() return nil + case tcell.KeyDown: + listNext() + return nil case tcell.KeyUp: - if mainBufferCursor > 0 { - mainBufferCursor-- + listPrevious() + return nil + case tcell.KeyPgDn: + numEntries := len(mainBufferFiles) + + if mainBufferOrigin >= numEntries-(mainBufHeight-1) { + mainBufferCursor = numEntries + + updateMain() + return nil } + + mainBufferOrigin += (mainBufHeight - 1) - 2 + if mainBufferOrigin > (numEntries-(mainBufHeight-1))+2 { + mainBufferOrigin = (numEntries - (mainBufHeight - 1)) + 2 + } + mainBufferCursor = mainBufferOrigin + updateMain() return nil - case tcell.KeyDown: - if mainBufferCursor < len(mainBufferFiles) { - mainBufferCursor++ + case tcell.KeyPgUp: + if mainBufferOrigin == 0 { + mainBufferCursor = 0 + + updateMain() + return nil } + + mainBufferOrigin -= (mainBufHeight - 1) - 2 + if mainBufferOrigin < 0 { + mainBufferOrigin = 0 + } + mainBufferCursor = mainBufferOrigin + updateMain() return nil } diff --git a/gui_list.go b/gui_list.go new file mode 100644 index 0000000..1d1bb80 --- /dev/null +++ b/gui_list.go @@ -0,0 +1,29 @@ +package main + +func listPrevious() { + if mainBufferOrigin > 0 && mainBufferCursor == mainBufferOrigin { + mainBufferOrigin-- + } + if mainBufferCursor > 0 { + mainBufferCursor-- + } + updateMain() +} + +func listNext() { + if mainBufferCursor < len(mainBufferFiles) { + mainBufferCursor++ + if mainBufferCursor-mainBufferOrigin > mainBufHeight-3 { + mainBufferOrigin++ + } + } + updateMain() +} + +func selectedEntry() *LibraryEntry { + return mainBufferFiles[mainBufferCursor-1] +} + +func offsetEntry(offset int) *LibraryEntry { + return mainBufferFiles[(mainBufferCursor-1)+offset] +} diff --git a/gui_mouse.go b/gui_mouse.go index f30b63b..8a0f036 100644 --- a/gui_mouse.go +++ b/gui_mouse.go @@ -16,7 +16,7 @@ func handleMouse(event *cview.EventMouse) *cview.EventMouse { if mouseY > 0 && mouseY < mainBufHeight+1 { // TODO Delay playing while cursor is moved if mouseY-1 < len(mainBufferFiles)+1 { - mainBufferCursor = mouseY - 1 + mainBufferCursor = mainBufferOrigin + (mouseY - 1) go selectTrack() } return nil