Skip to content

Commit

Permalink
feat(player): Jump to section when 0-9 is pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Sep 14, 2023
1 parent 180063d commit b7b4935
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/movie/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/movie/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import (
)

func NewMovie() Movie {
return Movie{}
return Movie{
Sections: make([]int, 10),
}
}

type Movie struct {
Filename string
Cap int
Frames []Frame
Width int
Sections []int

screenStyle lipgloss.Style
}
Expand Down
9 changes: 9 additions & 0 deletions internal/movie/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions internal/movie/player_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package movie

import (
"context"
"strconv"
"time"

"github.com/charmbracelet/bubbles/key"
Expand Down Expand Up @@ -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"),
Expand All @@ -60,5 +69,6 @@ func newKeymap() keymap {
key.WithKeys(" ", "enter"),
key.WithHelp("enter", "choose"),
),
jumps: jumps,
}
}

0 comments on commit b7b4935

Please sign in to comment.