Skip to content

Commit

Permalink
change: lyrics uses binary search instead of linear search to find cu…
Browse files Browse the repository at this point in the history
…rrent lyric.
  • Loading branch information
xxxserxxx committed Dec 24, 2024
1 parent bbeefd1 commit 847edf0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
28 changes: 15 additions & 13 deletions event_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"sort"
"time"

"github.com/spezifisch/stmps/mpvplayer"
Expand Down Expand Up @@ -86,20 +87,21 @@ func (ui *Ui) guiEventLoop() {
// in the future
p := statusData.Position*1000 + 500
_, _, _, fh := ui.queuePage.lyrics.GetInnerRect()
// FIXME (A) the lyrics lookup would perform better as a binary search
for i := 0; i < lcl-1; i++ {
if p >= cl[i].Start && p < cl[i+1].Start {
txt := ""
if i > 0 {
txt = cl[i-1].Value + "\n"
}
txt += "[::b]" + cl[i].Value + "[-:-:-]\n"
for k := i + 1; k < lcl && k-i < fh; k++ {
txt += cl[k].Value + "\n"
}
ui.queuePage.lyrics.SetText(txt)
break
i := sort.Search(len(cl), func(i int) bool {
return p < cl[i].Start
})
if i < lcl && p < cl[i].Start {
txt := ""
if i > 1 {
txt = cl[i-2].Value + "\n"
}
if i > 0 {
txt += "[::b]" + cl[i-1].Value + "[-:-:-]\n"
}
for k := i; k < lcl && k-i < fh; k++ {
txt += cl[k].Value + "\n"
}
ui.queuePage.lyrics.SetText(txt)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions page_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (ui *Ui) createQueuePage() *QueuePage {
queuePage.lyrics.SetDynamicColors(true).SetScrollable(true)
queuePage.lyrics.SetWrap(true)
queuePage.lyrics.SetWordWrap(true)
queuePage.lyrics.SetTextAlign(tview.AlignCenter)
queuePage.lyrics.SetBorderPadding(1, 1, 1, 1)

queuePage.queueList.SetSelectionChangedFunc(queuePage.changeSelection)
Expand Down
1 change: 1 addition & 0 deletions stmps.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
// TODO (A) Add mocking library
// TODO (C) Get unit tests up to some non-embarassing percentage
// TODO (B) Merge feature_27_save_queue / issue-54-save-queue-on-exit / seekable-queue-load, and finish the restoring play location on first run, or hotkey
// TODO (C) Support "Download" for songs, albums, artists, and playlists

var osExit = os.Exit // A variable to allow mocking os.Exit in tests
var headlessMode bool // This can be set to true during tests
Expand Down

0 comments on commit 847edf0

Please sign in to comment.