Improve list browsing

This commit is contained in:
Trevor Slocum 2020-01-08 17:50:52 -08:00
parent 27bf854dcf
commit 5a61dd98ed
6 changed files with 105 additions and 60 deletions

View file

@ -1,2 +1,5 @@
0.1.1:
- Improve list browsing
0.1.0:
- Initial release

View file

@ -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
}

73
gui.go
View file

@ -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() {

View file

@ -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
}

29
gui_list.go Normal file
View file

@ -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]
}

View file

@ -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