From affb5fbaba22867c357d8bfef3d942e3194b9547 Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sun, 18 Feb 2024 18:51:25 -0800 Subject: [PATCH] refac --- .goreleaser.yaml | 2 +- .woodpecker.yml | 2 +- Makefile | 18 +- completions/{_gospt-ng => _gspot} | 4 +- completions/{gospt-ng => gspot} | 0 go.mod | 2 +- main.go | 8 +- src/app/fx.go | 6 +- src/components/cache/cache.go | 2 +- src/components/cli/cli.go | 22 ++- src/components/commands/activate_device.go | 6 +- src/components/commands/commander.go | 2 +- src/components/commands/devices.go | 2 +- src/components/commands/next.go | 114 ++++++++++-- src/components/commands/radio.go | 17 +- src/components/commands/youtube-link.go | 2 +- src/components/tui/handlers.go | 120 ------------- src/components/tui/main.go | 198 ++++++++++++++++----- src/components/tui/tui.go | 2 +- src/components/tui/views.go | 2 +- src/components/youtube/youtube.go | 2 +- src/services/auth.go | 8 +- 22 files changed, 322 insertions(+), 219 deletions(-) rename completions/{_gospt-ng => _gspot} (86%) rename completions/{gospt-ng => gspot} (100%) delete mode 100644 src/components/tui/handlers.go diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 3f906f3..10a8385 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -18,7 +18,7 @@ builds: - goos: windows goarch: "386" ldflags: - - -s -w -X git.asdf.cafe/abs3nt/gospt-ng/src/components/cli.Version={{.Version}} + - -s -w -X git.asdf.cafe/abs3nt/gspot/src/components/cli.Version=({{.Version}} - {{.Now}}) archives: - format: tar.gz diff --git a/.woodpecker.yml b/.woodpecker.yml index 9bd309c..8fa04b2 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -3,7 +3,7 @@ steps: image: golang:1.22 commands: - go mod tidy - - go build -o gospt-ng + - go build -o gspot publish: image: goreleaser/goreleaser:nightly diff --git a/Makefile b/Makefile index 4f259e5..a00fdab 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ -build: gospt-ng +build: gspot -gospt-ng: $(shell find . -name '*.go') - go build -ldflags="-X 'git.asdf.cafe/abs3nt/gospt-ng/src/components/cli.Version=$(shell git rev-parse --short HEAD)'" -o dist/ . +gspot: $(shell find . -name '*.go') + go build -ldflags="-X 'git.asdf.cafe/abs3nt/gspot/src/components/cli.Version=$(shell git show -s --date=short --pretty='format:%h (%ad)' HEAD)'" -o dist/ . run: go run main.go @@ -13,11 +13,11 @@ clean: rm -rf bin uninstall: - rm -f /usr/bin/gospt-ng - rm -f /usr/share/zsh/site-functions/_gospt-ng - rm -f /usr/share/bash-completion/completions/gospt-ng + rm -f /usr/bin/gspot + rm -f /usr/share/zsh/site-functions/_gspot + rm -f /usr/share/bash-completion/completions/gspot install: - cp ./dist/gospt-ng /usr/bin - cp ./completions/_gospt-ng /usr/share/zsh/site-functions/_gospt-ng - cp ./completions/gospt-ng /usr/share/bash-completion/completions/gospt-ng + cp ./dist/gspot /usr/bin + cp ./completions/_gspot /usr/share/zsh/site-functions/_gspot + cp ./completions/gspot /usr/share/bash-completion/completionsgspotg diff --git a/completions/_gospt-ng b/completions/_gspot similarity index 86% rename from completions/_gospt-ng rename to completions/_gspot index 3177266..ced9c94 100755 --- a/completions/_gospt-ng +++ b/completions/_gspot @@ -1,4 +1,4 @@ -#compdef gospt-ng +#compdef gspot _cli_zsh_autocomplete() { local -a opts @@ -17,4 +17,4 @@ _cli_zsh_autocomplete() { fi } -compdef _cli_zsh_autocomplete gospt-ng +compdef _cli_zsh_autocomplete gspot diff --git a/completions/gospt-ng b/completions/gspot similarity index 100% rename from completions/gospt-ng rename to completions/gspot diff --git a/go.mod b/go.mod index 4f5fa5d..4f3ebd8 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module git.asdf.cafe/abs3nt/gospt-ng +module git.asdf.cafe/abs3nt/gspot go 1.22.0 diff --git a/main.go b/main.go index 0e8593c..0f8912a 100644 --- a/main.go +++ b/main.go @@ -3,10 +3,10 @@ package main import ( "go.uber.org/fx" - "git.asdf.cafe/abs3nt/gospt-ng/src/app" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/cache" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/cli" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/commands" + "git.asdf.cafe/abs3nt/gspot/src/app" + "git.asdf.cafe/abs3nt/gspot/src/components/cache" + "git.asdf.cafe/abs3nt/gspot/src/components/cli" + "git.asdf.cafe/abs3nt/gspot/src/components/commands" ) func main() { diff --git a/src/app/fx.go b/src/app/fx.go index c8f8cd7..9d8cc30 100644 --- a/src/app/fx.go +++ b/src/app/fx.go @@ -9,8 +9,8 @@ import ( "github.com/lmittmann/tint" "go.uber.org/fx" - "git.asdf.cafe/abs3nt/gospt-ng/src/config" - "git.asdf.cafe/abs3nt/gospt-ng/src/services" + "git.asdf.cafe/abs3nt/gspot/src/config" + "git.asdf.cafe/abs3nt/gspot/src/services" ) var Services = fx.Options( @@ -31,7 +31,7 @@ var Config = fx.Options( fx.Provide( func() *config.Config { c := &config.Config{} - gunner.LoadApp(c, "gospt") + gunner.LoadApp(c, "gspot") return c }, ), diff --git a/src/components/cache/cache.go b/src/components/cache/cache.go index ae898fa..f547615 100644 --- a/src/components/cache/cache.go +++ b/src/components/cache/cache.go @@ -35,7 +35,7 @@ type CacheParams struct { func NewCache(p CacheParams) CacheResult { c := &Cache{ - Root: filepath.Join(os.TempDir(), "gospt.cache"), + Root: filepath.Join(os.TempDir(), "gspot.cache"), Log: p.Log, } return CacheResult{ diff --git a/src/components/cli/cli.go b/src/components/cli/cli.go index 6ba3e0a..02f2e81 100644 --- a/src/components/cli/cli.go +++ b/src/components/cli/cli.go @@ -9,8 +9,8 @@ import ( "github.com/zmb3/spotify/v2" "go.uber.org/fx" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/commands" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/tui" + "git.asdf.cafe/abs3nt/gspot/src/components/commands" + "git.asdf.cafe/abs3nt/gspot/src/components/tui" ) var Version = "dev" @@ -23,6 +23,7 @@ func Run(c *commands.Commander, s fx.Shutdowner) { } }() app := &cli.App{ + Name: "gspot", EnableBashCompletion: true, Version: Version, Action: func(ctx *cli.Context) error { @@ -95,11 +96,20 @@ func Run(c *commands.Commander, s fx.Shutdowner) { Category: "Sharing", }, { - Name: "next", - Aliases: []string{"n", "skip"}, - Usage: "Skips to the next song", + Name: "next", + Aliases: []string{"n", "skip"}, + Usage: "Skips to the next song", + Args: true, + ArgsUsage: "amount", Action: func(cCtx *cli.Context) error { - return c.Next() + if cCtx.NArg() > 0 { + amt, err := strconv.Atoi(cCtx.Args().First()) + if err != nil { + return err + } + return c.Next(amt, false) + } + return c.Next(1, false) }, Category: "Playback", }, diff --git a/src/components/commands/activate_device.go b/src/components/commands/activate_device.go index 614d1c4..1379e3b 100644 --- a/src/components/commands/activate_device.go +++ b/src/components/commands/activate_device.go @@ -12,8 +12,8 @@ import ( func (c *Commander) activateDevice() (spotify.ID, error) { var device *spotify.PlayerDevice configDir, _ := os.UserConfigDir() - if _, err := os.Stat(filepath.Join(configDir, "gospt/device.json")); err == nil { - deviceFile, err := os.Open(filepath.Join(configDir, "gospt/device.json")) + if _, err := os.Stat(filepath.Join(configDir, "gspot/device.json")); err == nil { + deviceFile, err := os.Open(filepath.Join(configDir, "gspot/device.json")) if err != nil { return "", err } @@ -31,7 +31,7 @@ func (c *Commander) activateDevice() (spotify.ID, error) { return "", err } } else { - c.Log.Error("COMMANDER", "failed to activated device", "YOU MUST RUN gospt setdevice FIRST") + c.Log.Error("COMMANDER", "failed to activated device", "YOU MUST RUN gspot setdevice FIRST") } return device.ID, nil } diff --git a/src/components/commands/commander.go b/src/components/commands/commander.go index 32cd8fb..5dc9e29 100644 --- a/src/components/commands/commander.go +++ b/src/components/commands/commander.go @@ -8,7 +8,7 @@ import ( "github.com/zmb3/spotify/v2" "go.uber.org/fx" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/cache" + "git.asdf.cafe/abs3nt/gspot/src/components/cache" ) type CommanderResult struct { diff --git a/src/components/commands/devices.go b/src/components/commands/devices.go index 03c4461..3e41461 100644 --- a/src/components/commands/devices.go +++ b/src/components/commands/devices.go @@ -42,7 +42,7 @@ func (c *Commander) SetDevice(device spotify.ID) error { return err } configDir, _ := os.UserConfigDir() - err = os.WriteFile(filepath.Join(configDir, "gospt/device.json"), out, 0o600) + err = os.WriteFile(filepath.Join(configDir, "gspot/device.json"), out, 0o600) if err != nil { return err } diff --git a/src/components/commands/next.go b/src/components/commands/next.go index 88342ca..75bc168 100644 --- a/src/components/commands/next.go +++ b/src/components/commands/next.go @@ -1,25 +1,117 @@ package commands import ( + "strings" + "github.com/zmb3/spotify/v2" ) -func (c *Commander) Next() error { - err := c.Client.Next(c.Context) - if err != nil { - if isNoActiveError(err) { - deviceId, err := c.activateDevice() - if err != nil { - return err - } - err = c.Client.NextOpt(c.Context, &spotify.PlayOptions{ - DeviceID: &deviceId, - }) +func (c *Commander) Next(amt int, inqueue bool) error { + if inqueue { + for i := 0; i < amt; i++ { + err := c.Client.Next(c.Context) if err != nil { return err } } + return nil + } + if amt == 1 { + err := c.Client.Next(c.Context) + if err != nil { + if isNoActiveError(err) { + deviceId, err := c.activateDevice() + if err != nil { + return err + } + err = c.Client.NextOpt(c.Context, &spotify.PlayOptions{ + DeviceID: &deviceId, + }) + if err != nil { + return err + } + } + return err + } + return nil + } + // found := false + // playingIndex := 0 + current, err := c.Client.PlayerCurrentlyPlaying(c.Context) + if err != nil { return err } + playbackContext := current.PlaybackContext.Type + switch playbackContext { + case "playlist": + found := false + currentTrackIndex := 0 + page := 1 + for !found { + playlist, err := c.Client. + GetPlaylistItems( + c.Context, + spotify.ID(strings.Split(string(current.PlaybackContext.URI), ":")[2]), + spotify.Limit(50), + spotify.Offset((page-1)*50), + ) + if err != nil { + return err + } + for idx, track := range playlist.Items { + if track.Track.Track.ID == current.Item.ID { + currentTrackIndex = idx + (50 * (page - 1)) + found = true + break + } + } + page++ + } + pos := currentTrackIndex + amt + return c.Client.PlayOpt(c.Context, &spotify.PlayOptions{ + PlaybackContext: ¤t.PlaybackContext.URI, + PlaybackOffset: &spotify.PlaybackOffset{ + Position: &pos, + }, + }) + case "album": + found := false + currentTrackIndex := 0 + page := 1 + for !found { + playlist, err := c.Client. + GetAlbumTracks( + c.Context, + spotify.ID(strings.Split(string(current.PlaybackContext.URI), ":")[2]), + spotify.Limit(50), + spotify.Offset((page-1)*50), + ) + if err != nil { + return err + } + for idx, track := range playlist.Tracks { + if track.ID == current.Item.ID { + currentTrackIndex = idx + (50 * (page - 1)) + found = true + break + } + } + page++ + } + pos := currentTrackIndex + amt + return c.Client.PlayOpt(c.Context, &spotify.PlayOptions{ + PlaybackContext: ¤t.PlaybackContext.URI, + PlaybackOffset: &spotify.PlaybackOffset{ + Position: &pos, + }, + }) + default: + for i := 0; i < amt; i++ { + err := c.Client.Next(c.Context) + if err != nil { + return err + } + } + } return nil } diff --git a/src/components/commands/radio.go b/src/components/commands/radio.go index 784060e..51d6595 100644 --- a/src/components/commands/radio.go +++ b/src/components/commands/radio.go @@ -44,7 +44,12 @@ func (c *Commander) RadioFromPlaylist(playlist spotify.SimplePlaylist) error { if pages > 1 { randomPage = frand.Intn(pages-1) + 1 } - playlistPage, err := c.Client.GetPlaylistItems(c.Context, playlist.ID, spotify.Limit(50), spotify.Offset((randomPage-1)*50)) + playlistPage, err := c.Client.GetPlaylistItems( + c.Context, + playlist.ID, + spotify.Limit(50), + spotify.Offset((randomPage-1)*50), + ) if err != nil { return err } @@ -287,14 +292,14 @@ func (c *Commander) ClearRadio() error { } _, _ = db.Query("DROP TABLE IF EXISTS radio") configDir, _ := os.UserConfigDir() - os.Remove(filepath.Join(configDir, "gospt/radio.json")) + os.Remove(filepath.Join(configDir, "gspot/radio.json")) _ = c.Client.Pause(c.Context) return nil } func (c *Commander) GetRadioPlaylist(name string) (*spotify.FullPlaylist, *sql.DB, error) { configDir, _ := os.UserConfigDir() - playlistFile, err := os.ReadFile(filepath.Join(configDir, "gospt/radio.json")) + playlistFile, err := os.ReadFile(filepath.Join(configDir, "gspot/radio.json")) if errors.Is(err, os.ErrNotExist) { return c.CreateRadioPlaylist(name) } @@ -306,7 +311,7 @@ func (c *Commander) GetRadioPlaylist(name string) (*spotify.FullPlaylist, *sql.D if err != nil { return nil, nil, err } - db, err := sql.Open("sqlite", filepath.Join(configDir, "gospt/radio.db")) + db, err := sql.Open("sqlite", filepath.Join(configDir, "gspot/radio.db")) if err != nil { return nil, nil, err } @@ -325,11 +330,11 @@ func (c *Commander) CreateRadioPlaylist(name string) (*spotify.FullPlaylist, *sq if err != nil { return nil, nil, err } - err = os.WriteFile(filepath.Join(configDir, "gospt/radio.json"), raw, 0o600) + err = os.WriteFile(filepath.Join(configDir, "gspot/radio.json"), raw, 0o600) if err != nil { return nil, nil, err } - db, err := sql.Open("sqlite", filepath.Join(configDir, "gospt/radio.db")) + db, err := sql.Open("sqlite", filepath.Join(configDir, "gspot/radio.db")) if err != nil { return nil, nil, err } diff --git a/src/components/commands/youtube-link.go b/src/components/commands/youtube-link.go index eb2bc09..e083725 100644 --- a/src/components/commands/youtube-link.go +++ b/src/components/commands/youtube-link.go @@ -3,7 +3,7 @@ package commands import ( "fmt" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/youtube" + "git.asdf.cafe/abs3nt/gspot/src/components/youtube" ) func (c *Commander) PrintYoutubeLink() error { diff --git a/src/components/tui/handlers.go b/src/components/tui/handlers.go deleted file mode 100644 index 0e79df3..0000000 --- a/src/components/tui/handlers.go +++ /dev/null @@ -1,120 +0,0 @@ -package tui - -import ( - "github.com/zmb3/spotify/v2" - - "git.asdf.cafe/abs3nt/gospt-ng/src/components/commands" -) - -func HandlePlayWithContext(commands *commands.Commander, uri *spotify.URI, pos *int) { - err := commands.PlaySongInPlaylist(uri, pos) - if err != nil { - return - } -} - -func HandleRadio(commands *commands.Commander, song spotify.SimpleTrack) { - err := commands.RadioGivenSong(song, 0) - if err != nil { - return - } -} - -func HandleAlbumRadio(commands *commands.Commander, album spotify.SimpleAlbum) { - err := commands.RadioFromAlbum(album) - if err != nil { - return - } -} - -func HandleSeek(commands *commands.Commander, fwd bool) { - err := commands.Seek(fwd) - if err != nil { - return - } -} - -func HandleVolume(commands *commands.Commander, up bool) { - vol := 10 - if !up { - vol = -10 - } - err := commands.ChangeVolume(vol) - if err != nil { - return - } -} - -func HandleArtistRadio(commands *commands.Commander, artist spotify.SimpleArtist) { - err := commands.RadioGivenArtist(artist) - if err != nil { - return - } -} - -func HandleAlbumArtist(commands *commands.Commander, artist spotify.SimpleArtist) { - err := commands.RadioGivenArtist(artist) - if err != nil { - return - } -} - -func HandlePlaylistRadio(commands *commands.Commander, playlist spotify.SimplePlaylist) { - err := commands.RadioFromPlaylist(playlist) - if err != nil { - return - } -} - -func HandleLibraryRadio(commands *commands.Commander) { - err := commands.RadioFromSavedTracks() - if err != nil { - return - } -} - -func HandlePlayLikedSong(commands *commands.Commander, position int) { - err := commands.PlayLikedSongs(position) - if err != nil { - return - } -} - -func HandlePlayTrack(commands *commands.Commander, track spotify.ID) { - err := commands.QueueSong(track) - if err != nil { - return - } - err = commands.Next() - if err != nil { - return - } -} - -func HandleNextInQueue(commands *commands.Commander, amt int) { - err := commands.Next() - if err != nil { - return - } -} - -func HandleQueueItem(commands *commands.Commander, item spotify.ID) { - err := commands.QueueSong(item) - if err != nil { - return - } -} - -func HandleDeleteTrackFromPlaylist(commands *commands.Commander, item, playlist spotify.ID) { - err := commands.DeleteTracksFromPlaylist([]spotify.ID{item}, playlist) - if err != nil { - return - } -} - -func HandleSetDevice(commands *commands.Commander, player spotify.PlayerDevice) { - err := commands.SetDevice(player.ID) - if err != nil { - return - } -} diff --git a/src/components/tui/main.go b/src/components/tui/main.go index 6849caf..e5f0ec7 100644 --- a/src/components/tui/main.go +++ b/src/components/tui/main.go @@ -14,7 +14,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/zmb3/spotify/v2" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/commands" + "git.asdf.cafe/abs3nt/gspot/src/components/commands" ) var ( @@ -93,37 +93,92 @@ func (m *mainModel) PlayRadio() { selectedItem := m.list.SelectedItem().(mainItem).SpotifyItem switch item := selectedItem.(type) { case spotify.SimplePlaylist: - go HandlePlaylistRadio(m.commands, item) + go func() { + err := m.commands.RadioFromPlaylist(item) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case *spotify.SavedTrackPage: - go HandleLibraryRadio(m.commands) + go func() { + err := m.commands.RadioFromSavedTracks() + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.SimpleAlbum: - go HandleAlbumRadio(m.commands, item) + go func() { + err := m.commands.RadioFromAlbum(item) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.FullAlbum: - go HandleAlbumRadio(m.commands, item.SimpleAlbum) + go func() { + err := m.commands.RadioFromAlbum(item.SimpleAlbum) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.SimpleArtist: - go HandleArtistRadio(m.commands, item) + go func() { + err := m.commands.RadioGivenArtist(item) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.FullArtist: - go HandleArtistRadio(m.commands, item.SimpleArtist) + go func() { + err := m.commands.RadioGivenArtist(item.SimpleArtist) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.SimpleTrack: - go HandleRadio(m.commands, item) + go func() { + err := m.commands.RadioGivenSong(item, 0) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.FullTrack: - go HandleRadio(m.commands, item.SimpleTrack) + go func() { + err := m.commands.RadioGivenSong(item.SimpleTrack, 0) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.PlaylistTrack: - go HandleRadio(m.commands, item.Track.SimpleTrack) + go func() { + err := m.commands.RadioGivenSong(item.Track.SimpleTrack, 0) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.PlaylistItem: - go HandleRadio(m.commands, item.Track.Track.SimpleTrack) + go func() { + err := m.commands.RadioGivenSong(item.Track.Track.SimpleTrack, 0) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return case spotify.SavedTrack: - go HandleRadio(m.commands, item.SimpleTrack) + go func() { + err := m.commands.RadioGivenSong(item.SimpleTrack, 0) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() return } } @@ -254,29 +309,38 @@ func (m *mainModel) SendMessage(msg string, duration time.Duration) { } func (m *mainModel) QueueItem() error { + var id spotify.ID + var name string switch item := m.list.SelectedItem().(mainItem).SpotifyItem.(type) { case spotify.PlaylistTrack: - go m.SendMessage("Adding "+item.Track.Name+" to queue", 2*time.Second) - go HandleQueueItem(m.commands, item.Track.ID) + name = item.Track.Name + id = item.Track.ID case spotify.SavedTrack: - go m.SendMessage("Adding "+item.Name+" to queue", 2*time.Second) - go HandleQueueItem(m.commands, item.ID) + name = item.Name + id = item.ID case spotify.SimpleTrack: - go m.SendMessage("Adding "+item.Name+" to queue", 2*time.Second) - go HandleQueueItem(m.commands, item.ID) + name = item.Name + id = item.ID case spotify.FullTrack: - go m.SendMessage("Adding "+item.Name+" to queue", 2*time.Second) - go HandleQueueItem(m.commands, item.ID) + name = item.Name + id = item.ID case *spotify.FullTrack: - go m.SendMessage("Adding "+item.Name+" to queue", 2*time.Second) - go HandleQueueItem(m.commands, item.ID) + name = item.Name + id = item.ID case *spotify.SimpleTrack: - go m.SendMessage("Adding "+item.Name+" to queue", 2*time.Second) - go HandleQueueItem(m.commands, item.ID) + name = item.Name + id = item.ID case *spotify.SimplePlaylist: - go m.SendMessage("Adding "+item.Name+" to queue", 2*time.Second) - go HandleQueueItem(m.commands, item.ID) + name = item.Name + id = item.ID } + go m.SendMessage("Adding "+name+" to queue", 2*time.Second) + go func() { + err := m.commands.QueueSong(id) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() if m.mode == Queue { go func() { new_items, err := QueueView(m.commands) @@ -296,10 +360,13 @@ func (m *mainModel) DeleteTrackFromPlaylist() error { track := m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.PlaylistTrack).Track go m.SendMessage("Deleteing "+track.Name+" from "+m.playlist.Name, 2*time.Second) go func() { - HandleDeleteTrackFromPlaylist(m.commands, track.ID, m.playlist.ID) + err := m.commands.DeleteTracksFromPlaylist([]spotify.ID{track.ID}, m.playlist.ID) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } new_items, err := PlaylistView(m.commands, m.playlist) if err != nil { - return + m.SendMessage(err.Error(), 5*time.Second) } m.list.SetItems(new_items) }() @@ -311,10 +378,13 @@ func (m *mainModel) SelectItem() error { case Queue: page = 1 go func() { - HandleNextInQueue(m.commands, m.list.Index()) + err := m.commands.Next(m.list.Index(), true) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } new_items, err := QueueView(m.commands) if err != nil { - return + m.SendMessage(err.Error(), 5*time.Second) } m.list.SetItems(new_items) m.list.ResetSelected() @@ -471,17 +541,43 @@ func (m *mainModel) SelectItem() error { m.list.ResetSelected() case Album, ArtistAlbum, SearchArtistAlbum, SearchAlbum: pos := m.list.Cursor() + (m.list.Paginator.Page * m.list.Paginator.TotalPages) - go HandlePlayWithContext(m.commands, &m.album.URI, &pos) + go func() { + _ = m.commands.PlaySongInPlaylist(&m.album.URI, &pos) + }() case Playlist, SearchPlaylist: pos := m.list.Cursor() + (m.list.Paginator.Page * m.list.Paginator.PerPage) - go HandlePlayWithContext(m.commands, &m.playlist.URI, &pos) + go func() { + _ = m.commands.PlaySongInPlaylist(&m.playlist.URI, &pos) + }() case Tracks: - go HandlePlayLikedSong(m.commands, m.list.Cursor()+(m.list.Paginator.Page*m.list.Paginator.PerPage)) + go func() { + err := m.commands.PlayLikedSongs(m.list.Cursor() + (m.list.Paginator.Page * m.list.Paginator.PerPage)) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() case SearchTracks: - go HandlePlayTrack(m.commands, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.FullTrack).ID) + go func() { + err := m.commands.QueueSong(m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.FullTrack).ID) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + return + } + err = m.commands.Next(1, false) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + return + } + }() case Devices: - go HandleSetDevice(m.commands, m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.PlayerDevice)) - go m.SendMessage("Setting device to "+m.list.SelectedItem().FilterValue(), 2*time.Second) + go func() { + err := m.commands.SetDevice(m.list.SelectedItem().(mainItem).SpotifyItem.(spotify.PlayerDevice).ID) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } else { + m.SendMessage("Setting device to "+m.list.SelectedItem().FilterValue(), 2*time.Second) + } + }() m.mode = "main" new_items, err := MainView(m.commands) if err != nil { @@ -654,16 +750,36 @@ func (m *mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } if msg.String() == ">" { - go HandleSeek(m.commands, true) + go func() { + err := m.commands.Seek(true) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() } if msg.String() == "<" { - go HandleSeek(m.commands, false) + go func() { + err := m.commands.Seek(false) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() } if msg.String() == "+" { - go HandleVolume(m.commands, true) + go func() { + err := m.commands.ChangeVolume(10) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() } if msg.String() == "-" { - go HandleVolume(m.commands, false) + go func() { + err := m.commands.ChangeVolume(-10) + if err != nil { + m.SendMessage(err.Error(), 5*time.Second) + } + }() } // search input if m.input.Focused() { @@ -769,7 +885,7 @@ func InitMain(c *commands.Commander, mode Mode) (tea.Model, error) { mode: mode, progress: prog, } - m.list.Title = "GOSPT" + m.list.Title = "GSPOT" go m.TickPlayback() Tick() m.list.DisableQuitKeybindings() diff --git a/src/components/tui/tui.go b/src/components/tui/tui.go index ca34999..8bfc571 100644 --- a/src/components/tui/tui.go +++ b/src/components/tui/tui.go @@ -3,7 +3,7 @@ package tui import ( tea "github.com/charmbracelet/bubbletea" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/commands" + "git.asdf.cafe/abs3nt/gspot/src/components/commands" ) // StartTea the entry point for the UI. Initializes the model. diff --git a/src/components/tui/views.go b/src/components/tui/views.go index a4172e4..04a3829 100644 --- a/src/components/tui/views.go +++ b/src/components/tui/views.go @@ -9,7 +9,7 @@ import ( "github.com/zmb3/spotify/v2" "golang.org/x/sync/errgroup" - "git.asdf.cafe/abs3nt/gospt-ng/src/components/commands" + "git.asdf.cafe/abs3nt/gspot/src/components/commands" ) const regex = `<.*?>` diff --git a/src/components/youtube/youtube.go b/src/components/youtube/youtube.go index 7b70465..6fd9290 100644 --- a/src/components/youtube/youtube.go +++ b/src/components/youtube/youtube.go @@ -94,7 +94,7 @@ func Search(query string) string { ctx := context.Background() confDir, _ := os.UserConfigDir() - b, err := os.ReadFile(filepath.Join(confDir, "gospt", "client_secret.json")) + b, err := os.ReadFile(filepath.Join(confDir, "gspot", "client_secret.json")) if err != nil { log.Fatalf("Unable to read client secret file: %v", err) } diff --git a/src/services/auth.go b/src/services/auth.go index 94f8f07..325622b 100644 --- a/src/services/auth.go +++ b/src/services/auth.go @@ -17,7 +17,7 @@ import ( "golang.org/x/exp/slog" "golang.org/x/oauth2" - "git.asdf.cafe/abs3nt/gospt-ng/src/config" + "git.asdf.cafe/abs3nt/gspot/src/config" ) type SpotifyClientResult struct { @@ -79,8 +79,8 @@ func NewSpotifyClient(conf *config.Config) (c SpotifyClientResult, err error) { spotifyauth.ScopeStreaming, ), ) - if _, err := os.Stat(filepath.Join(configDir, "gospt/auth.json")); err == nil { - authFilePath := filepath.Join(configDir, "gospt/auth.json") + if _, err := os.Stat(filepath.Join(configDir, "gspot/auth.json")); err == nil { + authFilePath := filepath.Join(configDir, "gspot/auth.json") authFile, err := os.Open(authFilePath) if err != nil { return SpotifyClientResult{}, err @@ -157,7 +157,7 @@ func completeAuth(w http.ResponseWriter, r *http.Request) { slog.Error("AUTHENTICATOR", "failed to unmarshal", err) os.Exit(1) } - err = os.WriteFile(filepath.Join(configDir, "gospt/auth.json"), out, 0o600) + err = os.WriteFile(filepath.Join(configDir, "gspot/auth.json"), out, 0o600) if err != nil { slog.Error("AUTHENTICATOR", "failed to save auth", err) }