From b7b49357524d0b59bdc73c865994a739d54c67cd Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Thu, 14 Sep 2023 13:13:49 -0500 Subject: [PATCH] feat(player): Jump to section when 0-9 is pressed --- internal/movie/file.go | 4 ++++ internal/movie/movie.go | 5 ++++- internal/movie/player.go | 9 +++++++++ internal/movie/player_events.go | 10 ++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/internal/movie/file.go b/internal/movie/file.go index 81db0056..fba6223b 100644 --- a/internal/movie/file.go +++ b/internal/movie/file.go @@ -71,6 +71,10 @@ func (m *Movie) LoadFile(path string, src io.Reader, speed float64) error { for i, f := range m.Frames { f.Progress = bar.Generate(currentPosition+f.Duration/2, totalDuration, m.Width+2) m.Frames[i] = f + percent := int(currentPosition * 10 / totalDuration) + if percent < len(m.Sections)-1 { + m.Sections[percent+1] = i + } if frameCap < len(f.Data) { frameCap = len(f.Data) } diff --git a/internal/movie/movie.go b/internal/movie/movie.go index 27cc7f23..4eae7071 100644 --- a/internal/movie/movie.go +++ b/internal/movie/movie.go @@ -7,7 +7,9 @@ import ( ) func NewMovie() Movie { - return Movie{} + return Movie{ + Sections: make([]int, 10), + } } type Movie struct { @@ -15,6 +17,7 @@ type Movie struct { Cap int Frames []Frame Width int + Sections []int screenStyle lipgloss.Style } diff --git a/internal/movie/player.go b/internal/movie/player.go index 3f502114..dc893bde 100644 --- a/internal/movie/player.go +++ b/internal/movie/player.go @@ -123,6 +123,15 @@ func (p Player) Update(msg tea.Msg) (tea.Model, tea.Cmd) { p.selectedOption = 0 case key.Matches(msg, p.keymap.end): p.selectedOption = len(playerOptions) - 1 + case key.Matches(msg, p.keymap.jumps...): + for i, binding := range p.keymap.jumps { + if key.Matches(msg, binding) { + p.frame = p.movie.Sections[i] + if p.isPlaying() { + return p, p.play() + } + } + } } case quitMsg: if p.log != nil { diff --git a/internal/movie/player_events.go b/internal/movie/player_events.go index f1251a61..11851854 100644 --- a/internal/movie/player_events.go +++ b/internal/movie/player_events.go @@ -2,6 +2,7 @@ package movie import ( "context" + "strconv" "time" "github.com/charmbracelet/bubbles/key" @@ -34,9 +35,17 @@ type keymap struct { home key.Binding end key.Binding choose key.Binding + jumps []key.Binding } func newKeymap() keymap { + jumps := make([]key.Binding, 0, 10) + for i := 0; i < 10; i += 1 { + jumps = append(jumps, key.NewBinding( + key.WithKeys(strconv.Itoa(i)), + )) + } + return keymap{ quit: key.NewBinding( key.WithKeys("q", "ctrl+c", "ctrl+d", "esc"), @@ -60,5 +69,6 @@ func newKeymap() keymap { key.WithKeys(" ", "enter"), key.WithHelp("enter", "choose"), ), + jumps: jumps, } }