Browse Source

Improve list browsing

master v0.1.1
Trevor Slocum 3 years ago
parent
commit
5a61dd98ed
  1. 3
      CHANGELOG
  2. 2
      audio.go
  3. 73
      gui.go
  4. 56
      gui_key.go
  5. 29
      gui_list.go
  6. 2
      gui_mouse.go

3
CHANGELOG

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

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

73
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]"
}
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"
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++
}
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() {

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

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

2
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

Loading…
Cancel
Save